| 1 | <?php |
||
| 11 | trait FreezableValueObject { |
||
| 12 | |||
| 13 | private bool $isFrozen = false; |
||
|
|
|||
| 14 | |||
| 15 | 4 | public function freeze(): self { |
|
| 16 | 4 | $this->isFrozen = true; |
|
| 17 | 4 | return $this; |
|
| 18 | } |
||
| 19 | |||
| 20 | 5 | protected function assertIsWritable(): self { |
|
| 21 | 5 | if ( $this->isFrozen ) { |
|
| 22 | 2 | throw new \RuntimeException( 'Cannot write to a frozen object!' ); |
|
| 23 | } |
||
| 24 | 4 | return $this; |
|
| 25 | } |
||
| 26 | |||
| 27 | 2 | public function assertNoNullFields(): self { |
|
| 28 | 2 | foreach ( get_object_vars( $this ) as $fieldName => $fieldValue ) { |
|
| 29 | 2 | if ( $fieldValue === null ) { |
|
| 30 | 1 | throw new \RuntimeException( "Field '$fieldName' cannot be null" ); |
|
| 31 | } |
||
| 32 | } |
||
| 33 | 1 | return $this; |
|
| 34 | } |
||
| 35 | |||
| 36 | } |
||
| 37 |