These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\ODM\MongoDB\Event; |
||
6 | |||
7 | use Doctrine\ODM\MongoDB\DocumentManager; |
||
8 | use InvalidArgumentException; |
||
9 | use const E_USER_DEPRECATED; |
||
10 | use function get_class; |
||
11 | use function sprintf; |
||
12 | use function trigger_error; |
||
13 | |||
14 | /** |
||
15 | * Class that holds event arguments for a preUpdate event. |
||
16 | * |
||
17 | * @final |
||
18 | */ |
||
19 | class PreUpdateEventArgs extends LifecycleEventArgs |
||
20 | { |
||
21 | /** @var array */ |
||
22 | private $documentChangeSet; |
||
23 | |||
24 | 236 | public function __construct(object $document, DocumentManager $dm, array $changeSet) |
|
25 | { |
||
26 | 236 | if (self::class !== static::class) { |
|
27 | @trigger_error(sprintf('The class "%s" extends "%s" which will be final in MongoDB ODM 2.0.', static::class, self::class), E_USER_DEPRECATED); |
||
0 ignored issues
–
show
|
|||
28 | } |
||
29 | 236 | parent::__construct($document, $dm); |
|
30 | 236 | $this->documentChangeSet = $changeSet; |
|
31 | 236 | } |
|
32 | |||
33 | public function getDocumentChangeSet() : array |
||
34 | { |
||
35 | return $this->documentChangeSet; |
||
36 | } |
||
37 | |||
38 | 1 | public function hasChangedField(string $field) : bool |
|
39 | { |
||
40 | 1 | return isset($this->documentChangeSet[$field]); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Gets the old value of the changeset of the changed field. |
||
45 | * |
||
46 | * @return mixed |
||
47 | */ |
||
48 | public function getOldValue(string $field) |
||
49 | { |
||
50 | $this->assertValidField($field); |
||
51 | |||
52 | return $this->documentChangeSet[$field][0]; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Gets the new value of the changeset of the changed field. |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | public function getNewValue(string $field) |
||
61 | { |
||
62 | $this->assertValidField($field); |
||
63 | |||
64 | return $this->documentChangeSet[$field][1]; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Sets the new value of this field. |
||
69 | * |
||
70 | * @param mixed $value |
||
71 | */ |
||
72 | 1 | public function setNewValue(string $field, $value) : void |
|
73 | { |
||
74 | 1 | $this->assertValidField($field); |
|
75 | |||
76 | 1 | $this->documentChangeSet[$field][1] = $value; |
|
77 | 1 | $this->getDocumentManager()->getUnitOfWork()->setDocumentChangeSet($this->getDocument(), $this->documentChangeSet); |
|
78 | 1 | } |
|
79 | |||
80 | /** |
||
81 | * Asserts the field exists in changeset. |
||
82 | * |
||
83 | * @throws InvalidArgumentException If the field has no changeset. |
||
84 | */ |
||
85 | 1 | private function assertValidField(string $field) : void |
|
86 | { |
||
87 | 1 | if (! isset($this->documentChangeSet[$field])) { |
|
88 | throw new InvalidArgumentException(sprintf( |
||
89 | 'Field "%s" is not a valid field of the document "%s" in PreUpdateEventArgs.', |
||
90 | $field, |
||
91 | get_class($this->getDocument()) |
||
92 | )); |
||
93 | } |
||
94 | 1 | } |
|
95 | } |
||
96 |
If you suppress an error, we recommend checking for the error condition explicitly: