44
55use OG \ValueObjects \Exceptions \ValidatorException ;
66use OG \ValueObjects \Interfaces \GlobalValidatorInterface ;
7+ use OG \ValueObjects \Interfaces \ValueObjectInterface ;
78use Throwable ;
89
9- abstract class AbstractValueObject
10+ abstract class AbstractValueObject implements ValueObjectInterface
1011{
12+ private $ touched = [];
13+ private $ ignoredAttrs = ["ignoredAttrs " , "touched " ];
14+
15+ public function isIgnoredAttr (string $ attrName ): bool
16+ {
17+ return in_array ($ attrName , $ this ->ignoredAttrs );
18+ }
1119
1220 /**
13- * setter for attribute of ValueObject
14- *
15- * @param string $attrName
16- * @param string $attrSetter
17- * @param $attrValue
18- * @param object $valueObject
19- * @return mixed
20- * @throws ValidatorException
21+ * @inheritDoc
2122 */
22- abstract public function setValue (string $ attrName , string $ attrSetter , $ attrValue , object $ valueObject );
23+ public function setTouched (string $ attrName ): void
24+ {
25+ $ this ->touched [$ attrName ] = true ;
26+ }
2327
2428 /**
25- * @param Throwable $throwable
26- * @param string $attrName
27- * @param string $attrSetter
28- * @param $attrValue
29- * @param object $valueObject
30- * @return mixed
29+ * @inheritDoc
3130 */
32- abstract public function getAttributeErrorMessage (Throwable $ throwable , string $ attrName , string $ attrSetter , $ attrValue , object $ valueObject ): string ;
31+ public function isTouched (string $ attrName ): bool
32+ {
33+ return $ this ->touched [$ attrName ];
34+ }
3335
3436 /**
35- * @param object|array $data
36- * @return void
37- * @throws ValidatorException
37+ * @inheritDoc
38+ */
39+ public function getTouchedAll (): array
40+ {
41+ return $ this ->touched ;
42+ }
43+
44+ /**
45+ * @inheritDoc
3846 */
3947 public function init ($ data ): void
4048 {
4149 if (is_object ($ data )) {
4250 $ data = get_object_vars ($ data );
4351 }
52+ // foreach get data
4453 foreach ($ data as $ name => $ item ) {
4554 $ setter = "set " . ucfirst ($ name );
4655 $ value = $ data [$ name ] ?? null ;
56+ $ this ->setTouched ($ name );
4757 try {
48- $ this ->setValue ($ name , $ setter , $ value , $ this );
58+ if (method_exists ($ this , $ setter )) {
59+ $ this ->setValue ($ name , $ setter , $ value , $ this );
60+ } else {
61+ // attribute setter is missing
62+ $ this ->handleUndefinedAttribute ($ name , $ setter , $ value , $ this );
63+ }
4964 } catch (Throwable $ e ) {
5065 $ msg = $ this ->getAttributeErrorMessage ($ e , $ name , $ setter , $ value , $ this );
5166 $ code = $ this ->getAttributeErrorCode ($ e , $ name , $ setter , $ value , $ this );
@@ -59,28 +74,24 @@ public function init($data): void
5974 }
6075
6176 /**
62- * @param Throwable $throwable
63- * @param string $attrName
64- * @param string $attrSetter
65- * @param $attrValue
66- * @param object $valueObject
67- * @return int
77+ * @inheritDoc
6878 */
6979 public function getAttributeErrorCode (Throwable $ throwable , string $ attrName , string $ attrSetter , $ attrValue , object $ valueObject ): int
7080 {
7181 return 0 ;
7282 }
7383
7484 /**
75- * Transform value object to array
76- *
77- * @return array
85+ * @inheritDoc
7886 */
7987 public function toArray (): array
8088 {
8189 $ arr = get_object_vars ($ this );
8290 $ out = [];
8391 foreach ($ arr as $ name => $ item ) {
92+ if ($ this ->isIgnoredAttr ($ name )) {
93+ continue ;
94+ }
8495 // ValueObject element
8596 if ($ item instanceof AbstractValueObject) {
8697 $ out [$ name ] = $ item ->toArray ();
0 commit comments