-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from KaririCode-Framework/develop
feat(processor-pipeline): implement attribute processing system
- Loading branch information
Showing
14 changed files
with
1,236 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KaririCode\ProcessorPipeline\Contract; | ||
|
||
use KaririCode\Contract\Processor\Attribute\ProcessableAttribute; | ||
|
||
interface ProcessorConfigBuilder | ||
{ | ||
/** | ||
* Constrói a configuração dos processadores a partir de um atributo processável. | ||
* | ||
* @param ProcessableAttribute $attribute o atributo que fornece os processadores | ||
* | ||
* @return array a configuração dos processadores | ||
*/ | ||
public function build(ProcessableAttribute $attribute): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KaririCode\ProcessorPipeline\Handler; | ||
|
||
use KaririCode\Contract\Processor\Attribute\CustomizableMessageAttribute; | ||
use KaririCode\Contract\Processor\Attribute\ProcessableAttribute; | ||
use KaririCode\Contract\Processor\ProcessorBuilder; | ||
use KaririCode\Contract\Processor\ProcessorValidator as ProcessorProcessorContract; | ||
use KaririCode\ProcessorPipeline\Contract\ProcessorConfigBuilder as ProcessorConfigBuilderContract; | ||
use KaririCode\ProcessorPipeline\Processor\ProcessorConfigBuilder; | ||
use KaririCode\ProcessorPipeline\Processor\ProcessorValidator; | ||
use KaririCode\PropertyInspector\Contract\PropertyAttributeHandler; | ||
use KaririCode\PropertyInspector\Contract\PropertyChangeApplier; | ||
use KaririCode\PropertyInspector\Utility\PropertyAccessor; | ||
|
||
class AttributeHandler implements PropertyAttributeHandler, PropertyChangeApplier | ||
{ | ||
private array $processedPropertyValues = []; | ||
private array $processingResultErrors = []; | ||
private array $processingResultMessages = []; | ||
private array $processorCache = []; | ||
|
||
public function __construct( | ||
private readonly string $processorType, | ||
private readonly ProcessorBuilder $builder, | ||
private readonly ProcessorProcessorContract $validator = new ProcessorValidator(), | ||
private readonly ProcessorConfigBuilderContract $configBuilder = new ProcessorConfigBuilder() | ||
) { | ||
} | ||
|
||
public function handleAttribute(string $propertyName, object $attribute, mixed $value): mixed | ||
{ | ||
if (!$attribute instanceof ProcessableAttribute) { | ||
return null; | ||
} | ||
|
||
try { | ||
return $this->processAttribute($propertyName, $attribute, $value); | ||
} catch (\Exception $e) { | ||
$this->processingResultErrors[$propertyName][] = $e->getMessage(); | ||
|
||
return $value; | ||
} | ||
} | ||
|
||
private function processAttribute(string $propertyName, ProcessableAttribute $attribute, mixed $value): mixed | ||
{ | ||
$config = $this->configBuilder->build($attribute); | ||
$messages = []; | ||
|
||
if ($attribute instanceof CustomizableMessageAttribute) { | ||
foreach ($config as $processorName => &$processorConfig) { | ||
if ($message = $attribute->getMessage($processorName)) { | ||
$processorConfig['customMessage'] = $message; | ||
$messages[$processorName] = $message; | ||
} | ||
} | ||
} | ||
|
||
$processedValue = $this->processValue($value, $config); | ||
|
||
if ($errors = $this->validateProcessors($config, $messages)) { | ||
$this->processingResultErrors[$propertyName] = $errors; | ||
} | ||
|
||
$this->processedPropertyValues[$propertyName] = [ | ||
'value' => $processedValue, | ||
'messages' => $messages, | ||
]; | ||
|
||
$this->processingResultMessages[$propertyName] = $messages; | ||
|
||
return $processedValue; | ||
} | ||
|
||
private function validateProcessors(array $processorsConfig, array $messages): array | ||
{ | ||
$errors = []; | ||
foreach ($processorsConfig as $processorName => $config) { | ||
// Simplify cache key to processor name | ||
if (!isset($this->processorCache[$processorName])) { | ||
$this->processorCache[$processorName] = $this->builder->build( | ||
$this->processorType, | ||
$processorName, | ||
$config | ||
); | ||
} | ||
|
||
$processor = $this->processorCache[$processorName]; | ||
|
||
if ($error = $this->validator->validate($processor, $processorName, $messages)) { | ||
$errors[$processorName] = $error; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
private function processValue(mixed $value, array $config): mixed | ||
{ | ||
return $this->builder | ||
->buildPipeline($this->processorType, $config) | ||
->process($value); | ||
} | ||
|
||
public function applyChanges(object $entity): void | ||
{ | ||
foreach ($this->processedPropertyValues as $propertyName => $data) { | ||
(new PropertyAccessor($entity, $propertyName))->setValue($data['value']); | ||
} | ||
} | ||
|
||
public function getProcessedPropertyValues(): array | ||
{ | ||
return $this->processedPropertyValues; | ||
} | ||
|
||
public function getProcessingResultErrors(): array | ||
{ | ||
return $this->processingResultErrors; | ||
} | ||
|
||
public function getProcessingResultMessages(): array | ||
{ | ||
return $this->processingResultMessages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KaririCode\ProcessorPipeline\Processor; | ||
|
||
use KaririCode\Contract\Processor\Attribute\ProcessableAttribute; | ||
use KaririCode\ProcessorPipeline\Contract\ProcessorConfigBuilder as ProcessorConfigBuilderContract; | ||
|
||
readonly class ProcessorConfigBuilder implements ProcessorConfigBuilderContract | ||
{ | ||
public function build(ProcessableAttribute $attribute): array | ||
{ | ||
$processors = $attribute->getProcessors(); | ||
$processorsConfig = []; | ||
|
||
foreach ($processors as $key => $processor) { | ||
if ($this->isSimpleProcessor($processor)) { | ||
$processorsConfig[$processor] = $this->getDefaultProcessorConfig(); | ||
} elseif ($this->isConfigurableProcessor($processor)) { | ||
$processorName = $this->determineProcessorName($key, $processor); | ||
$processorsConfig[$processorName] = $this->getProcessorConfig($processor); | ||
} | ||
} | ||
|
||
return $processorsConfig; | ||
} | ||
|
||
private function isSimpleProcessor(mixed $processor): bool | ||
{ | ||
return is_string($processor); | ||
} | ||
|
||
private function isConfigurableProcessor(mixed $processor): bool | ||
{ | ||
return is_array($processor); | ||
} | ||
|
||
private function getDefaultProcessorConfig(): array | ||
{ | ||
return []; | ||
} | ||
|
||
private function determineProcessorName(string|int $key, array $processor): string | ||
{ | ||
$nameNormalizer = new ProcessorNameNormalizer(); | ||
|
||
return $nameNormalizer->normalize($key, $processor); | ||
} | ||
|
||
private function getProcessorConfig(array $processor): array | ||
{ | ||
return $processor; | ||
} | ||
} |
Oops, something went wrong.