|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Resource; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
|
6
|
|
|
use prgTW\BaseCRM\Transport\Transport; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Resource extends BaseResource |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var \Resource[] */ |
|
11
|
|
|
protected $subResources = []; |
|
12
|
|
|
|
|
13
|
|
|
/** @var Transport */ |
|
14
|
|
|
protected $transport; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $uri; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Transport $transport |
|
21
|
|
|
* @param string $uri |
|
22
|
|
|
* @param array $data |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Transport $transport, $uri, array $data = []) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->transport = $transport; |
|
27
|
|
|
$this->uri = $uri; |
|
28
|
|
|
if ([] !== $data) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->hydrate($data); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$this->init(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract protected function getEndpoint(); |
|
40
|
|
|
|
|
41
|
|
|
protected function init() |
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $suffix |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function getFullUri($suffix = '') |
|
52
|
|
|
{ |
|
53
|
|
|
return sprintf('%s/%s/%s%s', static::getEndpoint(), static::PREFIX, $this->uri, $suffix); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* |
|
59
|
|
|
* @return Resource|\Resource[] |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getSubResources($name = null) |
|
62
|
|
|
{ |
|
63
|
|
|
if (isset($name)) |
|
64
|
|
|
{ |
|
65
|
|
|
return isset($this->subResources[$name]) ? $this->subResources[$name] : null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->subResources; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string[] $resourceClassNames |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \InvalidArgumentException when class name is not a Resource |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function setSubResources(array $resourceClassNames) |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($resourceClassNames as $key => $value) |
|
79
|
|
|
{ |
|
80
|
|
|
$resourceClassName = is_int($key) ? $value : $key; |
|
81
|
|
|
if (false === is_subclass_of($resourceClassName, Resource::class)) |
|
82
|
|
|
{ |
|
83
|
|
|
//@codeCoverageIgnoreStart |
|
84
|
|
|
throw new \InvalidArgumentException(sprintf('Class %s is not an instance of %s', $resourceClassName, Resource::class)); |
|
|
|
|
|
|
85
|
|
|
//@codeCoverageIgnoreEnd |
|
86
|
|
|
} |
|
87
|
|
|
$resourceName = $this->getResourceName($resourceClassName); |
|
88
|
|
|
$uri = ltrim(sprintf('%s/%s', $this->uri, $resourceName), '/'); |
|
89
|
|
|
if (false === is_int($key)) |
|
90
|
|
|
{ |
|
91
|
|
|
$uri = $value; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$class = new $resourceClassName($this->transport, $uri); |
|
95
|
|
|
|
|
96
|
|
|
$this->subResources[lcfirst(Inflector::camelize($resourceName))] = $class; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param array $params |
|
102
|
|
|
* @param string $instanceClassName |
|
103
|
|
|
* @param string $idParam |
|
104
|
|
|
* |
|
105
|
|
|
* @return Resource |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getObjectFromArray(array $params, $instanceClassName = null, $idParam = 'id') |
|
108
|
|
|
{ |
|
109
|
|
|
if (null === $instanceClassName) |
|
110
|
|
|
{ |
|
111
|
|
|
$instanceClassName = Inflector::singularize(static::class); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (array_key_exists($idParam, $params)) |
|
115
|
|
|
{ |
|
116
|
|
|
$uri = sprintf('%s/%s', $this->uri, $params[$idParam]); |
|
117
|
|
|
} |
|
118
|
|
|
else |
|
119
|
|
|
{ |
|
120
|
|
|
$uri = $this->uri; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return new $instanceClassName($this->transport, $uri, $params); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $method |
|
128
|
|
|
* @param $arguments |
|
129
|
|
|
* |
|
130
|
|
|
* @return Resource|ResourceCollection |
|
131
|
|
|
* @throws \BadMethodCallException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __call($method, $arguments) |
|
134
|
|
|
{ |
|
135
|
|
|
if ('get' !== substr($method, 0, 3)) |
|
136
|
|
|
{ |
|
137
|
|
|
//@codeCoverageIgnoreStart |
|
138
|
|
|
throw new \BadMethodCallException('Unknown resource'); |
|
139
|
|
|
//@codeCoverageIgnoreEnd |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$key = lcfirst(substr($method, 3)); |
|
143
|
|
|
$subResource = $this->getSubResources($key); |
|
144
|
|
|
if (null === $subResource) |
|
145
|
|
|
{ |
|
146
|
|
|
//@codeCoverageIgnoreStart |
|
147
|
|
|
throw new \BadMethodCallException(sprintf('Resource "%s" not found', $key)); |
|
148
|
|
|
//@codeCoverageIgnoreEnd |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $subResource; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return Transport |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getTransport() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->transport; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.