1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Validator; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Validator\ValidatorInterface; |
17
|
|
|
use Silverback\ApiComponentsBundle\Publishable\PublishableHelper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Builds and add validation group for published resources. |
21
|
|
|
* |
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class PublishableValidator implements ValidatorInterface |
25
|
|
|
{ |
26
|
|
|
public const PUBLISHED_KEY = 'published'; |
27
|
|
|
|
28
|
|
|
private ValidatorInterface $decorated; |
29
|
|
|
private PublishableHelper $publishableHelper; |
30
|
|
|
|
31
|
|
|
public function __construct(ValidatorInterface $decorated, PublishableHelper $publishableHelper) |
32
|
|
|
{ |
33
|
|
|
$this->decorated = $decorated; |
34
|
|
|
$this->publishableHelper = $publishableHelper; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function validate($data, array $context = []): void |
41
|
|
|
{ |
42
|
|
|
if ( |
43
|
|
|
\is_object($data) && |
44
|
|
|
$this->publishableHelper->getAnnotationReader()->isConfigured($data) && |
45
|
|
|
($this->publishableHelper->hasPublicationDate($data) || isset($context[self::PUBLISHED_KEY])) |
46
|
|
|
) { |
47
|
|
|
$groups = [(new \ReflectionClass(\get_class($data)))->getShortName() . ':published']; |
48
|
|
|
if (!empty($this->publishableHelper->getAnnotationReader()->getConfiguration($data)->validationGroups)) { |
49
|
|
|
$groups = $this->publishableHelper->getAnnotationReader()->getConfiguration($data)->validationGroups; |
50
|
|
|
} |
51
|
|
|
$context['groups'] = array_merge($context['groups'] ?? ['Default'], $groups); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->decorated->validate($data, $context); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|