Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
9 
13 
15 {
19  const CONSTRAINT_TYPE_ENTITY = 'entity';
20 
21  const CONSTRAINT_TYPE_PROPERTY = 'property';
25  protected $_defaultBuilderClass = \Magento\Framework\Validator\Builder::class;
26 
30  protected $_builderFactory;
31 
37  public function __construct(
38  $configFiles,
39  \Magento\Framework\Config\DomFactory $domFactory,
40  \Magento\Framework\Validator\UniversalFactory $builderFactory
41  ) {
42  $this->_builderFactory = $builderFactory;
43  parent::__construct($configFiles, $domFactory);
44  }
45 
55  public function createValidatorBuilder($entityName, $groupName, array $builderConfig = null)
56  {
57  if (!isset($this->_data[$entityName])) {
58  throw new \InvalidArgumentException(sprintf('Unknown validation entity "%s"', $entityName));
59  }
60 
61  if (!isset($this->_data[$entityName][$groupName])) {
62  throw new \InvalidArgumentException(
63  sprintf('Unknown validation group "%s" in entity "%s"', $groupName, $entityName)
64  );
65  }
66 
67  $builderClass = isset(
68  $this->_data[$entityName][$groupName]['builder']
69  ) ? $this->_data[$entityName][$groupName]['builder'] : $this->_defaultBuilderClass;
70 
71  if (!class_exists($builderClass)) {
72  throw new \InvalidArgumentException(sprintf('Builder class "%s" was not found', $builderClass));
73  }
74 
75  $builder = $this->_builderFactory->create(
76  $builderClass,
77  ['constraints' => $this->_data[$entityName][$groupName]['constraints']]
78  );
79  if (!$builder instanceof \Magento\Framework\Validator\Builder) {
80  throw new \InvalidArgumentException(
81  sprintf('Builder "%s" must extend \Magento\Framework\Validator\Builder', $builderClass)
82  );
83  }
84  if ($builderConfig) {
85  $builder->addConfigurations($builderConfig);
86  }
87  return $builder;
88  }
89 
98  public function createValidator($entityName, $groupName, array $builderConfig = null)
99  {
100  return $this->createValidatorBuilder($entityName, $groupName, $builderConfig)->createValidator();
101  }
102 
109  protected function _extractData(\DOMDocument $dom)
110  {
111  $result = [];
112 
114  foreach ($dom->getElementsByTagName('entity') as $entity) {
115  $result[$entity->getAttribute('name')] = $this->_extractEntityGroupsConstraintsData($entity);
116  }
117  return $result;
118  }
119 
126  protected function _extractEntityGroupsConstraintsData(\DOMElement $entity)
127  {
128  $result = [];
129  $rulesConstraints = $this->_extractRulesConstraintsData($entity);
130 
132  foreach ($entity->getElementsByTagName('group') as $group) {
133  $groupConstraints = [];
134 
136  foreach ($group->getElementsByTagName('use') as $use) {
137  $ruleName = $use->getAttribute('rule');
138  if (isset($rulesConstraints[$ruleName])) {
139  $groupConstraints = array_merge($groupConstraints, $rulesConstraints[$ruleName]);
140  }
141  }
142 
143  $result[$group->getAttribute('name')] = ['constraints' => $groupConstraints];
144  if ($group->hasAttribute('builder')) {
145  $result[$group->getAttribute('name')]['builder'] = $group->getAttribute('builder');
146  }
147  }
148 
149  unset($groupConstraints);
150  unset($rulesConstraints);
151 
152  return $result;
153  }
154 
161  protected function _extractRulesConstraintsData(\DOMElement $entity)
162  {
163  $rules = [];
165  foreach ($entity->getElementsByTagName('rule') as $rule) {
166  $ruleName = $rule->getAttribute('name');
167 
169  foreach ($rule->getElementsByTagName('property_constraints') as $propertyConstraints) {
171  foreach ($propertyConstraints->getElementsByTagName('property') as $property) {
173  foreach ($property->getElementsByTagName('constraint') as $constraint) {
174  $rules[$ruleName][] = [
175  'alias' => $constraint->getAttribute('alias'),
176  'class' => $constraint->getAttribute('class'),
177  'options' => $this->_extractConstraintOptions($constraint),
178  'property' => $property->getAttribute('name'),
180  ];
181  }
182  }
183  }
184 
186  foreach ($rule->getElementsByTagName('entity_constraints') as $entityConstraints) {
188  foreach ($entityConstraints->getElementsByTagName('constraint') as $constraint) {
189  $rules[$ruleName][] = [
190  'alias' => $constraint->getAttribute('alias'),
191  'class' => $constraint->getAttribute('class'),
192  'options' => $this->_extractConstraintOptions($constraint),
194  ];
195  }
196  }
197  }
198 
199  return $rules;
200  }
201 
208  protected function _extractConstraintOptions(\DOMElement $constraint)
209  {
210  if (!$constraint->hasChildNodes()) {
211  return null;
212  }
213  $options = [];
214  $children = $this->_collectChildren($constraint);
215 
230  $arguments = $this->_readArguments($children);
231  if ($arguments) {
232  $options['arguments'] = $arguments;
233  }
234 
242  $callback = $this->_readCallback($children);
243  if ($callback) {
244  $options['callback'] = $callback;
245  }
246 
250  $methods = $this->_readMethods($children);
251  if ($methods) {
252  $options['methods'] = $methods;
253  }
254  return $options;
255  }
256 
263  protected function _collectChildren($element)
264  {
265  $children = [];
267  foreach ($element->childNodes as $node) {
268  if (!$node instanceof \DOMElement) {
269  continue;
270  }
271  $nodeName = strtolower($node->nodeName);
272  if (!array_key_exists($nodeName, $children)) {
273  $children[$nodeName] = [];
274  }
275  $children[$nodeName][] = $node;
276  }
277  return $children;
278  }
279 
286  protected function _readArguments($children)
287  {
288  if (array_key_exists('argument', $children)) {
289  $arguments = [];
291  foreach ($children['argument'] as $node) {
292  $nodeChildren = $this->_collectChildren($node);
293  $callback = $this->_readCallback($nodeChildren);
294  $options = $this->_readOptions($nodeChildren);
295  if ($callback) {
296  $arguments[] = $callback[0];
297  } elseif ($options) {
298  $arguments[] = $options;
299  } else {
300  $argument = $node->textContent;
301  $arguments[] = new Option(trim($argument));
302  }
303  }
304  return $arguments;
305  }
306  return null;
307  }
308 
315  protected function _readCallback($children)
316  {
317  if (array_key_exists('callback', $children)) {
318  $callbacks = [];
320  foreach ($children['callback'] as $callbackData) {
321  $callbacks[] = new Callback(
322  [trim($callbackData->getAttribute('class')), trim($callbackData->getAttribute('method'))],
323  null,
324  true
325  );
326  }
327  return $callbacks;
328  }
329  return null;
330  }
331 
338  protected function _readOptions($children)
339  {
340  if (array_key_exists('option', $children)) {
341  $data = [];
343  foreach ($children['option'] as $option) {
344  $value = trim($option->textContent);
345  if ($option->hasAttribute('name')) {
346  $data[$option->getAttribute('name')] = $value;
347  } else {
348  $data[] = $value;
349  }
350  }
351  return new Option($data);
352  }
353  return null;
354  }
355 
376  protected function _readMethods($children)
377  {
378  if (array_key_exists('method', $children)) {
379  $methods = [];
381  foreach ($children['method'] as $method) {
382  $children = $this->_collectChildren($method);
383  $methodName = $method->getAttribute('name');
384  $methodOptions = ['method' => $methodName];
385  $arguments = $this->_readArguments($children);
386  if ($arguments) {
387  $methodOptions['arguments'] = $arguments;
388  }
389  $methods[$methodName] = $methodOptions;
390  }
391  return $methods;
392  }
393  return null;
394  }
395 
401  public function getSchemaFile()
402  {
403  return __DIR__ . '/etc/validation.xsd';
404  }
405 
411  protected function _getInitialXml()
412  {
413  return '<?xml version="1.0" encoding="UTF-8"?>' .
414  '<validation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></validation>';
415  }
416 
422  protected function _getIdAttributes()
423  {
424  return [
425  '/validation/entity' => 'name',
426  '/validation/entity/rules/rule' => 'name',
427  '/validation/entity/rules/rule/entity_constraints/constraint' => 'class',
428  '/validation/entity/rules/rule/property_constraints/property/constraint' => 'class',
429  '/validation/entity/rules/rule/property_constraints/property' => 'name',
430  '/validation/entity/groups/group' => 'name',
431  '/validation/entity/groups/group/uses/use' => 'rule'
432  ];
433  }
434 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
_extractConstraintOptions(\DOMElement $constraint)
Definition: Config.php:208
$group
Definition: sections.phtml:16
$methods
Definition: billing.phtml:71
$value
Definition: gender.phtml:16
$entity
Definition: element.phtml:22
$method
Definition: info.phtml:13
$arguments
$children
Definition: actions.phtml:11
createValidator($entityName, $groupName, array $builderConfig=null)
Definition: Config.php:98
createValidatorBuilder($entityName, $groupName, array $builderConfig=null)
Definition: Config.php:55
__construct( $configFiles, \Magento\Framework\Config\DomFactory $domFactory, \Magento\Framework\Validator\UniversalFactory $builderFactory)
Definition: Config.php:37
$element
Definition: element.phtml:12