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 #13 from zfegg/develop
Add transformers keyword.
- Loading branch information
Showing
26 changed files
with
782 additions
and
36 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
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
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Zfegg\ContentValidation\Factory; | ||
|
||
use Laminas\Filter\FilterPluginManager; | ||
use Psr\Container\ContainerInterface; | ||
use Zfegg\ContentValidation\Opis\Resolver\LaminasFilterResolver; | ||
use Zfegg\ContentValidation\Opis\Resolver\TransformerResolver; | ||
|
||
class TransformerResolverFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): TransformerResolver | ||
{ | ||
$resolver = new TransformerResolver(); | ||
|
||
if ($container->has(FilterPluginManager::class)) { | ||
$resolver->registerNS('laminas', new LaminasFilterResolver($container->get(FilterPluginManager::class))); | ||
} | ||
|
||
return $resolver; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Zfegg\ContentValidation\Opis; | ||
|
||
class Helper | ||
{ | ||
const JSON_ALL_TYPES = ['string', 'integer', 'number', 'boolean', 'null', 'object', '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
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Zfegg\ContentValidation\Opis\Keyword; | ||
|
||
use Opis\JsonSchema\ValidationContext; | ||
|
||
trait SetValueTrait | ||
{ | ||
|
||
public function setValue(ValidationContext $context, callable $transform): void | ||
{ | ||
$path = $context->currentDataPath(); | ||
$data = $context->rootData(); | ||
|
||
$target = $data; | ||
foreach ($path as $key) { | ||
if (is_object($target)) { | ||
$target = &$target->{$key}; | ||
} else { | ||
$target = &$target[$key]; | ||
} | ||
} | ||
|
||
$target = $transform($target); | ||
|
||
$resetPath = []; | ||
foreach (array_reverse($path) as $key) { | ||
array_unshift($resetPath, $key); | ||
$context->popDataPath(); | ||
if ($context->currentDataType() != 'array') { | ||
break; | ||
} | ||
} | ||
|
||
foreach ($resetPath as $key) { | ||
$context->pushDataPath($key); | ||
} | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Zfegg\ContentValidation\Opis\Keyword; | ||
|
||
use Opis\JsonSchema\Errors\ValidationError; | ||
use Opis\JsonSchema\Exceptions\UnresolvedFilterException; | ||
use Opis\JsonSchema\Keyword; | ||
use Opis\JsonSchema\Keywords\ErrorTrait; | ||
use Opis\JsonSchema\Schema; | ||
use Opis\JsonSchema\ValidationContext; | ||
use Zfegg\ContentValidation\Opis\Transformer\TransformerInterface; | ||
|
||
class TransformerKeyword implements Keyword | ||
{ | ||
use ErrorTrait; | ||
use SetValueTrait; | ||
|
||
/** @var array|object[] */ | ||
protected array $filters; | ||
|
||
/** | ||
* @param object[] $filters | ||
*/ | ||
public function __construct(array $filters) | ||
{ | ||
$this->filters = $filters; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function validate(ValidationContext $context, Schema $schema): ?ValidationError | ||
{ | ||
$type = $context->currentDataType(); | ||
$data = $context->currentData(); | ||
|
||
foreach ($this->filters as $filter) { | ||
if (! isset($filter->types[$type])) { | ||
throw new UnresolvedFilterException($filter->name, $type, $schema, $context); | ||
} | ||
|
||
$func = $filter->types[$type]; | ||
|
||
if ($filter->args) { | ||
$args = (array)$filter->args->resolve($context->rootData(), $context->currentDataPath()); | ||
$args += $context->globals(); | ||
} else { | ||
$args = $context->globals(); | ||
} | ||
|
||
if ($func instanceof TransformerInterface) { | ||
$data = $func->transform($data, $context, $schema->info(), $args); | ||
} else { | ||
$data = $func($data, ...$args); | ||
} | ||
} | ||
|
||
$this->setValue($context, fn() => $data); | ||
|
||
return null; | ||
} | ||
} |
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
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Zfegg\ContentValidation\Opis\Resolver; | ||
|
||
use Laminas\Filter\FilterPluginManager; | ||
use Zfegg\ContentValidation\Opis\Transformer\LaminasTransformer; | ||
use Zfegg\ContentValidation\Opis\Transformer\TransformerInterface; | ||
|
||
class LaminasFilterResolver implements ResolverInterface | ||
{ | ||
private FilterPluginManager $filterManager; | ||
|
||
public function __construct(FilterPluginManager $filterManager) | ||
{ | ||
$this->filterManager = $filterManager; | ||
} | ||
|
||
public function resolve(string $name, string $type): TransformerInterface | ||
{ | ||
return new LaminasTransformer($this->filterManager, $name); | ||
} | ||
|
||
/** | ||
* @return TransformerInterface[]|null | ||
*/ | ||
public function resolveAll(string $name): ?array | ||
{ | ||
$types = ['string', 'number', 'boolean', 'integer']; | ||
return array_fill_keys( | ||
$types, | ||
new LaminasTransformer($this->filterManager, $name) | ||
); | ||
} | ||
} |
Oops, something went wrong.