1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Service\Behavior; |
4
|
|
|
|
5
|
|
|
use Instantiator\Exception\InvalidArgumentException; |
6
|
|
|
use prgTW\BaseCRM\Resource\CustomField; |
7
|
|
|
use prgTW\BaseCRM\Resource\CustomFieldsCollection; |
8
|
|
|
use prgTW\BaseCRM\Resource\DetachedResource; |
9
|
|
|
use prgTW\BaseCRM\Resource\LazyLoadedResource; |
10
|
|
|
use prgTW\BaseCRM\Service\Enum\CustomFields; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @property-read array $data |
14
|
|
|
*/ |
15
|
|
|
trait CustomFieldsTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param string $name |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
* |
22
|
|
|
* @throws \InvalidArgumentException when name is not string |
23
|
|
|
*/ |
24
|
|
|
public function hasCustomField($name) |
25
|
|
|
{ |
26
|
|
|
if (false === is_string($name)) |
27
|
|
|
{ |
28
|
|
|
throw new InvalidArgumentException('fieldName is not string'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if ($this instanceof LazyLoadedResource) |
32
|
|
|
{ |
33
|
|
|
$this->lazyLoadIfNecessary(); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$key = $this->getCustomFieldsKey(); |
37
|
|
|
|
38
|
|
|
if (false === array_key_exists($key, $this->data)) |
39
|
|
|
{ |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return array_key_exists($name, $this->data[$key]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* |
49
|
|
|
* @return null|CustomField |
50
|
|
|
* @throws \InvalidArgumentException when name is not string |
51
|
|
|
*/ |
52
|
|
|
public function getCustomField($name) |
53
|
|
|
{ |
54
|
|
|
if (false === is_string($name)) |
55
|
|
|
{ |
56
|
|
|
throw new InvalidArgumentException('fieldName is not string'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (false === $this->hasCustomField($name)) |
60
|
|
|
{ |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$key = $this->getCustomFieldsKey(); |
65
|
|
|
|
66
|
|
|
return new CustomField($name, $this->data[$key][$name]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @param mixed $value |
72
|
|
|
*/ |
73
|
|
|
public function setCustomField($name, $value) |
74
|
|
|
{ |
75
|
|
|
if ($this instanceof LazyLoadedResource) |
76
|
|
|
{ |
77
|
|
|
$this->lazyLoadIfNecessary(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$customField = new CustomField($name, $value); |
81
|
|
|
$key = $this->getCustomFieldsKey(); |
82
|
|
|
$this->data[$key][$name] = $customField->getData(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return null|CustomFieldsCollection |
87
|
|
|
*/ |
88
|
|
|
public function getCustomFields() |
89
|
|
|
{ |
90
|
|
|
if ($this instanceof LazyLoadedResource) |
91
|
|
|
{ |
92
|
|
|
$this->lazyLoadIfNecessary(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$key = $this->getCustomFieldsKey(); |
96
|
|
|
|
97
|
|
|
if (false === array_key_exists($key, $this->data)) |
98
|
|
|
{ |
99
|
|
|
return new CustomFieldsCollection([]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$customFields = []; |
103
|
|
|
foreach (array_keys($this->data[$key]) as $fieldName) |
104
|
|
|
{ |
105
|
|
|
$customFields[$fieldName] = $this->getCustomField($fieldName); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return new CustomFieldsCollection($customFields); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function getCustomFieldsKey() |
115
|
|
|
{ |
116
|
|
|
return $this instanceof DetachedResource ? CustomFields::KEY_CREATING : CustomFields::KEY_FETCHING; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.