Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
field.php
1<?php
18
24abstract class Field
25{
27 protected $name;
28
30 protected $dataType;
31
34
36 protected $title;
37
39 protected $validation = null;
40
42 protected $validators = null;
43
45 protected $additionalValidators = array();
46
48 protected $fetchDataModification = null;
49
52
54 protected $additionalFetchDataModifiers = array();
55
57 protected $saveDataModification = null;
58
61
63 protected $additionalSaveDataModifiers = array();
64
70 protected $isSerialized = false;
71
73 protected $parentField;
74
76 protected $entity;
77
82 protected static $oldDataTypes = array(
83 'float' => 'Bitrix\Main\ORM\Fields\FloatField',
84 'string' => 'Bitrix\Main\ORM\Fields\StringField',
85 'text' => 'Bitrix\Main\ORM\Fields\TextField',
86 'datetime' => 'Bitrix\Main\ORM\Fields\DatetimeField',
87 'date' => 'Bitrix\Main\ORM\Fields\DateField',
88 'integer' => 'Bitrix\Main\ORM\Fields\IntegerField',
89 'enum' => 'Bitrix\Main\ORM\Fields\EnumField',
90 'boolean' => 'Bitrix\Main\ORM\Fields\BooleanField'
91 );
92
99 public function __construct($name, $parameters = array())
100 {
101 if ($name == '')
102 {
103 throw new SystemException('Field name required');
104 }
105
106 $this->name = $name;
107 $this->dataType = null;
108 $this->initialParameters = $parameters;
109
110 if (isset($parameters['title']))
111 {
112 $this->title = $parameters['title'];
113 }
114
115 // validation
116 if (isset($parameters['validation']))
117 {
118 $this->validation = $parameters['validation'];
119 }
120
121 // fetch data modifiers
122 if (isset($parameters['fetch_data_modification']))
123 {
124 $this->fetchDataModification = $parameters['fetch_data_modification'];
125 }
126
127 // save data modifiers
128 if (isset($parameters['save_data_modification']))
129 {
130 $this->saveDataModification = $parameters['save_data_modification'];
131 }
132
133 if (!empty($parameters['serialized']))
134 {
135 $this->setSerialized();
136 }
137 }
138
144 public function setEntity(Entity $entity)
145 {
146 if ($this->entity !== null)
147 {
148 throw new SystemException(sprintf('Field "%s" already has entity', $this->name));
149 }
150
151 $this->entity = $entity;
152 }
153
154 public function resetEntity()
155 {
156 $this->entity = null;
157 }
158
159 abstract public function getTypeMask();
160
170 public function validateValue($value, $primary, $row, Result $result)
171 {
172 if ($value instanceof SqlExpression)
173 {
174 return $result;
175 }
176
177 $validators = $this->getValidators();
178
179 foreach ($validators as $validator)
180 {
181 if ($validator instanceof IValidator)
182 {
183 $vResult = $validator->validate($value, $primary, $row, $this);
184 }
185 else
186 {
187 $vResult = call_user_func_array($validator, array($value, $primary, $row, $this));
188 }
189
190 if ($vResult !== true)
191 {
192 if ($vResult instanceof EntityError)
193 {
194 $result->addError($vResult);
195 }
196 else
197 {
198 $result->addError(new FieldError($this, $vResult, FieldError::INVALID_VALUE));
199 }
200 }
201 }
202
203 return $result;
204 }
205
213 public function modifyValueBeforeSave($value, $data)
214 {
215 $modifiers = $this->getSaveDataModifiers();
216
217 foreach ($modifiers as $modifier)
218 {
219 $value = call_user_func_array($modifier, array($value, $data));
220 }
221
222 return $value;
223 }
224
229 public function getValidators()
230 {
231 if ($this->validators === null)
232 {
233 $this->validators = array();
234
235 if ($this->validation !== null)
236 {
237 $validators = call_user_func($this->validation);
238
239 if (!is_array($validators))
240 {
241 throw new SystemException(sprintf(
242 'Validation for %s field of %s entity should return array of validators',
243 $this->name, $this->entity->getDataClass()
244 ));
245 }
246
247 foreach ($validators as $validator)
248 {
249 $this->appendValidator($validator);
250 }
251 }
252
253 foreach ($this->additionalValidators as $validator)
254 {
255 $this->appendValidator($validator);
256 }
257 }
258
259 return $this->validators;
260 }
261
268 public function addValidator($validator)
269 {
270 // append only when not null. and when is null - delay it
271 if ($this->validators === null)
272 {
273 $this->additionalValidators[] = $validator;
274 }
275 else
276 {
277 $this->appendValidator($validator);
278 }
279
280 return $this;
281 }
282
288 protected function appendValidator($validator)
289 {
290 if (!($validator instanceof Validators\Validator) && !is_callable($validator))
291 {
292 throw new SystemException(sprintf(
293 'Validators of "%s" field of "%s" entity should be a Validator\Base or callback',
294 $this->name, $this->entity->getDataClass()
295 ));
296 }
297
298 $this->validators[] = $validator;
299 }
300
305 public function getFetchDataModifiers()
306 {
307 if ($this->fetchDataModifiers === null)
308 {
309 $this->fetchDataModifiers = array();
310
311 if ($this->fetchDataModification !== null)
312 {
313 $modifiers = call_user_func($this->fetchDataModification);
314
315 if (!is_array($modifiers))
316 {
317 throw new SystemException(sprintf(
318 'Fetch Data Modification for %s field of %s entity should return array of modifiers (callbacks)',
319 $this->name, $this->entity->getDataClass()
320 ));
321 }
322
323 foreach ($modifiers as $modifier)
324 {
325 $this->appendFetchDataModifier($modifier);
326 }
327 }
328
329 foreach ($this->additionalFetchDataModifiers as $modifier)
330 {
331 $this->appendFetchDataModifier($modifier);
332 }
333 }
334
336 }
337
344 public function addFetchDataModifier($modifier)
345 {
346 // append only when not null. and when is null - delay it
347 if ($this->fetchDataModifiers === null)
348 {
349 $this->additionalFetchDataModifiers[] = $modifier;
350 }
351 else
352 {
353 $this->appendFetchDataModifier($modifier);
354 }
355
356 return $this;
357 }
358
364 protected function appendFetchDataModifier($modifier)
365 {
366 if (!is_callable($modifier))
367 {
368 throw new SystemException(sprintf(
369 'Modifier of "%s" field of "%s" entity should be a callback',
370 $this->name, $this->entity->getDataClass()
371 ));
372 }
373
374 $this->fetchDataModifiers[] = $modifier;
375 }
376
381 public function getSaveDataModifiers()
382 {
383 if ($this->saveDataModifiers === null)
384 {
385 $this->saveDataModifiers = array();
386
387 if ($this->saveDataModification !== null)
388 {
389 $modifiers = call_user_func($this->saveDataModification);
390
391 if (!is_array($modifiers))
392 {
393 throw new SystemException(sprintf(
394 'Save Data Modification for %s field of %s entity should return array of modifiers (callbacks)',
395 $this->name, $this->entity->getDataClass()
396 ));
397 }
398
399 foreach ($modifiers as $modifier)
400 {
401 $this->appendSaveDataModifier($modifier);
402 }
403 }
404
405 foreach ($this->additionalSaveDataModifiers as $modifier)
406 {
407 $this->appendSaveDataModifier($modifier);
408 }
409 }
410
412 }
413
420 public function addSaveDataModifier($modifier)
421 {
422 // append only when not null. and when is null - delay it
423 if ($this->saveDataModifiers === null)
424 {
425 $this->additionalSaveDataModifiers[] = $modifier;
426 }
427 else
428 {
429 $this->appendSaveDataModifier($modifier);
430 }
431
432 return $this;
433 }
434
440 protected function appendSaveDataModifier($modifier)
441 {
442 if (!is_callable($modifier))
443 {
444 throw new SystemException(sprintf(
445 'Save modifier of "%s" field of "%s" entity should be a callback',
446 $this->name, $this->entity->getDataClass()
447 ));
448 }
449
450 $this->saveDataModifiers[] = $modifier;
451 }
452
456 public function isSerialized()
457 {
458 return !empty($this->isSerialized);
459 }
460
464 public function setSerialized()
465 {
466 if (!$this->isSerialized)
467 {
468 $this->isSerialized = true;
469
470 // add save- and fetch modifiers
471 $this->addSaveDataModifier(array($this, 'serialize'));
472 $this->addFetchDataModifier(array($this, 'unserialize'));
473 }
474 }
475
481 public function configureSerialized()
482 {
483 $this->setSerialized();
484 return $this;
485 }
486
487 public function getName()
488 {
489 return $this->name;
490 }
491
492 public function setName($name)
493 {
494 $this->name = $name;
495 }
496
504 public function configureTitle($title)
505 {
506 $this->title = $title;
507 return $this;
508 }
509
510 public function getTitle()
511 {
512 if($this->title !== null)
513 {
514 return $this->title;
515 }
516
517 if(($title = Loc::getMessage($this->getLangCode())) <> '')
518 {
519 return $this->title = $title;
520 }
521
522 return $this->title = $this->name;
523 }
524
525 public function setParameter($name, $value)
526 {
527 $this->initialParameters[$name] = $value;
528
529 return $this;
530 }
531
532 public function getParameter($name)
533 {
534 return $this->initialParameters[$name];
535 }
536
537 public function hasParameter($name)
538 {
539 return array_key_exists($name, $this->initialParameters);
540 }
541
546 {
547 $this->parentField = $parentField;
548 }
549
553 public function getParentField()
554 {
555 return $this->parentField;
556 }
557
562 public function getDataType()
563 {
564 if (empty($this->dataType))
565 {
566 return static::getOldDataTypeByField($this);
567 }
568
569 return $this->dataType;
570 }
571
578 public static function getOldDataTypeByClass($class)
579 {
580 $map = array_flip(static::$oldDataTypes);
581
582 return $map[$class] ?? 'string';
583 }
584
591 public static function getOldDataTypeByField(Field $field)
592 {
593 return static::getOldDataTypeByClass(get_class($field));
594 }
595
602 public static function getClassByOldDataType($dateType)
603 {
604 return isset(static::$oldDataTypes[$dateType]) ? '\\'.static::$oldDataTypes[$dateType] : false;
605 }
606
607 public function getEntity()
608 {
609 return $this->entity;
610 }
611
612 public function getLangCode()
613 {
614 $entity = $this->getEntity();
615 if($entity !== null)
616 {
617 return $entity->getLangCode().'_'.$this->getName().'_FIELD';
618 }
619 return null;
620 }
621
626 public function getConnection()
627 {
628 if ($this->entity)
629 {
630 return $this->entity->getConnection();
631 }
632
634 }
635
636 public function serialize($value)
637 {
638 return serialize($value);
639 }
640
641 public function unserialize($value)
642 {
643 return unserialize((string)$value);
644 }
645
650 public function postInitialize()
651 {
652 return null;
653 }
654}
static getConnection($name="")
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getOldDataTypeByClass($class)
Definition field.php:578
setEntity(Entity $entity)
Definition field.php:144
setParameter($name, $value)
Definition field.php:525
static getClassByOldDataType($dateType)
Definition field.php:602
setParentField(Field $parentField)
Definition field.php:545
appendSaveDataModifier($modifier)
Definition field.php:440
__construct($name, $parameters=array())
Definition field.php:99
appendFetchDataModifier($modifier)
Definition field.php:364
validateValue($value, $primary, $row, Result $result)
Definition field.php:170
modifyValueBeforeSave($value, $data)
Definition field.php:213
appendValidator($validator)
Definition field.php:288
static getOldDataTypeByField(Field $field)
Definition field.php:591
addFetchDataModifier($modifier)
Definition field.php:344
addSaveDataModifier($modifier)
Definition field.php:420
addError(Error $error)
Definition result.php:50