1 | <?php |
||
20 | class ReadOnlyValidator extends ConstraintValidator |
||
21 | { |
||
22 | private $dm; |
||
23 | |||
24 | /** |
||
25 | * Creates a new ReadOnlyValidator instance |
||
26 | * |
||
27 | * @param DocumentManager $dm Document manager |
||
28 | */ |
||
29 | 1 | public function __construct(DocumentManager $dm) |
|
30 | { |
||
31 | 1 | $this->dm = $dm; |
|
32 | 1 | } |
|
33 | |||
34 | /** |
||
35 | * Checks read only |
||
36 | * |
||
37 | * @param string $value Input value |
||
38 | * @param Constraint $constraint Constraint instance |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | 1 | public function validate($value, Constraint $constraint) |
|
43 | { |
||
44 | 1 | $submittedData = $this->context->getRoot()->getData(); |
|
45 | |||
46 | // if the structure is nested it can't access the id via getObject() |
||
47 | 1 | $recordClass = get_class($submittedData); |
|
48 | 1 | $recordId = $this->context->getRoot()->getData()->getId(); |
|
49 | |||
50 | 1 | $record = $this->dm->find($recordClass, $recordId); |
|
51 | |||
52 | 1 | if ($record) { |
|
53 | $storedValue = $this->getStoredValueByPath($this->context->getPropertyPath(), $record); |
||
54 | |||
55 | if ($storedValue instanceof \DateTime) { |
||
56 | $isEqual = ($storedValue == $value); |
||
57 | } else { |
||
58 | $isEqual = ($value === $storedValue); |
||
59 | } |
||
60 | |||
61 | if (!$isEqual) { |
||
62 | $this->context->addViolation( |
||
63 | $constraint->message, |
||
64 | array('%string%' => $this->context->getPropertyPath()) |
||
65 | ); |
||
66 | } |
||
67 | } |
||
68 | 1 | } |
|
69 | |||
70 | /** |
||
71 | * Gets the stored value from a path |
||
72 | * |
||
73 | * @param string $path Value path |
||
74 | * @param TranslatableDocumentInterface $record Record |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function getStoredValueByPath($path, $record) |
||
93 | } |
||
94 |