1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasEntity\Validator; |
6
|
|
|
|
7
|
|
|
use Arp\Entity\EntityInterface; |
8
|
|
|
use Arp\LaminasEntity\Constant\IsEntityError; |
9
|
|
|
use Laminas\Validator\Exception\RuntimeException; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Attempt to validate that an entity exists that matches the provided criteria. |
13
|
|
|
* |
14
|
|
|
* @author Alex Patterson <[email protected]> |
15
|
|
|
* @package Arp\LaminasEntity\Validator |
16
|
|
|
*/ |
17
|
|
|
class IsEntityValidator extends AbstractValidator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $messageTemplates = [ |
|
|
|
|
23
|
|
|
IsEntityError::NOT_FOUND |
24
|
|
|
=> 'An entity of type \'%entityName%\' could not be found matching for value \'%value%\'.', |
25
|
|
|
IsEntityError::INVALID |
26
|
|
|
=> 'The entity found using the provided criteria is not of type \'%entityName%\'.', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $abstractOptions = [ |
33
|
|
|
'messageVariables' => [ |
34
|
|
|
'entityName' => 'entityName', // Placeholder variables that can be nested in the validation error messages. |
35
|
|
|
], |
36
|
|
|
'messages' => [], // Array of validation failure messages |
37
|
|
|
'messageTemplates' => [], // Array of validation failure message templates |
38
|
|
|
|
39
|
|
|
'translator' => null, // Translation object to used -> Translator\TranslatorInterface |
40
|
|
|
'translatorTextDomain' => null, // Translation text domain |
41
|
|
|
'translatorEnabled' => true, // Is translation enabled? |
42
|
|
|
'valueObscured' => false, // Flag indicating whether or not value should be obfuscated |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Validate the entity exists matching the provided $value/$context |
47
|
|
|
* |
48
|
|
|
* @param mixed $value The value that should be validated. |
49
|
|
|
* @param array|null $context Optional additional data to use to perform the validation. |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
* |
53
|
|
|
* @throws RuntimeException If the validation cannot be performed |
54
|
|
|
*/ |
55
|
|
|
public function isValid($value, array $context = []): bool |
56
|
|
|
{ |
57
|
|
|
$criteria = $this->getFilterCriteria($value, $context); |
58
|
|
|
$entityName = $this->repository->getClassName(); |
59
|
|
|
|
60
|
|
|
if (empty($criteria)) { |
61
|
|
|
throw new RuntimeException( |
62
|
|
|
sprintf( |
63
|
|
|
'Unable to perform validation for entity \'%s\' in \'%s\' with an empty criteria', |
64
|
|
|
$entityName, |
65
|
|
|
static::class |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$entity = $this->repository->findOneBy($criteria); |
72
|
|
|
|
73
|
|
|
if (null === $entity) { |
74
|
|
|
$this->error(IsEntityError::NOT_FOUND, $criteria); |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (!$entity instanceof EntityInterface || !$entity instanceof $entityName) { |
79
|
|
|
$this->error(IsEntityError::INVALID, $criteria); |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return true; |
84
|
|
|
} catch (\Throwable $e) { |
85
|
|
|
throw new RuntimeException( |
86
|
|
|
sprintf( |
87
|
|
|
'Failed to perform the validation for entity of type \'%s\': %s', |
88
|
|
|
$entityName, |
89
|
|
|
$e->getMessage() |
90
|
|
|
), |
91
|
|
|
$e->getCode(), |
92
|
|
|
$e |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths