|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ExtReferenceValidator class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\RestBundle\Validator\Constraints\ExtReference; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\DocumentBundle\Entity\ExtReference as ExtRef; |
|
9
|
|
|
use Symfony\Component\Validator\Constraint; |
|
10
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
11
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
12
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Validator for the extref type |
|
16
|
|
|
* |
|
17
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
18
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
19
|
|
|
* @link http://swisscom.ch |
|
20
|
|
|
*/ |
|
21
|
|
|
class ExtReferenceValidator extends ConstraintValidator |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** @var DocumentManager $documentManager */ |
|
25
|
|
|
private $documentManager; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Boolean */ |
|
28
|
|
|
private $validateId; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* ExtReferenceValidator constructor. |
|
32
|
|
|
* @param DocumentManager $documentManager DB manager |
|
33
|
|
|
* @param boolean $validateId Validate ID against DB or not. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(DocumentManager $documentManager, $validateId) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->documentManager = $documentManager; |
|
38
|
|
|
$this->validateId = $validateId; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Checks if the passed value is valid. |
|
43
|
|
|
* |
|
44
|
|
|
* @param mixed $value The value that should be validated |
|
45
|
|
|
* @param Constraint $constraint The constraint for the validation |
|
46
|
|
|
* @return void |
|
47
|
|
|
* @throws UnexpectedTypeException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function validate($value, Constraint $constraint) |
|
50
|
|
|
{ |
|
51
|
|
|
if (!$constraint instanceof ExtReference) { |
|
52
|
|
|
throw new UnexpectedTypeException($constraint, ExtReference::class); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($value === null) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!$value instanceof ExtRef) { |
|
60
|
|
|
throw new UnexpectedTypeException($value, ExtRef::class); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!empty($constraint->collections) && |
|
64
|
|
|
!in_array('*', $constraint->collections, true) && |
|
65
|
|
|
!in_array($value->getRef(), $constraint->collections, true) |
|
66
|
|
|
) { |
|
67
|
|
|
$this->context->addViolation($constraint->message, ['%collection%' => $value->getRef()]); |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Does ID exists in DB. |
|
72
|
|
|
if ($this->validateId) { |
|
73
|
|
|
$db = $this->documentManager->getConnection()->selectDatabase('db'); |
|
74
|
|
|
$collection = $db->selectCollection($value->getRef()); |
|
75
|
|
|
$document = $collection->findOne(['_id' => $value->getId()]); |
|
76
|
|
|
if (!$document) { |
|
77
|
|
|
$msgArg = ['%collection%' => $value->getRef().':invalid:'.$value->getId()]; |
|
78
|
|
|
$this->context->addViolation($constraint->message, $msgArg); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|