1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Resource; |
4
|
|
|
|
5
|
|
|
use prgTW\BaseCRM\Exception\ResourceException; |
6
|
|
|
use prgTW\BaseCRM\Service\Behavior\CustomFieldsTrait; |
7
|
|
|
use prgTW\BaseCRM\Service\Enum\CustomFields; |
8
|
|
|
|
9
|
|
|
abstract class InstanceResource extends LazyLoadedResource |
10
|
|
|
{ |
11
|
|
|
use CustomFieldsTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @throws \InvalidArgumentException when resource key is not found in the response |
15
|
|
|
* @return $this |
16
|
|
|
*/ |
17
|
|
|
public function get() |
18
|
|
|
{ |
19
|
|
|
$uri = $this->getFullUri(); |
20
|
|
|
$resourceName = $this->getResourceName(); |
21
|
|
|
$data = $this->transport->get($uri, $resourceName); |
22
|
|
|
|
23
|
|
|
$this->hydrate($data); |
|
|
|
|
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $fieldNames |
30
|
|
|
* |
31
|
|
|
* @throws ResourceException when there's no data to save |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function save(array $fieldNames = []) |
35
|
|
|
{ |
36
|
|
|
if ([] === $fieldNames) |
37
|
|
|
{ |
38
|
|
|
$fieldNames = array_keys($this->data); |
39
|
|
|
if ([] === $fieldNames) |
40
|
|
|
{ |
41
|
|
|
//@codeCoverageIgnoreStart |
42
|
|
|
throw new ResourceException('No data to save'); |
43
|
|
|
//@codeCoverageIgnoreEnd |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->save($fieldNames); |
47
|
|
|
} |
48
|
|
|
$resourceName = $this->getResourceName(); |
49
|
|
|
$uri = $this->getFullUri(); |
50
|
|
|
$data = $this->dehydrate($fieldNames); |
51
|
|
|
|
52
|
|
|
if (in_array(CustomFieldsTrait::class, class_uses($this))) |
53
|
|
|
{ |
54
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
55
|
|
|
$customFields = $this->getCustomFields()->toArray(); |
56
|
|
|
if ([] !== $customFields) |
57
|
|
|
{ |
58
|
|
|
$data[CustomFields::KEY_SAVING] = $customFields; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$response = $this->transport->put($uri, $resourceName, [ |
63
|
|
|
'query' => [ |
64
|
|
|
$resourceName => $data, |
65
|
|
|
], |
66
|
|
|
]); |
67
|
|
|
$this->hydrate($response); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** {@inheritdoc} */ |
73
|
|
|
protected function lazyLoad() |
74
|
|
|
{ |
75
|
|
|
$this->get(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.