@@ 16-51 (lines=36) @@ | ||
13 | * The EntityExists validator will validate a certain entity was found. Validation will |
|
14 | * fail when the entity is not present |
|
15 | */ |
|
16 | class EntityExists extends AbstractEntityValidator |
|
17 | { |
|
18 | /** |
|
19 | * Error constants |
|
20 | */ |
|
21 | const ERROR_NO_OBJECT_FOUND = 'noObjectFound'; |
|
22 | ||
23 | /** |
|
24 | * @var array Message templates |
|
25 | */ |
|
26 | protected $messageTemplates = array( |
|
27 | self::ERROR_NO_OBJECT_FOUND => "No object matching '%value%' was found", |
|
28 | ); |
|
29 | ||
30 | /** |
|
31 | * Returns true when $this->entityService returns an entity |
|
32 | * when $this->method is called with the given criteria |
|
33 | * |
|
34 | * @param mixed $value |
|
35 | * @return boolean |
|
36 | */ |
|
37 | public function isValid($value) |
|
38 | { |
|
39 | $result = $this->fetchResult($value); |
|
40 | ||
41 | // The following code will check if we have a valid result. In case of an empty array (we did not get a valid |
|
42 | // match) the not-operator will result to true, causing the error to be set. In case of a null value (no object |
|
43 | // has been found), the not-operator will also result to true. |
|
44 | if (!$result) { |
|
45 | $this->error(self::ERROR_NO_OBJECT_FOUND, $value); |
|
46 | return false; |
|
47 | } |
|
48 | ||
49 | return true; |
|
50 | } |
|
51 | } |
|
52 |
@@ 16-50 (lines=35) @@ | ||
13 | * The EntityNotExists validator will validate a certain entity was found. |
|
14 | * Validation will fail when the entity is present |
|
15 | */ |
|
16 | class EntityNotExists extends AbstractEntityValidator |
|
17 | { |
|
18 | /** |
|
19 | * Error constants |
|
20 | */ |
|
21 | const ERROR_OBJECT_FOUND = 'objectFound'; |
|
22 | ||
23 | /** |
|
24 | * @var array Message templates |
|
25 | */ |
|
26 | protected $messageTemplates = array( |
|
27 | self::ERROR_OBJECT_FOUND => "Object matching '%value%' was found", |
|
28 | ); |
|
29 | ||
30 | /** |
|
31 | * Returns true when $this->entityService returns an empty result |
|
32 | * when $this->method is called with the given criteria |
|
33 | * |
|
34 | * @param mixed $value |
|
35 | * @return boolean |
|
36 | */ |
|
37 | public function isValid($value) |
|
38 | { |
|
39 | $result = $this->fetchResult($value); |
|
40 | ||
41 | // A filled array or an object both pass this test causing the error to be set. An empty array or a null value |
|
42 | // will cause the if check to return false. Meaning no error will be set. |
|
43 | if ($result) { |
|
44 | $this->error(self::ERROR_OBJECT_FOUND, $value); |
|
45 | return false; |
|
46 | } |
|
47 | ||
48 | return true; |
|
49 | } |
|
50 | } |
|
51 |