|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PhpMob package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace PhpMob\Omise; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Ishmael Doss <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Facade |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Api[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $api = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Model |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $domain; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $data |
|
33
|
|
|
*/ |
|
34
|
55 |
|
public function __construct(array $data = []) |
|
35
|
|
|
{ |
|
36
|
55 |
|
$class = str_replace('Facade', 'Domain', get_called_class()); |
|
37
|
|
|
|
|
38
|
55 |
|
$this->domain = new $class($data); |
|
39
|
55 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $data |
|
43
|
|
|
* |
|
44
|
|
|
* @return static |
|
45
|
|
|
*/ |
|
46
|
9 |
|
public static function make(array $data = []) |
|
47
|
|
|
{ |
|
48
|
9 |
|
return new static($data); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Api $api |
|
53
|
|
|
*/ |
|
54
|
62 |
|
public static function setApi(Api $api) |
|
55
|
|
|
{ |
|
56
|
62 |
|
self::$api[get_called_class()] = $api; |
|
57
|
62 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return Api |
|
61
|
|
|
*/ |
|
62
|
62 |
|
private static function getApiForClass() |
|
63
|
|
|
{ |
|
64
|
62 |
|
return self::$api[get_called_class()]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $method |
|
69
|
|
|
*/ |
|
70
|
62 |
|
private static function checkApiMethodExists($method) |
|
71
|
|
|
{ |
|
72
|
62 |
|
if (!method_exists(self::getApiForClass(), $method)) { |
|
73
|
|
|
throw new \InvalidArgumentException( |
|
74
|
|
|
sprintf('Not found method named `%s` for `%s` api.', $method, get_called_class()) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
62 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $method |
|
81
|
|
|
* @param array $args |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed|self |
|
84
|
|
|
*/ |
|
85
|
43 |
|
public static function __callStatic($method, $args) |
|
86
|
|
|
{ |
|
87
|
43 |
|
self::checkApiMethodExists($method); |
|
88
|
|
|
|
|
89
|
43 |
|
return self::getApiForClass()->$method(...$args); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $method |
|
94
|
|
|
* @param $args |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
33 |
|
public function __call($method, $args) |
|
99
|
|
|
{ |
|
100
|
33 |
|
if (method_exists($this->domain, $method)) { |
|
101
|
29 |
|
return $this->domain->$method(...$args); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
33 |
|
self::checkApiMethodExists($method); |
|
105
|
|
|
|
|
106
|
33 |
|
return self::getApiForClass()->$method($this->domain, ...$args); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
39 |
|
public function __get($name) |
|
110
|
|
|
{ |
|
111
|
39 |
|
$result = $this->domain->$name; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* No need due to `FacadeHydration` implementation. |
|
115
|
|
|
* if ($result instanceof Model) { |
|
116
|
|
|
* $class = str_replace('Domain', 'Facade', get_class($result)); |
|
117
|
|
|
* |
|
118
|
|
|
* return new $class($result->toArray()); |
|
119
|
|
|
* } |
|
120
|
|
|
*/ |
|
121
|
|
|
|
|
122
|
39 |
|
return $result; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
23 |
|
public function __set($name, $value) |
|
126
|
|
|
{ |
|
127
|
23 |
|
$this->domain->$name = $value; |
|
128
|
23 |
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|