Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Builder.php
Go to the documentation of this file.
1 <?php
10 
12 
13 class Builder
14 {
18  protected $_constraints;
19 
24 
28  protected $_validatorFactory;
29 
34 
41  public function __construct(
42  \Magento\Framework\Validator\ConstraintFactory $constraintFactory,
43  \Magento\Framework\ValidatorFactory $validatorFactory,
44  \Magento\Framework\Validator\UniversalFactory $oneValidatorFactory,
45  array $constraints
46  ) {
47  foreach ($constraints as $constraint) {
48  if (isset($constraint['options']) && is_array($constraint['options'])) {
49  $this->_checkConfigurationArguments($constraint['options'], true);
50  $this->_checkConfigurationCallback($constraint['options'], true);
51  }
52  }
53  $this->_constraints = $constraints;
54  $this->_constraintFactory = $constraintFactory;
55  $this->_validatorFactory = $validatorFactory;
56  $this->_oneValidatorFactory = $oneValidatorFactory;
57  }
58 
67  protected function _checkConfigurationArguments(array $configuration, $argumentsIsArray)
68  {
69  // https://jira.corp.x.com/browse/MAGETWO-10439
70  $allowedKeys = ['arguments', 'callback', 'method', 'methods', 'breakChainOnFailure'];
71  if (!array_intersect($allowedKeys, array_keys($configuration))) {
72  throw new \InvalidArgumentException('Configuration has incorrect format');
73  }
74  // Check method arguments
75  if ($argumentsIsArray) {
76  if (array_key_exists('methods', $configuration)) {
77  foreach ($configuration['methods'] as $method) {
78  $this->_checkMethodArguments($method);
79  }
80  }
81  } elseif (array_key_exists('method', $configuration)) {
82  $this->_checkMethodArguments($configuration);
83  }
84 
85  // Check constructor arguments
86  if (array_key_exists('arguments', $configuration) && !is_array($configuration['arguments'])) {
87  throw new \InvalidArgumentException('Arguments must be an array');
88  }
89  }
90 
98  protected function _checkMethodArguments(array $configuration)
99  {
100  if (!is_string($configuration['method'])) {
101  throw new \InvalidArgumentException('Method has to be passed as string');
102  }
103  if (array_key_exists('arguments', $configuration) && !is_array($configuration['arguments'])) {
104  throw new \InvalidArgumentException('Method arguments must be an array');
105  }
106  }
107 
116  protected function _checkConfigurationCallback(array $configuration, $callbackIsArray)
117  {
118  if (array_key_exists('callback', $configuration)) {
119  if ($callbackIsArray) {
120  $callbacks = $configuration['callback'];
121  } else {
122  $callbacks = [$configuration['callback']];
123  }
124  foreach ($callbacks as $callback) {
125  if (!$callback instanceof \Magento\Framework\Validator\Constraint\Option\Callback) {
126  throw new \InvalidArgumentException(
127  'Callback must be instance of \Magento\Framework\Validator\Constraint\Option\Callback'
128  );
129  }
130  }
131  }
132  }
133 
139  public function createValidator()
140  {
141  return $this->_createValidatorInstance();
142  }
143 
149  protected function _createValidatorInstance()
150  {
151  $validator = $this->_validatorFactory->create();
152  foreach ($this->_constraints as $constraintData) {
153  // https://jira.corp.x.com/browse/MAGETWO-10439
154  $breakChainOnFailure = !empty($constraintData['options']['breakChainOnFailure']);
155  $validator->addValidator($this->_createConstraint($constraintData), $breakChainOnFailure);
156  }
157  return $validator;
158  }
159 
168  public function addConfiguration($alias, array $configuration)
169  {
170  $this->_checkConfigurationArguments($configuration, false);
171  $this->_checkConfigurationCallback($configuration, false);
172  foreach ($this->_constraints as &$constraint) {
173  if ($constraint['alias'] != $alias) {
174  continue;
175  }
176  if (!array_key_exists('options', $constraint) || !is_array($constraint['options'])) {
177  $constraint['options'] = [];
178  }
179  if (!array_key_exists('method', $configuration)) {
180  if (array_key_exists('arguments', $configuration)) {
181  $constraint['options']['arguments'] = $configuration['arguments'];
182  } elseif (array_key_exists('callback', $configuration)) {
183  $constraint = $this->_addConstraintCallback($constraint, $configuration['callback']);
184  }
185  } else {
186  $constraint = $this->_addConstraintMethod($constraint, $configuration);
187  }
188  }
189 
190  return $this;
191  }
192 
200  protected function _addConstraintCallback(
201  array $constraint,
202  \Magento\Framework\Validator\Constraint\Option\Callback $callback
203  ) {
204  if (!array_key_exists('callback', $constraint['options'])) {
205  $constraint['options']['callback'] = [];
206  }
207  $constraint['options']['callback'][] = $callback;
208  return $constraint;
209  }
210 
218  protected function _addConstraintMethod(array $constraint, array $configuration)
219  {
220  if (!array_key_exists('methods', $constraint['options'])) {
221  $constraint['options']['methods'] = [];
222  }
223  $constraint['options']['methods'][] = $configuration;
224  return $constraint;
225  }
226 
233  public function addConfigurations(array $configurations)
234  {
235  foreach ($configurations as $alias => $concreteConfigs) {
236  foreach ($concreteConfigs as $configuration) {
237  $this->addConfiguration($alias, $configuration);
238  }
239  }
240  return $this;
241  }
242 
249  protected function _createConstraint(array $data)
250  {
251  // Create validator instance
252  $validator = $this->_createConstraintValidator($data);
253  if (isset($data['options']) && is_array($data['options'])) {
254  $this->_configureConstraintValidator($validator, $data['options']);
255  }
256 
257  if (\Magento\Framework\Validator\Config::CONSTRAINT_TYPE_PROPERTY == $data['type']) {
258  $result = new \Magento\Framework\Validator\Constraint\Property(
259  $validator,
260  $data['property'],
261  $data['alias']
262  );
263  } else {
264  $result = $this->_constraintFactory->create(['validator' => $validator, 'alias' => $data['alias']]);
265  }
266 
267  return $result;
268  }
269 
277  protected function _createConstraintValidator(array $data)
278  {
279  $validator = $this->_oneValidatorFactory->create(
280  $data['class'],
281  isset(
282  $data['options']['arguments']
283  ) ? $this->_applyArgumentsCallback(
284  $data['options']['arguments']
285  ) : []
286  );
287 
288  // Check validator type
289  if (!$validator instanceof \Magento\Framework\Validator\ValidatorInterface) {
290  throw new \InvalidArgumentException(
291  sprintf(
292  'Constraint class "%s" must implement \Magento\Framework\Validator\ValidatorInterface',
293  $data['class']
294  )
295  );
296  }
297 
298  return $validator;
299  }
300 
308  protected function _configureConstraintValidator(
309  \Magento\Framework\Validator\ValidatorInterface $validator,
310  array $options
311  ) {
312  // Call all validator methods according to configuration
313  if (isset($options['methods'])) {
314  foreach ($options['methods'] as $methodData) {
315  $methodName = $methodData['method'];
316  if (method_exists($validator, $methodName)) {
317  if (array_key_exists('arguments', $methodData)) {
318  $arguments = $this->_applyArgumentsCallback($methodData['arguments']);
319  call_user_func_array([$validator, $methodName], $arguments);
320  } else {
321  call_user_func([$validator, $methodName]);
322  }
323  }
324  }
325  }
326 
327  // Call validator configurators if any
328  if (isset($options['callback'])) {
330  foreach ($options['callback'] as $callback) {
331  $callback->setArguments($validator);
332  $callback->getValue();
333  }
334  }
335  }
336 
343  protected function _applyArgumentsCallback(array $arguments)
344  {
345  foreach ($arguments as &$argument) {
346  if (is_array($argument)) {
347  $argument = $this->_applyArgumentsCallback($argument);
348  } elseif ($argument instanceof OptionInterface) {
349  $argument = $argument->getValue();
350  }
351  }
352  return $arguments;
353  }
354 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$configuration
Definition: index.php:33
__construct(\Magento\Framework\Validator\ConstraintFactory $constraintFactory, \Magento\Framework\ValidatorFactory $validatorFactory, \Magento\Framework\Validator\UniversalFactory $oneValidatorFactory, array $constraints)
Definition: Builder.php:41
addConfigurations(array $configurations)
Definition: Builder.php:233
_applyArgumentsCallback(array $arguments)
Definition: Builder.php:343
_checkMethodArguments(array $configuration)
Definition: Builder.php:98
_checkConfigurationCallback(array $configuration, $callbackIsArray)
Definition: Builder.php:116
$method
Definition: info.phtml:13
$arguments
if(!trim($html)) $alias
Definition: details.phtml:20
_checkConfigurationArguments(array $configuration, $argumentsIsArray)
Definition: Builder.php:67
_addConstraintCallback(array $constraint, \Magento\Framework\Validator\Constraint\Option\Callback $callback)
Definition: Builder.php:200
addConfiguration($alias, array $configuration)
Definition: Builder.php:168
_addConstraintMethod(array $constraint, array $configuration)
Definition: Builder.php:218