1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Resource; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
use prgTW\BaseCRM\Exception\ResourceException; |
7
|
|
|
|
8
|
|
|
abstract class BaseResource |
9
|
|
|
{ |
10
|
|
|
const ENDPOINT_APP = 'https://app.futuresimple.com'; |
11
|
|
|
const ENDPOINT_COMMON = 'https://common.futuresimple.com'; |
12
|
|
|
const ENDPOINT_CORE = 'https://core.futuresimple.com'; |
13
|
|
|
const ENDPOINT_CRM = 'https://crm.futuresimple.com'; |
14
|
|
|
const ENDPOINT_LEADS = 'https://leads.futuresimple.com'; |
15
|
|
|
const ENDPOINT_SALES = 'https://sales.futuresimple.com'; |
16
|
|
|
const ENDPOINT_TAGS = 'https://tags.futuresimple.com'; |
17
|
|
|
|
18
|
|
|
const PREFIX = 'api/v1'; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $data = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $resourceClassName Fully classified class name |
|
|
|
|
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
protected function getClassNameOnly($resourceClassName = null) |
29
|
|
|
{ |
30
|
|
|
$name = null === $resourceClassName ? static::class : $resourceClassName; |
31
|
|
|
$name = explode('\\', $name); |
32
|
|
|
$name = end($name); |
33
|
|
|
|
34
|
|
|
return $name; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $resourceClassName Fully classified class name |
|
|
|
|
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getResourceName($resourceClassName = null) |
43
|
|
|
{ |
44
|
|
|
$name = $this->getClassNameOnly($resourceClassName); |
45
|
|
|
|
46
|
|
|
return Inflector::tableize($name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $resourceClassName Fully classified class name |
|
|
|
|
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
protected function getChildResourceName($resourceClassName = null) |
55
|
|
|
{ |
56
|
|
|
$resourceName = $this->getResourceName($resourceClassName); |
57
|
|
|
$singular = Inflector::singularize($resourceName); |
58
|
|
|
|
59
|
|
|
return $singular; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $data |
64
|
|
|
*/ |
65
|
|
|
protected function hydrate(array $data) |
66
|
|
|
{ |
67
|
|
|
$this->data = []; |
68
|
|
View Code Duplication |
foreach ($data as $key => $value) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$setter = sprintf('set%s', ucfirst(Inflector::camelize($key))); |
71
|
|
|
if (method_exists($this, $setter)) |
72
|
|
|
{ |
73
|
|
|
$this->$setter($value); |
74
|
|
|
} |
75
|
|
|
else |
76
|
|
|
{ |
77
|
|
|
$this->data[$key] = $value; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->postHydrate($data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $fieldNames |
86
|
|
|
* |
87
|
|
|
* @throws ResourceException when dehydration has been stopped |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function dehydrate(array $fieldNames = []) |
91
|
|
|
{ |
92
|
|
|
$result = $this->preDehydrate($fieldNames); |
93
|
|
|
if (false === $result) |
94
|
|
|
{ |
95
|
|
|
//@codeCoverageIgnoreStart |
96
|
|
|
throw new ResourceException('Dehydration has been stopped'); |
97
|
|
|
//@codeCoverageIgnoreEnd |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (is_array($result)) |
101
|
|
|
{ |
102
|
|
|
$fieldNames = $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$data = []; |
106
|
|
|
if ([] === $fieldNames) |
107
|
|
|
{ |
108
|
|
|
$data = $this->data; |
109
|
|
|
} |
110
|
|
|
else |
111
|
|
|
{ |
112
|
|
View Code Duplication |
foreach ($fieldNames as $fieldName) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$getter = sprintf('get%s', ucfirst(Inflector::camelize($fieldName))); |
115
|
|
|
$data[$fieldName] = method_exists($this, $getter) ? $this->$getter() : $this->data[$fieldName]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->postDehydrate($data); |
120
|
|
|
|
121
|
|
|
return $data; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $name |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function __isset($name) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$issetter = sprintf('isset%s', ucfirst(Inflector::camelize($name))); |
132
|
|
|
if (method_exists($this, $issetter)) |
133
|
|
|
{ |
134
|
|
|
return $this->$issetter(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$key = Inflector::tableize($name); |
138
|
|
|
if (array_key_exists($key, $this->data)) |
139
|
|
|
{ |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return isset($this->$name); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $name |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function __get($name) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$getter = sprintf('get%s', ucfirst(Inflector::camelize($name))); |
154
|
|
|
if (method_exists($this, $getter)) |
155
|
|
|
{ |
156
|
|
|
return $this->$getter(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$key = Inflector::tableize($name); |
160
|
|
|
if (array_key_exists($key, $this->data)) |
161
|
|
|
{ |
162
|
|
|
return $this->data[$key]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->$name; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $name |
170
|
|
|
* @param mixed $value |
171
|
|
|
*/ |
172
|
|
|
public function __set($name, $value) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$setter = sprintf('set%s', ucfirst(Inflector::camelize($name))); |
175
|
|
|
if (method_exists($this, $setter)) |
176
|
|
|
{ |
177
|
|
|
return $this->$setter($value); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$key = Inflector::tableize($name); |
181
|
|
|
$this->data[$key] = $value; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $data |
186
|
|
|
* |
187
|
|
|
* @codeCoverageIgnore |
188
|
|
|
*/ |
189
|
|
|
protected function postHydrate(array $data) |
190
|
|
|
{ |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $fieldNames |
196
|
|
|
* |
197
|
|
|
* @return array|bool False: stop dehydration, array: new field names |
|
|
|
|
198
|
|
|
* |
199
|
|
|
* @codeCoverageIgnore |
200
|
|
|
*/ |
201
|
|
|
protected function preDehydrate(array $fieldNames) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param array $data |
208
|
|
|
* |
209
|
|
|
* @codeCoverageIgnore |
210
|
|
|
*/ |
211
|
|
|
protected function postDehydrate(array &$data) |
212
|
|
|
{ |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.