1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
field.php
См. документацию.
1<?php
8
9namespace Bitrix\Main\ORM\Fields;
10use Bitrix\Main\Application;
11use Bitrix\Main\DB\SqlExpression;
12use Bitrix\Main\Localization\Loc;
13use Bitrix\Main\ORM\Data\Result;
14use Bitrix\Main\ORM\Entity;
15use Bitrix\Main\ORM\EntityError;
16use Bitrix\Main\ORM\Fields\Validators\IValidator;
17use Bitrix\Main\SystemException;
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
46
48 protected $fetchDataModification = null;
49
52
55
57 protected $saveDataModification = null;
58
61
64
70 protected $isSerialized = false;
71
73 protected $parentField;
74
76 protected $entity;
77
78 protected $connection = null;
83 protected static $oldDataTypes = array(
84 'float' => 'Bitrix\Main\ORM\Fields\FloatField',
85 'decimal' => 'Bitrix\Main\ORM\Fields\DecimalField',
86 'string' => 'Bitrix\Main\ORM\Fields\StringField',
87 'text' => 'Bitrix\Main\ORM\Fields\TextField',
88 'datetime' => 'Bitrix\Main\ORM\Fields\DatetimeField',
89 'date' => 'Bitrix\Main\ORM\Fields\DateField',
90 'integer' => 'Bitrix\Main\ORM\Fields\IntegerField',
91 'enum' => 'Bitrix\Main\ORM\Fields\EnumField',
92 'boolean' => 'Bitrix\Main\ORM\Fields\BooleanField'
93 );
94
101 public function __construct($name, $parameters = array())
102 {
103 if ($name == '')
104 {
105 throw new SystemException('Field name required');
106 }
107
108 $this->name = $name;
109 $this->dataType = null;
110 $this->initialParameters = $parameters;
111
112 if (isset($parameters['title']))
113 {
114 $this->title = $parameters['title'];
115 }
116
117 // validation
118 if (isset($parameters['validation']))
119 {
120 $this->validation = $parameters['validation'];
121 }
122
123 // fetch data modifiers
124 if (isset($parameters['fetch_data_modification']))
125 {
126 $this->fetchDataModification = $parameters['fetch_data_modification'];
127 }
128
129 // save data modifiers
130 if (isset($parameters['save_data_modification']))
131 {
132 $this->saveDataModification = $parameters['save_data_modification'];
133 }
134
135 if (!empty($parameters['serialized']))
136 {
137 $this->setSerialized();
138 }
139
140 if (isset($parameters['connection']))
141 {
142 $this->connection = $parameters['connection'];
143 }
144 }
145
151 public function setEntity(Entity $entity)
152 {
153 if ($this->entity !== null)
154 {
155 throw new SystemException(sprintf('Field "%s" already has entity', $this->name));
156 }
157
158 $this->entity = $entity;
159 }
160
161 public function resetEntity()
162 {
163 $this->entity = null;
164 }
165
166 abstract public function getTypeMask();
167
177 public function validateValue($value, $primary, $row, Result $result)
178 {
179 if ($value instanceof SqlExpression)
180 {
181 return $result;
182 }
183
184 $validators = $this->getValidators();
185
186 foreach ($validators as $validator)
187 {
188 if ($validator instanceof IValidator)
189 {
190 $vResult = $validator->validate($value, $primary, $row, $this);
191 }
192 else
193 {
194 $vResult = call_user_func_array($validator, array($value, $primary, $row, $this));
195 }
196
197 if ($vResult !== true)
198 {
199 if ($vResult instanceof EntityError)
200 {
201 $result->addError($vResult);
202 }
203 else
204 {
205 $result->addError(new FieldError($this, $vResult, FieldError::INVALID_VALUE));
206 }
207 }
208 }
209
210 return $result;
211 }
212
220 public function modifyValueBeforeSave($value, $data)
221 {
222 $modifiers = $this->getSaveDataModifiers();
223
224 foreach ($modifiers as $modifier)
225 {
226 $value = call_user_func_array($modifier, array($value, $data));
227 }
228
229 return $value;
230 }
231
236 public function getValidators()
237 {
238 if ($this->validators === null)
239 {
240 $this->validators = array();
241
242 if ($this->validation !== null)
243 {
244 $validators = call_user_func($this->validation);
245
246 if (!is_array($validators))
247 {
248 throw new SystemException(sprintf(
249 'Validation for %s field of %s entity should return array of validators',
250 $this->name, $this->entity->getDataClass()
251 ));
252 }
253
254 foreach ($validators as $validator)
255 {
256 $this->appendValidator($validator);
257 }
258 }
259
260 foreach ($this->additionalValidators as $validator)
261 {
262 $this->appendValidator($validator);
263 }
264 }
265
266 return $this->validators;
267 }
268
275 public function addValidator($validator)
276 {
277 // append only when not null. and when is null - delay it
278 if ($this->validators === null)
279 {
280 $this->additionalValidators[] = $validator;
281 }
282 else
283 {
284 $this->appendValidator($validator);
285 }
286
287 return $this;
288 }
289
295 protected function appendValidator($validator)
296 {
297 if (!($validator instanceof Validators\Validator) && !is_callable($validator))
298 {
299 throw new SystemException(sprintf(
300 'Validators of "%s" field of "%s" entity should be a Validator\Base or callback',
301 $this->name, $this->entity->getDataClass()
302 ));
303 }
304
305 $this->validators[] = $validator;
306 }
307
312 public function getFetchDataModifiers()
313 {
314 if ($this->fetchDataModifiers === null)
315 {
316 $this->fetchDataModifiers = array();
317
318 if ($this->fetchDataModification !== null)
319 {
320 $modifiers = call_user_func($this->fetchDataModification);
321
322 if (!is_array($modifiers))
323 {
324 throw new SystemException(sprintf(
325 'Fetch Data Modification for %s field of %s entity should return array of modifiers (callbacks)',
326 $this->name, $this->entity->getDataClass()
327 ));
328 }
329
330 foreach ($modifiers as $modifier)
331 {
332 $this->appendFetchDataModifier($modifier);
333 }
334 }
335
336 foreach ($this->additionalFetchDataModifiers as $modifier)
337 {
338 $this->appendFetchDataModifier($modifier);
339 }
340 }
341
343 }
344
351 public function addFetchDataModifier($modifier)
352 {
353 // append only when not null. and when is null - delay it
354 if ($this->fetchDataModifiers === null)
355 {
356 $this->additionalFetchDataModifiers[] = $modifier;
357 }
358 else
359 {
360 $this->appendFetchDataModifier($modifier);
361 }
362
363 return $this;
364 }
365
371 protected function appendFetchDataModifier($modifier)
372 {
373 if (!is_callable($modifier))
374 {
375 throw new SystemException(sprintf(
376 'Modifier of "%s" field of "%s" entity should be a callback',
377 $this->name, $this->entity->getDataClass()
378 ));
379 }
380
381 $this->fetchDataModifiers[] = $modifier;
382 }
383
388 public function getSaveDataModifiers()
389 {
390 if ($this->saveDataModifiers === null)
391 {
392 $this->saveDataModifiers = array();
393
394 if ($this->saveDataModification !== null)
395 {
396 $modifiers = call_user_func($this->saveDataModification);
397
398 if (!is_array($modifiers))
399 {
400 throw new SystemException(sprintf(
401 'Save Data Modification for %s field of %s entity should return array of modifiers (callbacks)',
402 $this->name, $this->entity->getDataClass()
403 ));
404 }
405
406 foreach ($modifiers as $modifier)
407 {
408 $this->appendSaveDataModifier($modifier);
409 }
410 }
411
412 foreach ($this->additionalSaveDataModifiers as $modifier)
413 {
414 $this->appendSaveDataModifier($modifier);
415 }
416 }
417
419 }
420
427 public function addSaveDataModifier($modifier)
428 {
429 // append only when not null. and when is null - delay it
430 if ($this->saveDataModifiers === null)
431 {
432 $this->additionalSaveDataModifiers[] = $modifier;
433 }
434 else
435 {
436 $this->appendSaveDataModifier($modifier);
437 }
438
439 return $this;
440 }
441
447 protected function appendSaveDataModifier($modifier)
448 {
449 if (!is_callable($modifier))
450 {
451 throw new SystemException(sprintf(
452 'Save modifier of "%s" field of "%s" entity should be a callback',
453 $this->name, $this->entity->getDataClass()
454 ));
455 }
456
457 $this->saveDataModifiers[] = $modifier;
458 }
459
463 public function isSerialized()
464 {
465 return !empty($this->isSerialized);
466 }
467
471 public function setSerialized()
472 {
473 if (!$this->isSerialized)
474 {
475 $this->isSerialized = true;
476
477 // add save- and fetch modifiers
478 $this->addSaveDataModifier(array($this, 'serialize'));
479 $this->addFetchDataModifier(array($this, 'unserialize'));
480 }
481 }
482
488 public function configureSerialized()
489 {
490 $this->setSerialized();
491 return $this;
492 }
493
494 public function getName()
495 {
496 return $this->name;
497 }
498
499 public function setName($name)
500 {
501 $this->name = $name;
502 }
503
511 public function configureTitle($title)
512 {
513 $this->title = $title;
514 return $this;
515 }
516
517 public function getTitle()
518 {
519 if($this->title !== null)
520 {
521 return $this->title;
522 }
523
524 if(($title = Loc::getMessage($this->getLangCode())) <> '')
525 {
526 return $this->title = $title;
527 }
528
529 return $this->title = $this->name;
530 }
531
532 public function setParameter($name, $value)
533 {
534 $this->initialParameters[$name] = $value;
535
536 return $this;
537 }
538
539 public function getParameter($name)
540 {
541 return $this->initialParameters[$name];
542 }
543
544 public function hasParameter($name)
545 {
546 return array_key_exists($name, $this->initialParameters);
547 }
548
553 {
554 $this->parentField = $parentField;
555 }
556
560 public function getParentField()
561 {
562 return $this->parentField;
563 }
564
569 public function getDataType()
570 {
571 if (empty($this->dataType))
572 {
573 return static::getOldDataTypeByField($this);
574 }
575
576 return $this->dataType;
577 }
578
585 public static function getOldDataTypeByClass($class)
586 {
587 $map = array_flip(static::$oldDataTypes);
588
589 return $map[$class] ?? 'string';
590 }
591
598 public static function getOldDataTypeByField(Field $field)
599 {
600 return static::getOldDataTypeByClass(get_class($field));
601 }
602
609 public static function getClassByOldDataType($dateType)
610 {
611 return isset(static::$oldDataTypes[$dateType]) ? '\\'.static::$oldDataTypes[$dateType] : false;
612 }
613
614 public function getEntity()
615 {
616 return $this->entity;
617 }
618
619 public function getLangCode()
620 {
621 $entity = $this->getEntity();
622 if($entity !== null)
623 {
624 return $entity->getLangCode().'_'.$this->getName().'_FIELD';
625 }
626 return null;
627 }
628
629 public function setConnection($connection)
630 {
631 $this->connection = $connection;
632 }
633
638 public function getConnection()
639 {
640 if ($this->entity)
641 {
642 return $this->entity->getConnection();
643 }
644 elseif ($this->connection)
645 {
646 return $this->connection;
647 }
648
650 }
651
652 public function serialize($value)
653 {
654 return serialize($value);
655 }
656
657 public function unserialize($value)
658 {
659 return unserialize((string)$value);
660 }
661
666 public function postInitialize()
667 {
668 return null;
669 }
670}
static getConnection($name="")
Определения application.php:638
getEntity()
Определения field.php:614
getDataType()
Определения field.php:569
static getOldDataTypeByClass($class)
Определения field.php:585
setEntity(Entity $entity)
Определения field.php:151
setParameter($name, $value)
Определения field.php:532
setName($name)
Определения field.php:499
addValidator($validator)
Определения field.php:275
getValidators()
Определения field.php:236
setConnection($connection)
Определения field.php:629
$saveDataModification
Определения field.php:57
$additionalSaveDataModifiers
Определения field.php:63
getParentField()
Определения field.php:560
getLangCode()
Определения field.php:619
isSerialized()
Определения field.php:463
getFetchDataModifiers()
Определения field.php:312
$fetchDataModification
Определения field.php:48
postInitialize()
Определения field.php:666
static getClassByOldDataType($dateType)
Определения field.php:609
$fetchDataModifiers
Определения field.php:51
$additionalFetchDataModifiers
Определения field.php:54
setParentField(Field $parentField)
Определения field.php:552
configureTitle($title)
Определения field.php:511
appendSaveDataModifier($modifier)
Определения field.php:447
__construct($name, $parameters=array())
Определения field.php:101
appendFetchDataModifier($modifier)
Определения field.php:371
validateValue($value, $primary, $row, Result $result)
Определения field.php:177
modifyValueBeforeSave($value, $data)
Определения field.php:220
appendValidator($validator)
Определения field.php:295
setSerialized()
Определения field.php:471
$saveDataModifiers
Определения field.php:60
serialize($value)
Определения field.php:652
unserialize($value)
Определения field.php:657
resetEntity()
Определения field.php:161
getConnection()
Определения field.php:638
static getOldDataTypeByField(Field $field)
Определения field.php:598
configureSerialized()
Определения field.php:488
getSaveDataModifiers()
Определения field.php:388
addFetchDataModifier($modifier)
Определения field.php:351
$isSerialized
Определения field.php:70
hasParameter($name)
Определения field.php:544
getParameter($name)
Определения field.php:539
$additionalValidators
Определения field.php:45
$initialParameters
Определения field.php:33
static $oldDataTypes
Определения field.php:83
addSaveDataModifier($modifier)
Определения field.php:427
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$map
Определения config.php:5
Определения ufield.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
<? endif;?> window document title
Определения prolog_main_admin.php:76