From 8415018ac8a673727c5a8f64927098fb6e79c4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Z=C4=81l=C4=ABtis?= Date: Fri, 5 Apr 2024 14:32:21 +0300 Subject: [PATCH 1/2] fix: wip: validation --- src/Task/AbstractDocumentTask.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Task/AbstractDocumentTask.php b/src/Task/AbstractDocumentTask.php index 0fb5bac..79b2752 100644 --- a/src/Task/AbstractDocumentTask.php +++ b/src/Task/AbstractDocumentTask.php @@ -16,6 +16,7 @@ use function array_flip; use function array_keys; use function array_merge; +use function array_merge_recursive; use function count; use function get_debug_type; use function implode; @@ -100,7 +101,7 @@ protected function validate(array $data): void { $count = 0; $invalid = []; - $fullData = array_merge($this->getRequiredFields(), $this->getOptionalFields()); + $fullData = array_merge_recursive($this->getRequiredFields(), $this->getOptionalFields()); $dataDump = self::makeOneDimension($data, onlyLast: true); $fullDump = self::makeOneDimension($fullData, onlyLast: true); @@ -112,7 +113,7 @@ protected function validate(array $data): void if (preg_match('/\.\d+\./', $check) && preg_match('/[0-9]/', $check) > 0) { preg_match_all('/\d+/', $check, $matches); $check = preg_replace("/\d/", '0', $check); - if ('0' !== $matches[0][0]) { + if ('0' !== $matches[0][0] && isset($requiredDump[$check])) { $requiredCount++; } } @@ -128,13 +129,13 @@ protected function validate(array $data): void $count += (int) isset($requiredDump[$check]); } - if ($requiredCount !== $count) { - throw new InvalidArgumentException(sprintf('Missing required fields: "%s"', implode(', ', array_diff(array_keys($requiredDump), array_keys($dataDump))))); - } - if ([] !== $invalid) { throw new InvalidArgumentException(sprintf('Invalid mapping found: "%s"', implode(', ', $invalid))); } + + if ($requiredCount !== $count) { + throw new InvalidArgumentException(sprintf('Missing required fields: "%s"', implode(', ', array_diff(array_keys($requiredDump), array_keys($dataDump))))); + } } /* From e5cf0697f41a15ca5fbec4fab2d9e09f4359d113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Z=C4=81l=C4=ABtis?= Date: Mon, 8 Apr 2024 14:22:52 +0300 Subject: [PATCH 2/2] fix: redo field check --- src/Contracts/Task.php | 4 +--- src/DataProvider/DocumentDataProvider.php | 10 ++++++++++ src/Task/AbstractDocumentTask.php | 22 ++++++---------------- src/Task/DocumentTask.php | 2 +- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/Contracts/Task.php b/src/Contracts/Task.php index 95654a9..8e9513f 100644 --- a/src/Contracts/Task.php +++ b/src/Contracts/Task.php @@ -12,9 +12,7 @@ public function getGenerator(): Generator; public function getTransformer(): Transformer; - public function getRequiredFields(): array; - - public function getOptionalFields(): array; + public function getTransformerFields(): array; public function generate(mixed $input): Document; diff --git a/src/DataProvider/DocumentDataProvider.php b/src/DataProvider/DocumentDataProvider.php index edaa1b1..9ef8f50 100644 --- a/src/DataProvider/DocumentDataProvider.php +++ b/src/DataProvider/DocumentDataProvider.php @@ -22,6 +22,11 @@ public function setTask(DocumentTask $task): void $this->task = $task; } + /** + * @throws ReflectionException + * @throws ExceptionInterface + * @throws ResourceClassNotFoundException + */ public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null { if ('generate' === $operation->getName()) { @@ -41,6 +46,11 @@ protected function createResource(BaseEntity $entity, array $context): DocumentR return DocumentResource::create($entity, $context); } + /** + * @throws ReflectionException + * @throws ExceptionInterface + * @throws ResourceClassNotFoundException + */ private function receipt(Operation $operation, array $uriVariables = [], array $context = []): object|array|null { $entity = $this->entityManager->find($this->getEntityClass($operation), $uriVariables['id']); diff --git a/src/Task/AbstractDocumentTask.php b/src/Task/AbstractDocumentTask.php index 79b2752..de4ad2e 100644 --- a/src/Task/AbstractDocumentTask.php +++ b/src/Task/AbstractDocumentTask.php @@ -15,8 +15,6 @@ use function array_diff_key; use function array_flip; use function array_keys; -use function array_merge; -use function array_merge_recursive; use function count; use function get_debug_type; use function implode; @@ -58,8 +56,7 @@ final public function generate(mixed $input): Document $storageItem = (new StorageItem())->setFile(new ReplacingFile($result)); $this->em->persist($storageItem); - $sourceFields = array_merge($this->getRequiredFields(), $this->getOptionalFields()); - $sourceDump = self::makeOneDimension($sourceFields, onlyLast: true); + $sourceDump = self::makeOneDimension($this->getTransformerFields(), onlyLast: true); $dataDump = self::makeOneDimension($data, onlyLast: true); $extraKeys = array_diff(array_keys($sourceDump), array_keys($dataDump)); $sourceDump = array_diff_key($sourceDump, array_flip($extraKeys)); @@ -77,11 +74,6 @@ final public function generate(mixed $input): Document return $document; } - public function getOptionalFields(): array - { - return []; - } - public function getGenerator(): Generator { return $this->generator; @@ -101,19 +93,17 @@ protected function validate(array $data): void { $count = 0; $invalid = []; - $fullData = array_merge_recursive($this->getRequiredFields(), $this->getOptionalFields()); $dataDump = self::makeOneDimension($data, onlyLast: true); - $fullDump = self::makeOneDimension($fullData, onlyLast: true); - $requiredDump = self::makeOneDimension($this->getRequiredFields(), onlyLast: true); - $requiredCount = count($requiredDump); + $fullDump = self::makeOneDimension($this->getTransformerFields(), onlyLast: true); + $requiredCount = count($fullDump); foreach ($dataDump as $key => $value) { $check = $key; if (preg_match('/\.\d+\./', $check) && preg_match('/[0-9]/', $check) > 0) { preg_match_all('/\d+/', $check, $matches); $check = preg_replace("/\d/", '0', $check); - if ('0' !== $matches[0][0] && isset($requiredDump[$check])) { + if ('0' !== $matches[0][0] && isset($fullDump[$check])) { $requiredCount++; } } @@ -126,7 +116,7 @@ protected function validate(array $data): void throw new InvalidArgumentException(sprintf('Incompatible input type. Expected "%s", got "%s"', $fullDump[$check], get_debug_type($value))); } - $count += (int) isset($requiredDump[$check]); + $count += (int) isset($fullDump[$check]); } if ([] !== $invalid) { @@ -134,7 +124,7 @@ protected function validate(array $data): void } if ($requiredCount !== $count) { - throw new InvalidArgumentException(sprintf('Missing required fields: "%s"', implode(', ', array_diff(array_keys($requiredDump), array_keys($dataDump))))); + throw new InvalidArgumentException(sprintf('Missing required fields: "%s"', implode(', ', array_diff(array_keys($fullDump), array_keys($dataDump))))); } } diff --git a/src/Task/DocumentTask.php b/src/Task/DocumentTask.php index c9f2309..787b303 100644 --- a/src/Task/DocumentTask.php +++ b/src/Task/DocumentTask.php @@ -19,7 +19,7 @@ public function __construct( parent::__construct($em, $generator, $transformer); } - public function getRequiredFields(): array + public function getTransformerFields(): array { return $this->getInput()->getSourceData(); }