File tree Expand file tree Collapse file tree 4 files changed +51
-1
lines changed
Expand file tree Collapse file tree 4 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -25,4 +25,5 @@ PHP Value object for storing valid data.
2525 - add interface ` ValueObjectInterface `
2626 - remove trait
2727 - min request PHP 7.4
28- - check null
28+ - check null
29+ - root element
Original file line number Diff line number Diff line change 1616
1717abstract class AbstractValueObject implements ValueObjectInterface
1818{
19+ /**
20+ * it is used if value object contains single value object
21+ */
22+ const ROOT = "root " ;
23+
1924 private array $ touched = [];
2025 private array $ ignoredAttrs = ["ignoredAttrs " , "touched " ];
2126
@@ -61,6 +66,9 @@ public function init($data): self
6166 foreach ($ data as $ name => $ item ) {
6267 $ setter = "set " . ucfirst ($ name );
6368 $ value = $ data [$ name ] ?? null ;
69+ if ($ name == self ::ROOT ) {
70+ $ value = $ data ;
71+ }
6472 $ this ->setTouched ($ name );
6573 try {
6674 if (method_exists ($ this , $ setter )) {
Original file line number Diff line number Diff line change 66use OGSoft \ValueObjects \Exceptions \ValidatorException ;
77use PHPUnit \Framework \TestCase ;
88use Tests \ValueObjects \CarValueObject ;
9+ use Tests \ValueObjects \TireValueObject ;
910
1011final class ValueObjectTest extends TestCase
1112{
@@ -64,5 +65,14 @@ public function testNull()
6465 $ car ->init ($ data );
6566 $ this ->assertTrue (true );
6667 }
68+
69+ public function testRoot ()
70+ {
71+ $ data = ["winter " => true ];
72+
73+ $ tire = new TireValueObject ();
74+ $ tire ->init ($ data );
75+ $ this ->assertEquals ($ data , $ tire ->toArray ());
76+ }
6777}
6878
Original file line number Diff line number Diff line change 1+ <?php
2+
3+
4+ namespace Tests \ValueObjects ;
5+
6+
7+ class RootValueObject
8+ {
9+ /**
10+ * @var TireValueObject
11+ */
12+ protected $ root ;
13+
14+ /**
15+ * @return TireValueObject
16+ */
17+ public function getRoot (): TireValueObject
18+ {
19+ return $ this ->root ;
20+ }
21+
22+ /**
23+ * @param TireValueObject $root
24+ * @return RootValueObject
25+ */
26+ public function setRoot (TireValueObject $ root ): RootValueObject
27+ {
28+ $ this ->root = $ root ;
29+ return $ this ;
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments