-
Notifications
You must be signed in to change notification settings - Fork 512
/
ValidateProperties.php
79 lines (69 loc) · 2.49 KB
/
ValidateProperties.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Akeneo\Pim\Enrichment\Component\Product\Connector\UseCase\Validator;
use Akeneo\Pim\Enrichment\Component\Product\Query\Filter\Operators;
use Akeneo\Tool\Component\Api\Exception\InvalidQueryException;
use Akeneo\Tool\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface;
/**
* @author Pierre Allard <pierre.allard@akeneo.com>
* @author Alexandre Hocquard <alexandre.hocquard@akeneo.com>
* @author Mathias Métayer <mathias.metayer@akeneo.com>
* @copyright 2019 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
final class ValidateProperties
{
private static $productFields = [
'family',
'categories',
'completeness',
'identifier',
'created',
'updated',
'enabled',
'groups',
'parent',
];
/** @var IdentifiableObjectRepositoryInterface */
private $attributeRepository;
public function __construct(
IdentifiableObjectRepositoryInterface $attributeRepository
) {
$this->attributeRepository = $attributeRepository;
}
/**
* @throws InvalidQueryException
*/
public function validate(array $search): void
{
foreach ($search as $propertyCode => $filters) {
foreach ($filters as $filter) {
if (!(
$this->isProductField($propertyCode) ||
$this->isExistingAttribute($propertyCode) ||
$this->hasWrongOperatorForParentProperty($propertyCode, $filter['operator'])
)) {
throw new InvalidQueryException(
sprintf(
'Filter on property "%s" is not supported or does not support operator "%s"',
$propertyCode,
$filter['operator']
)
);
}
}
}
}
private function isProductField(string $propertyCode): bool
{
return in_array($propertyCode, self::$productFields);
}
private function isExistingAttribute(string $propertyCode): bool
{
return null !== $this->attributeRepository->findOneByIdentifier($propertyCode);
}
private function hasWrongOperatorForParentProperty(string $propertyCode, string $operator): bool
{
return ($propertyCode === 'parent' && $operator !== Operators::EQUALS);
}
}