1
+ <?php
2
+
3
+ namespace Styde ;
4
+
5
+ abstract class Model
6
+ {
7
+ protected $ attributes = [];
8
+
9
+ public function __construct (array $ attributes = [])
10
+ {
11
+ $ this ->fill ($ attributes );
12
+ }
13
+
14
+ public function fill (array $ attributes = [])
15
+ {
16
+ $ this ->attributes = $ attributes ;
17
+ }
18
+
19
+ public function getAttributes ()
20
+ {
21
+ return $ this ->attributes ;
22
+ }
23
+
24
+ public function getAttribute ($ name )
25
+ {
26
+ $ value = $ this ->getAttributeValue ($ name );
27
+
28
+ if ($ this ->hasGetMutator ($ name )) {
29
+ return $ this ->mutateAttribute ($ name , $ value );
30
+ }
31
+
32
+ return $ value ;
33
+ }
34
+
35
+ protected function hasGetMutator ($ name )
36
+ {
37
+ return method_exists ($ this , 'get ' .Str::studly ($ name ).'Attribute ' );
38
+ }
39
+
40
+ protected function mutateAttribute ($ name , $ value )
41
+ {
42
+ return $ this ->{'get ' .Str::studly ($ name ).'Attribute ' }($ value );
43
+ }
44
+
45
+ public function getAttributeValue ($ name )
46
+ {
47
+ if (array_key_exists ($ name , $ this ->attributes )) {
48
+ return $ this ->attributes [$ name ];
49
+ }
50
+ }
51
+
52
+ public function setAttribute ($ name , $ value )
53
+ {
54
+ $ this ->attributes [$ name ] = $ value ;
55
+ }
56
+
57
+ public function __set ($ name , $ value )
58
+ {
59
+ $ this ->setAttribute ($ name , $ value );
60
+ }
61
+
62
+ public function __get ($ name )
63
+ {
64
+ return $ this ->getAttribute ($ name );
65
+ }
66
+
67
+ public function __isset ($ name )
68
+ {
69
+ return isset ($ this ->attributes [$ name ]);
70
+ }
71
+
72
+ public function __unset ($ name )
73
+ {
74
+ unset ($ this ->attributes [$ name ]);
75
+ }
76
+ }
0 commit comments