Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
objectfield.php
1<?php
10
17{
19 protected $objectClasses = [];
20
22 protected $encodeFunction;
23
25 protected $decodeFunction;
26
35 public function __construct($name, $parameters = [])
36 {
37 $this->addSaveDataModifier([$this, 'encode']);
38 $this->addFetchDataModifier([$this, 'decode']);
39
40 parent::__construct($name, $parameters);
41 }
42
46 public function getObjectClasses()
47 {
49 }
50
56 public function configureObjectClass($objectClass)
57 {
58 $classes = is_array($objectClass) ? $objectClass : [$objectClass];
59
60 foreach ($classes as $class)
61 {
62 if (substr($class, 0, 1) !== '\\')
63 {
64 $class = '\\'.$class;
65 }
66
67 $this->objectClasses[] = $class;
68 }
69
70 return $this;
71 }
72
80 public function configureSerializeCallback($callback)
81 {
82 $this->encodeFunction = $callback;
83
84 return $this;
85 }
86
94 public function configureUnserializeCallback($callback)
95 {
96 $this->decodeFunction = $callback;
97
98 return $this;
99 }
100
106 public function encode($value)
107 {
108 $callback = $this->encodeFunction;
109 return $callback($value);
110 }
111
117 public function decode($value)
118 {
119 $callback = $this->decodeFunction;
120 return $callback($value);
121 }
122
127 public function cast($value)
128 {
129 // try to check type
130 if (!empty($this->objectClasses))
131 {
132 $foundClass = false;
133
134 foreach ($this->objectClasses as $objectType)
135 {
136 if ($value instanceof $objectType)
137 {
138 $foundClass = true;
139 break;
140 }
141 }
142
143 if (!$foundClass)
144 {
145 trigger_error(sprintf(
146 'Invalid value type `%s` for `%s` field',
147 gettype($value) === 'object' ? get_class($value) : gettype($value),
148 $this->entity->getFullName().':'.$this->name
149 ), E_USER_WARNING
150 );
151 }
152 }
153
154 return $value;
155 }
156
160 public function convertValueFromDb($value)
161 {
162 return $this->getConnection()->getSqlHelper()->convertFromDbString($value);
163 }
164
168 public function convertValueToDb($value)
169 {
170 return $value === null && $this->is_nullable
171 ? $value
172 : $this->getConnection()->getSqlHelper()->convertToDbString($value);
173 }
174
178 public function getGetterTypeHint()
179 {
180 $type = 'mixed';
181
182 if (!empty($this->objectClasses))
183 {
184 $types = $this->objectClasses;
185
186 if ($this->is_nullable)
187 {
188 $types[] = 'null';
189 }
190
191 $type = join('|', $types);
192 }
193
194 return $type;
195 }
196
200 public function getSetterTypeHint()
201 {
202 $type = 'mixed';
203
204 if (!empty($this->objectClasses))
205 {
206 $types = $this->objectClasses;
207
208 if ($this->is_nullable)
209 {
210 $types[] = 'null';
211 }
212
213 $type = join('|', $types);
214 }
215
216 return $type;
217 }
218}
addFetchDataModifier($modifier)
Definition field.php:344
addSaveDataModifier($modifier)
Definition field.php:420
__construct($name, $parameters=[])