Skip to content

Commit a5a73c3

Browse files
committed
refactor: add new way to make function calls
1 parent c1ecad4 commit a5a73c3

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $mta = new Mta($server, $auth);
4242

4343
$response = $mta->getResource('someResource')->call('callableFunction', $arg1, $arg2, $arg3, ...);
4444
// or
45-
$response = $mta->getResource('someResource')->callableFunction($arg1, $arg2, $arg3, ...);
45+
$response = $mta->getResource('someResource')->call->callableFunction($arg1, $arg2, $arg3, ...);
4646

4747
var_dump($response);
4848
```

src/Model/Resource.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ class Resource implements JsonSerializable
3030
*/
3131
private $mtaService;
3232

33+
public $call;
34+
3335
public const SERVER_PREFIX = '^R^';
3436
protected const UNDEFINED_SERVICE_EXCEPTION = 'Resource %s can not be called because Mta service is not defined';
3537

3638
public function __construct(string $name, MtaService $mtaService = null)
3739
{
3840
$this->name = $name;
3941
$this->mtaService = $mtaService;
42+
$this->call = new ResourceCall($this);
4043
}
4144

4245
public static function fromServer(string $value): self
@@ -63,12 +66,6 @@ public function call(string $function, array ...$arguments)
6366
return $this->mtaService->callFunction($this->name, $function, $arguments);
6467
}
6568

66-
public function __call(string $name, array $arguments)
67-
{
68-
array_unshift($arguments, $name);
69-
return call_user_func_array([$this, 'call'], $arguments);
70-
}
71-
7269
public function jsonSerialize(): string
7370
{
7471
return self::SERVER_PREFIX . $this->name;

src/Model/ResourceCall.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: ResourceCall.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Model;
16+
17+
use MultiTheftAuto\Sdk\Model\Resource as MtaResource;
18+
19+
class ResourceCall
20+
{
21+
/**
22+
* @var MtaResource
23+
*/
24+
protected $resource;
25+
26+
public function __construct(MtaResource $resource)
27+
{
28+
$this->resource = $resource;
29+
}
30+
31+
public function __call(string $name, array $arguments)
32+
{
33+
array_unshift($arguments, $name);
34+
return call_user_func_array([$this->resource, 'call'], $arguments);
35+
}
36+
}

tests/Functional/MtaTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testItReturnsResponseUsingDirectCall(): void
6262
$credential = new Authentication('someUser', 'somePassword');
6363

6464
$mta = new Mta($server, $credential, $client);
65-
$return = $mta->getResource('someCallableResource')->someCallableFunction();
65+
$return = $mta->getResource('someCallableResource')->call->someCallableFunction();
6666

6767
$this->assertInstanceOf(Resource::class, $return[0]);
6868
$this->assertEquals('someResource', $return[0]->getName());

0 commit comments

Comments
 (0)