1+ <?php
2+
3+ namespace Nanvaie \DatabaseRepository \Models \Entity ;
4+
5+ use Illuminate \Contracts \Support \Arrayable ;
6+ use JsonSerializable ;
7+
8+ abstract class Entity implements JsonSerializable, Arrayable
9+ {
10+ // contain originals value of attributes
11+ private array $ originals = [];
12+
13+ /**
14+ * @return int
15+ */
16+ abstract public function getId ();
17+
18+ public function __construct ()
19+ {
20+
21+ }
22+
23+ public function __set ($ name , $ value )
24+ {
25+ if (property_exists ($ this , $ name )) {
26+ $ function = camel_case ('set_ ' . snake_case ($ name ));
27+ $ this ->$ function ($ value );
28+ }
29+ }
30+
31+ public function __get ($ name )
32+ {
33+ if (property_exists ($ this , $ name )) {
34+ $ function = camel_case ('get_ ' . snake_case ($ name ));
35+ return $ this ->$ function ();
36+ }
37+ }
38+
39+ public function __isset ($ name )
40+ {
41+ return property_exists ($ this , $ name );
42+ }
43+
44+ /**
45+ * Make all variables of the object as null
46+ * @return $this
47+ */
48+ public function clearVariables ()
49+ {
50+ $ attributes = get_object_vars ($ this );
51+ foreach ($ attributes as $ attributeName => $ attributeValue ) {
52+ $ this ->$ attributeName = null ;
53+ }
54+ return $ this ;
55+ }
56+
57+ /**
58+ * @return int
59+ */
60+ public function getPrimaryKey ()
61+ {
62+ return $ this ->getId ();
63+ }
64+
65+ /**
66+ * Fill the model
67+ */
68+ public function fill ()
69+ {
70+
71+ }
72+
73+ /**
74+ * get an Array of current Attributes value
75+ */
76+ public function toArray (): array
77+ {
78+ return get_object_vars ($ this );
79+ }
80+
81+ /**
82+ * store an array of attributes original value
83+ */
84+ public function storeOriginals ()
85+ {
86+ $ this ->originals = $ this ->toArray ();
87+ }
88+
89+ /**
90+ * get an Array of Changed Attributes
91+ * @return array
92+ */
93+ public function changedAttributesName ()
94+ {
95+ $ changedAttributes = [];
96+ $ attributes = $ this ->toArray ();
97+ foreach ($ attributes as $ key => $ value ) {
98+ if (isset ($ this ->originals [$ key ])) {
99+ if ($ value != $ this ->originals [$ key ] && !((is_array ($ this ->originals [$ key ]) || is_object ($ this ->originals [$ key ])))) {
100+ $ changedAttributes [] = $ key ;
101+ }
102+ }
103+ }
104+ return $ changedAttributes ;
105+ }
106+
107+ /**
108+ * get an Array of Changed Attributes with new values
109+ * @return array
110+ */
111+ public function getDirty ()
112+ {
113+ $ dirty = [];
114+ $ attributes = $ this ->toArray ();
115+
116+ foreach ($ this ->changedAttributesName () as $ key ) {
117+ $ dirty [$ key ] = $ attributes [$ key ];
118+ }
119+
120+ return $ dirty ;
121+ }
122+
123+ /**
124+ * get an Array of Changed Attributes with original values
125+ * @return array
126+ */
127+ public function getChanges ()
128+ {
129+ $ changes = [];
130+
131+ foreach ($ this ->changedAttributesName () as $ key ) {
132+ $ changes [$ key ] = $ this ->originals [$ key ];
133+ }
134+
135+ return $ changes ;
136+ }
137+
138+ /**
139+ * is any attribute changed?
140+ * @return bool
141+ */
142+ public function isDirty ()
143+ {
144+ if (count ($ this ->changedAttributesName ()) > 0 ) return true ;
145+
146+ return false ;
147+ }
148+
149+ public function jsonSerialize ()
150+ {
151+ return $ this ->toArray ();
152+ }
153+ }
0 commit comments