| @@ 20-102 (lines=83) @@ | ||
| 17 | * Domain Validation should be done in Domain |
|
| 18 | * Should be used for prototyping project knowing you are accumulating technical debt |
|
| 19 | */ |
|
| 20 | abstract class AbstractPragmaticCommandResolver |
|
| 21 | { |
|
| 22 | /** @var UIValidationEngine */ |
|
| 23 | protected $validationEngine; |
|
| 24 | ||
| 25 | /** @var PragmaticRawValueValidator */ |
|
| 26 | protected $rawValueValidator; |
|
| 27 | ||
| 28 | /** @var PragmaticRequestAttributeValueValidator */ |
|
| 29 | protected $attributeValueValidator; |
|
| 30 | ||
| 31 | /** @var PragmaticRequestQueryStringValueValidator */ |
|
| 32 | protected $queryStringValueValidator; |
|
| 33 | ||
| 34 | public function __construct(UIValidationEngine $validationEngine = null, PragmaticRawValueValidator $rawValueValidator = null, PragmaticRequestAttributeValueValidator $attributeValueValidator = null, PragmaticRequestQueryStringValueValidator $queryStringValueValidator = null) |
|
| 35 | { |
|
| 36 | if (null === $validationEngine) { |
|
| 37 | $validationEngine = UIValidationEngine::initialize(); |
|
| 38 | } |
|
| 39 | ||
| 40 | if (null === $rawValueValidator) { |
|
| 41 | $rawValueValidator = new PragmaticRawValueValidator($validationEngine); |
|
| 42 | } |
|
| 43 | ||
| 44 | if (null === $attributeValueValidator) { |
|
| 45 | $attributeValueValidator = new PragmaticRequestAttributeValueValidator( |
|
| 46 | $rawValueValidator |
|
| 47 | ); |
|
| 48 | } |
|
| 49 | ||
| 50 | if (null === $queryStringValueValidator) { |
|
| 51 | $queryStringValueValidator = new PragmaticRequestQueryStringValueValidator( |
|
| 52 | $rawValueValidator |
|
| 53 | ); |
|
| 54 | } |
|
| 55 | ||
| 56 | $this->validationEngine = $validationEngine; |
|
| 57 | $this->rawValueValidator = $rawValueValidator; |
|
| 58 | $this->attributeValueValidator = $attributeValueValidator; |
|
| 59 | $this->queryStringValueValidator = $queryStringValueValidator; |
|
| 60 | } |
|
| 61 | ||
| 62 | /** |
|
| 63 | * @api |
|
| 64 | * Create a Command from a Request |
|
| 65 | * Perform the UI Validation (simple validation) |
|
| 66 | * Complex Validation will be done in the Domain |
|
| 67 | * @param ServerRequestInterface $request PSR7 Request |
|
| 68 | * |
|
| 69 | * @return object Immutable Command (DTO) |
|
| 70 | * @throws UIValidationCollectionException If any Validation fail |
|
| 71 | */ |
|
| 72 | abstract public function resolve(ServerRequestInterface $request); |
|
| 73 | ||
| 74 | /** |
|
| 75 | * @api |
|
| 76 | * Map a Command attributes from a Request into an array |
|
| 77 | * Perform the UI Validation (simple validation) |
|
| 78 | * |
|
| 79 | * @return mixed[] Attributes used to create the Command |
|
| 80 | * @throws CommandMappingException If any mapping validation failed |
|
| 81 | * @throws UIValidationCollectionException If any UI validation failed |
|
| 82 | */ |
|
| 83 | protected function resolveRequestAsArray(ServerRequestInterface $request): array |
|
| 84 | { |
|
| 85 | $values = $this->validateThenMapAttributes( |
|
| 86 | $request |
|
| 87 | ); |
|
| 88 | ||
| 89 | $this->validationEngine->guardAgainstAnyUIValidationException(); |
|
| 90 | ||
| 91 | return $values; |
|
| 92 | } |
|
| 93 | ||
| 94 | /** |
|
| 95 | * @api |
|
| 96 | * Resolve implementation |
|
| 97 | * @param ServerRequestInterface $request PSR-7 Request |
|
| 98 | * |
|
| 99 | * @return mixed[] |
|
| 100 | */ |
|
| 101 | abstract protected function validateThenMapAttributes(ServerRequestInterface $request): array; |
|
| 102 | } |
|
| 103 | ||
| @@ 16-98 (lines=83) @@ | ||
| 13 | /** |
|
| 14 | * Helper easing Command Resolver (mapping + UI Validation) implementation |
|
| 15 | */ |
|
| 16 | abstract class AbstractPureCommandResolver |
|
| 17 | { |
|
| 18 | /** @var UIValidationEngine */ |
|
| 19 | protected $validationEngine; |
|
| 20 | ||
| 21 | /** @var RawValueValidator */ |
|
| 22 | protected $rawValueValidator; |
|
| 23 | ||
| 24 | /** @var RequestAttributeValueValidator */ |
|
| 25 | protected $attributeValueValidator; |
|
| 26 | ||
| 27 | /** @var RequestQueryStringValueValidator */ |
|
| 28 | protected $queryStringValueValidator; |
|
| 29 | ||
| 30 | public function __construct(UIValidationEngine $validationEngine = null, RawValueValidator $rawValueValidator = null, RequestAttributeValueValidator $attributeValueValidator = null, RequestQueryStringValueValidator $queryStringValueValidator = null) |
|
| 31 | { |
|
| 32 | if (null === $validationEngine) { |
|
| 33 | $validationEngine = UIValidationEngine::initialize(); |
|
| 34 | } |
|
| 35 | ||
| 36 | if (null === $rawValueValidator) { |
|
| 37 | $rawValueValidator = new RawValueValidator($validationEngine); |
|
| 38 | } |
|
| 39 | ||
| 40 | if (null === $attributeValueValidator) { |
|
| 41 | $attributeValueValidator = new RequestAttributeValueValidator( |
|
| 42 | $rawValueValidator |
|
| 43 | ); |
|
| 44 | } |
|
| 45 | ||
| 46 | if (null === $queryStringValueValidator) { |
|
| 47 | $queryStringValueValidator = new RequestQueryStringValueValidator( |
|
| 48 | $rawValueValidator |
|
| 49 | ); |
|
| 50 | } |
|
| 51 | ||
| 52 | $this->validationEngine = $validationEngine; |
|
| 53 | $this->rawValueValidator = $rawValueValidator; |
|
| 54 | $this->attributeValueValidator = $attributeValueValidator; |
|
| 55 | $this->queryStringValueValidator = $queryStringValueValidator; |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * @api |
|
| 60 | * Create a Command from a Request |
|
| 61 | * Perform the UI Validation (simple validation) |
|
| 62 | * Complex Validation will be done in the Domain |
|
| 63 | * @param ServerRequestInterface $request PSR7 Request |
|
| 64 | * |
|
| 65 | * @return object Immutable Command (DTO) |
|
| 66 | * @throws UIValidationCollectionException If any Validation fail |
|
| 67 | */ |
|
| 68 | abstract public function resolve(ServerRequestInterface $request); |
|
| 69 | ||
| 70 | /** |
|
| 71 | * @api |
|
| 72 | * Map a Command attributes from a Request into an array |
|
| 73 | * Perform the UI Validation (simple validation) |
|
| 74 | * |
|
| 75 | * @return mixed[] Attributes used to create the Command |
|
| 76 | * @throws CommandMappingException If any mapping validation failed |
|
| 77 | * @throws UIValidationCollectionException If any UI validation failed |
|
| 78 | */ |
|
| 79 | protected function resolveRequestAsArray(ServerRequestInterface $request): array |
|
| 80 | { |
|
| 81 | $values = $this->validateThenMapAttributes( |
|
| 82 | $request |
|
| 83 | ); |
|
| 84 | ||
| 85 | $this->validationEngine->guardAgainstAnyUIValidationException(); |
|
| 86 | ||
| 87 | return $values; |
|
| 88 | } |
|
| 89 | ||
| 90 | /** |
|
| 91 | * @api |
|
| 92 | * Resolve implementation |
|
| 93 | * @param ServerRequestInterface $request PSR-7 Request |
|
| 94 | * |
|
| 95 | * @return mixed[] |
|
| 96 | */ |
|
| 97 | abstract protected function validateThenMapAttributes(ServerRequestInterface $request): array; |
|
| 98 | } |
|
| 99 | ||