42 $this->_builderFactory = $builderFactory;
57 if (!isset($this->_data[$entityName])) {
58 throw new \InvalidArgumentException(sprintf(
'Unknown validation entity "%s"', $entityName));
61 if (!isset($this->_data[$entityName][$groupName])) {
62 throw new \InvalidArgumentException(
63 sprintf(
'Unknown validation group "%s" in entity "%s"', $groupName, $entityName)
67 $builderClass = isset(
68 $this->_data[$entityName][$groupName][
'builder']
72 throw new \InvalidArgumentException(sprintf(
'Builder class "%s" was not found', $builderClass));
75 $builder = $this->_builderFactory->create(
77 [
'constraints' => $this->_data[$entityName][$groupName][
'constraints']]
80 throw new \InvalidArgumentException(
81 sprintf(
'Builder "%s" must extend \Magento\Framework\Validator\Builder', $builderClass)
85 $builder->addConfigurations($builderConfig);
98 public function createValidator($entityName, $groupName, array $builderConfig =
null)
114 foreach ($dom->getElementsByTagName(
'entity') as
$entity) {
126 protected function _extractEntityGroupsConstraintsData(\DOMElement
$entity)
129 $rulesConstraints = $this->_extractRulesConstraintsData(
$entity);
133 $groupConstraints = [];
136 foreach (
$group->getElementsByTagName(
'use') as $use) {
137 $ruleName = $use->getAttribute(
'rule');
138 if (isset($rulesConstraints[$ruleName])) {
139 $groupConstraints = array_merge($groupConstraints, $rulesConstraints[$ruleName]);
143 $result[
$group->getAttribute(
'name')] = [
'constraints' => $groupConstraints];
144 if (
$group->hasAttribute(
'builder')) {
149 unset($groupConstraints);
150 unset($rulesConstraints);
161 protected function _extractRulesConstraintsData(\DOMElement
$entity)
165 foreach (
$entity->getElementsByTagName(
'rule') as
$rule) {
166 $ruleName =
$rule->getAttribute(
'name');
169 foreach (
$rule->getElementsByTagName(
'property_constraints') as $propertyConstraints) {
171 foreach ($propertyConstraints->getElementsByTagName(
'property') as $property) {
173 foreach ($property->getElementsByTagName(
'constraint') as
$constraint) {
178 'property' => $property->getAttribute(
'name'),
186 foreach (
$rule->getElementsByTagName(
'entity_constraints') as $entityConstraints) {
188 foreach ($entityConstraints->getElementsByTagName(
'constraint') as
$constraint) {
242 $callback = $this->_readCallback(
$children);
263 protected function _collectChildren(
$element)
267 foreach (
$element->childNodes as $node) {
268 if (!$node instanceof \DOMElement) {
271 $nodeName = strtolower($node->nodeName);
272 if (!array_key_exists($nodeName,
$children)) {
286 protected function _readArguments(
$children)
288 if (array_key_exists(
'argument',
$children)) {
291 foreach (
$children[
'argument'] as $node) {
292 $nodeChildren = $this->_collectChildren($node);
293 $callback = $this->_readCallback($nodeChildren);
294 $options = $this->_readOptions($nodeChildren);
300 $argument = $node->textContent;
315 protected function _readCallback(
$children)
317 if (array_key_exists(
'callback',
$children)) {
320 foreach (
$children[
'callback'] as $callbackData) {
321 $callbacks[] =
new Callback(
322 [trim($callbackData->getAttribute(
'class')), trim($callbackData->getAttribute(
'method'))],
338 protected function _readOptions(
$children)
340 if (array_key_exists(
'option',
$children)) {
345 if (
$option->hasAttribute(
'name')) {
351 return new Option(
$data);
376 protected function _readMethods(
$children)
378 if (array_key_exists(
'method',
$children)) {
383 $methodName =
$method->getAttribute(
'name');
384 $methodOptions = [
'method' => $methodName];
389 $methods[$methodName] = $methodOptions;
403 return __DIR__ .
'/etc/validation.xsd';
413 return '<?xml version="1.0" encoding="UTF-8"?>' .
414 '<validation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></validation>';
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'
elseif(isset( $params[ 'redirect_parent']))
defined('TESTS_BP')||define('TESTS_BP' __DIR__
const CONSTRAINT_TYPE_PROPERTY
const CONSTRAINT_TYPE_ENTITY
_extractData(\DOMDocument $dom)
_extractConstraintOptions(\DOMElement $constraint)
createValidator($entityName, $groupName, array $builderConfig=null)
createValidatorBuilder($entityName, $groupName, array $builderConfig=null)
__construct( $configFiles, \Magento\Framework\Config\DomFactory $domFactory, \Magento\Framework\Validator\UniversalFactory $builderFactory)