Skip to content

Commit

Permalink
Add Twig Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
awurth committed Mar 25, 2023
1 parent 623eb9d commit 26d54b3
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -31,7 +31,8 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.11",
"phpunit/phpunit": "^9.5",
"slim/psr7": "^1.5"
"slim/psr7": "^1.5",
"twig/twig": "^3.5"
},
"config": {
"sort-packages": true
Expand Down
89 changes: 89 additions & 0 deletions src/Twig/LegacyValidatorExtension.php
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Awurth Validator package.
*
* (c) Alexis Wurth <awurth.dev@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awurth\Validator\Twig;

use Awurth\Validator\StatefulValidatorInterface;
use Awurth\Validator\ValidationFailureInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Validator Twig Extension.
*
* @author Alexis Wurth <awurth.dev@gmail.com>
*/
final class LegacyValidatorExtension extends AbstractExtension
{
/**
* An array of names for Twig functions.
*
* @var string[]
*/
private array $functionNames;

/**
* @param string[] $functionNames An array of names for Twig functions
*/
public function __construct(private readonly StatefulValidatorInterface $validator, array $functionNames = [])
{
$this->functionNames['error'] = $functionNames['error'] ?? 'error';
$this->functionNames['errors'] = $functionNames['errors'] ?? 'errors';
$this->functionNames['has_error'] = $functionNames['has_error'] ?? 'has_error';
$this->functionNames['has_errors'] = $functionNames['has_errors'] ?? 'has_errors';
}

public function getFunctions(): array
{
return [
new TwigFunction($this->functionNames['error'], [$this, 'getError']),
new TwigFunction($this->functionNames['errors'], [$this, 'getErrors']),
new TwigFunction($this->functionNames['has_error'], [$this, 'hasError']),
new TwigFunction($this->functionNames['has_errors'], [$this, 'hasErrors']),
];
}

public function getError(string $key, int $index = 0): ?string
{
$failures = $this->validator->getFailures()->filter(static fn (ValidationFailureInterface $failure): bool => $failure->getProperty() === $key);
$failure = $failures->has($index) ? $failures->get(0) : null;

return $failure?->getMessage();
}

/**
* @return string[]
*/
public function getErrors(?string $key = null): array
{
$failures = null === $key
? $this->validator->getFailures()
: $this->validator->getFailures()->filter(static fn(ValidationFailureInterface $failure) => $failure->getProperty() === $key)
;

return \array_map(
static fn(ValidationFailureInterface $failure) => $failure->getMessage(),
(array)$failures,
);
}

public function hasError(string $key): bool
{
return $this->validator->getFailures()->find(static fn(ValidationFailureInterface $failure) => $failure->getProperty() === $key) !== null;
}

public function hasErrors(): bool
{
return $this->validator->getFailures()->count() !== 0;
}
}
77 changes: 77 additions & 0 deletions src/Twig/ValidatorExtension.php
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Awurth Validator package.
*
* (c) Alexis Wurth <awurth.dev@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awurth\Validator\Twig;

use Awurth\Validator\StatefulValidatorInterface;
use Awurth\Validator\ValidationFailureCollectionInterface;
use Awurth\Validator\ValidationFailureInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Validator Twig Extension.
*
* @author Alexis Wurth <awurth.dev@gmail.com>
*/
final class ValidatorExtension extends AbstractExtension
{
/**
* An array of names for Twig functions.
*
* @var string[]
*/
protected array $functionNames;

/**
* @param string[] $functionNames An array of names for Twig functions
*/
public function __construct(private readonly StatefulValidatorInterface $validator, array $functionNames = [])
{
$this->functionNames['error'] = $functionNames['error'] ?? 'error';
$this->functionNames['errors'] = $functionNames['errors'] ?? 'errors';
$this->functionNames['has_errors'] = $functionNames['has_errors'] ?? 'has_errors';
}

public function getFunctions(): array
{
return [
new TwigFunction($this->functionNames['error'], [$this, 'findFirst']),
new TwigFunction($this->functionNames['errors'], [$this, 'findErrors']),
new TwigFunction($this->functionNames['has_errors'], [$this, 'hasErrors']),
];
}

public function findFirst(?callable $callback = null): ?ValidationFailureInterface
{
if (null === $callback) {
return $this->validator->getFailures()->has(0) ? $this->validator->getFailures()->get(0) : null;
}

return $this->validator->getFailures()->find($callback);
}

public function findErrors(?callable $callback = null): ValidationFailureCollectionInterface
{
if (null === $callback) {
return $this->validator->getFailures();
}

return $this->validator->getFailures()->filter($callback);
}

public function hasErrors(): bool
{
return $this->validator->getFailures()->count() !== 0;
}
}

0 comments on commit 26d54b3

Please sign in to comment.