Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
reference.php
1<?php
19
25class Reference extends Relation
26{
28 protected $reference;
29
30 protected $joinType = Join::TYPE_LEFT;
31
33
34 protected $cascadeDeletePolicy = CascadePolicy::NO_ACTION; // follow | no_action
35
36 const ELEMENTAL_THIS = 1;
37 const ELEMENTAL_REF = 2;
38 const ELEMENTAL_BOTH = 3;
39
49 public function __construct($name, $referenceEntity, $referenceFilter, $parameters = array())
50 {
51 parent::__construct($name);
52
53 if ($referenceEntity instanceof Entity)
54 {
55 $this->refEntity = $referenceEntity;
56 $this->refEntityName = $referenceEntity->getFullName();
57 }
58 else
59 {
60 // this one could be without leading backslash and/or with Table-postfix
61 $this->refEntityName = Entity::normalizeName($referenceEntity);
62 }
63
64 if (empty($referenceFilter))
65 {
66 throw new ArgumentException('Reference for `'.$name.'` shouldn\'t be empty');
67 }
68
69 $this->reference = $referenceFilter;
70
71 if (isset($parameters['join_type']))
72 {
73 $join_type = strtoupper($parameters['join_type']);
74
75 if (in_array($join_type, Join::getTypes(), true))
76 {
77 $this->joinType = $join_type;
78 }
79 }
80 }
81
82 public function getTypeMask()
83 {
85 }
86
97 public function validateValue($value, $primary, $row, Result $result)
98 {
99 $remoteObjectClass = $this->getRefEntity()->getObjectClass();
100
101 if ($value !== null && !($value instanceof $remoteObjectClass))
102 {
103 $result->addError(new Error(sprintf(
104 'Expected instance of `%s`, got `%s` instead', $remoteObjectClass, get_class($value)
105 )));
106 }
107
108 return parent::validateValue($value, $primary, $row, $result);
109 }
110
111 public function getDataType()
112 {
114 }
115
116 public function getReference()
117 {
118 return $this->reference;
119 }
120
126 public function getElementals()
127 {
128 if (!($this->reference instanceof Filter))
129 {
130 return false;
131 }
132
133 $elemental = [];
134
135 foreach ($this->reference->getConditions() as $condition)
136 {
137 if (!($condition->getValue() instanceof ColumnExpression)
138 || $condition->getOperator() != '='
139 )
140 {
141 continue;
142 }
143
144 // ok, we have a column filter. one should be `this.` and another one `ref.`
145 $col1 = $condition->getColumn();
146 $col2 = $condition->getValue()->getDefinition();
147
148 $col1Flag = static::getElementalFlag($col1);
149 $col2Flag = static::getElementalFlag($col2);
150
151 if (($col1Flag + $col2Flag) == static::ELEMENTAL_BOTH)
152 {
153 // we have this and ref link
154 $key = ($col1Flag == static::ELEMENTAL_THIS) ? $col1 : $col2;
155 $value = ($col1Flag == static::ELEMENTAL_REF) ? $col1 : $col2;
156
157 // cut .this and .ref from the start of definitions
158 $key = substr($key, 5);
159 $value = substr($value, 4);
160
161 $elemental[$key] = $value;
162 }
163 }
164
165 return $elemental;
166 }
167
168 protected static function getElementalFlag($definition)
169 {
170 if (substr_count($definition, '.') == 1)
171 {
172 if (strpos($definition, 'this.') === 0)
173 {
174 return static::ELEMENTAL_THIS;
175 }
176 elseif (strpos($definition, 'ref.') === 0)
177 {
178 return static::ELEMENTAL_REF;
179 }
180 }
181
182 return 0;
183 }
184}
185
186
static normalizeName($entityName)
Definition entity.php:876
__construct($name, $referenceEntity, $referenceFilter, $parameters=array())
Definition reference.php:49
validateValue($value, $primary, $row, Result $result)
Definition reference.php:97
addError(Error $error)
Definition result.php:50