Skip to content

Commit

Permalink
Merge pull request #8 from whitedigital-eu/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
acirulis authored Apr 8, 2024
2 parents 1671674 + e5cf069 commit 4506e9a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
4 changes: 1 addition & 3 deletions src/Contracts/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 10 additions & 0 deletions src/DataProvider/DocumentDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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']);
Expand Down
27 changes: 9 additions & 18 deletions src/Task/AbstractDocumentTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use function array_diff_key;
use function array_flip;
use function array_keys;
use function array_merge;
use function count;
use function get_debug_type;
use function implode;
Expand Down Expand Up @@ -57,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));
Expand All @@ -76,11 +74,6 @@ final public function generate(mixed $input): Document
return $document;
}

public function getOptionalFields(): array
{
return [];
}

public function getGenerator(): Generator
{
return $this->generator;
Expand All @@ -100,19 +93,17 @@ protected function validate(array $data): void
{
$count = 0;
$invalid = [];
$fullData = array_merge($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]) {
if ('0' !== $matches[0][0] && isset($fullDump[$check])) {
$requiredCount++;
}
}
Expand All @@ -125,16 +116,16 @@ 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]);
}

if ($requiredCount !== $count) {
throw new InvalidArgumentException(sprintf('Missing required fields: "%s"', implode(', ', array_diff(array_keys($requiredDump), array_keys($dataDump)))));
$count += (int) isset($fullDump[$check]);
}

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($fullDump), array_keys($dataDump)))));
}
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/Task/DocumentTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
parent::__construct($em, $generator, $transformer);
}

public function getRequiredFields(): array
public function getTransformerFields(): array
{
return $this->getInput()->getSourceData();
}
Expand Down

0 comments on commit 4506e9a

Please sign in to comment.