AtLeastOneTriggerRule.php
3 years ago
ConsistentStepMapRule.php
3 years ago
NoCycleRule.php
2 years ago
NoDuplicateEdgesRule.php
2 years ago
NoJoinRule.php
2 years ago
NoUnreachableStepsRule.php
2 years ago
TriggerNeedsToBeFollowedByActionRule.php
2 years ago
TriggersUnderRootRule.php
2 years ago
UnknownStepRule.php
3 years ago
ValidStepArgsRule.php
3 years ago
ValidStepFiltersRule.php
2 years ago
ValidStepOrderRule.php
3 years ago
ValidStepRule.php
2 months ago
ValidStepValidationRule.php
3 years ago
index.php
3 years ago
ValidStepArgsRule.php
75 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Validation\AutomationRules; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Registry; |
| 10 | use MailPoet\Automation\Engine\Validation\AutomationGraph\AutomationNode; |
| 11 | use MailPoet\Automation\Engine\Validation\AutomationGraph\AutomationNodeVisitor; |
| 12 | use MailPoet\Validator\ValidationException; |
| 13 | use MailPoet\Validator\Validator; |
| 14 | |
| 15 | class ValidStepArgsRule implements AutomationNodeVisitor { |
| 16 | /** @var Registry */ |
| 17 | private $registry; |
| 18 | |
| 19 | /** @var Validator */ |
| 20 | private $validator; |
| 21 | |
| 22 | public function __construct( |
| 23 | Registry $registry, |
| 24 | Validator $validator |
| 25 | ) { |
| 26 | $this->registry = $registry; |
| 27 | $this->validator = $validator; |
| 28 | } |
| 29 | |
| 30 | public function initialize(Automation $automation): void { |
| 31 | } |
| 32 | |
| 33 | public function visitNode(Automation $automation, AutomationNode $node): void { |
| 34 | $step = $node->getStep(); |
| 35 | $registryStep = $this->registry->getStep($step->getKey()); |
| 36 | if (!$registryStep) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $schema = $registryStep->getArgsSchema(); |
| 41 | $properties = $schema->toArray()['properties'] ?? null; |
| 42 | if (!$properties) { |
| 43 | $this->validator->validate($schema, $step->getArgs()); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $errors = []; |
| 48 | foreach ($properties as $property => $propertySchema) { |
| 49 | $schemaToValidate = array_merge( |
| 50 | $schema->toArray(), |
| 51 | ['properties' => [$property => $propertySchema]] |
| 52 | ); |
| 53 | try { |
| 54 | $this->validator->validateSchemaArray( |
| 55 | $schemaToValidate, |
| 56 | $step->getArgs(), |
| 57 | $property |
| 58 | ); |
| 59 | } catch (ValidationException $e) { |
| 60 | $errors[$property] = $e->getWpError()->get_error_code(); |
| 61 | } |
| 62 | } |
| 63 | if ($errors) { |
| 64 | $throwable = ValidationException::create(); |
| 65 | foreach ($errors as $errorKey => $errorMsg) { |
| 66 | $throwable->withError((string)$errorKey, (string)$errorMsg); |
| 67 | } |
| 68 | throw $throwable; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public function complete(Automation $automation): void { |
| 73 | } |
| 74 | } |
| 75 |