1: | <?php |
2: | namespace Hyperwallet\Model; |
3: | |
4: | /** |
5: | * This is the base model for handling updates and insert field calculations |
6: | * |
7: | * @package Hyperwallet\Model |
8: | */ |
9: | class BaseModel { |
10: | |
11: | /** |
12: | * @internal |
13: | * @access private |
14: | * |
15: | * The model properties |
16: | * |
17: | * @var array |
18: | */ |
19: | private $properties; |
20: | |
21: | /** |
22: | * @internal |
23: | * |
24: | * The modified property key names |
25: | * |
26: | * @var string[] |
27: | */ |
28: | private $updatedProperties; |
29: | |
30: | /** |
31: | * @internal |
32: | * |
33: | * The read only property key names |
34: | * |
35: | * @var string[] |
36: | */ |
37: | private $readOnlyProperties; |
38: | |
39: | /** |
40: | * Creates a instance of BaseModel |
41: | * |
42: | * @param string[] $readOnlyProperties The read only key names |
43: | * @param array $properties The default properties |
44: | */ |
45: | public function __construct(array $readOnlyProperties = array(), array $properties = array()) { |
46: | $this->readOnlyProperties = $readOnlyProperties; |
47: | $this->properties = $properties; |
48: | $this->updatedProperties = array(); |
49: | } |
50: | |
51: | /** |
52: | * @internal |
53: | * |
54: | * Magic get method |
55: | * |
56: | * @param string $key |
57: | * @return mixed |
58: | */ |
59: | public function __get($key) { |
60: | if ($this->__isset($key)) { |
61: | return $this->properties[$key]; |
62: | } |
63: | return null; |
64: | } |
65: | |
66: | /** |
67: | * @internal |
68: | * |
69: | * Magic set method |
70: | * |
71: | * @param string $key |
72: | * @param mixed $value |
73: | */ |
74: | public function __set($key, $value) { |
75: | if (!in_array($key, $this->updatedProperties)) { |
76: | $this->updatedProperties[] = $key; |
77: | } |
78: | |
79: | $this->properties[$key] = $value; |
80: | } |
81: | |
82: | /** |
83: | * @internal |
84: | * |
85: | * Magic isset method |
86: | * |
87: | * @param string $key |
88: | * @return bool |
89: | * |
90: | * @access private |
91: | */ |
92: | public function __isset($key) { |
93: | return isset($this->properties[$key]); |
94: | } |
95: | |
96: | /** |
97: | * @internal |
98: | * |
99: | * Magic unset method |
100: | * |
101: | * @param string $key |
102: | */ |
103: | public function __unset($key) { |
104: | unset($this->updatedProperties[array_search($key, $this->updatedProperties)]); |
105: | unset($this->properties[$key]); |
106: | } |
107: | |
108: | /** |
109: | * @internal |
110: | * |
111: | * Get the model properties |
112: | * |
113: | * @return array |
114: | */ |
115: | public function getProperties() { |
116: | return $this->properties; |
117: | } |
118: | |
119: | /** |
120: | * @internal |
121: | * |
122: | * Get all non read only model properties |
123: | * |
124: | * @return array |
125: | */ |
126: | public function getPropertiesForCreate() { |
127: | return array_diff_key($this->properties, array_flip($this->readOnlyProperties)); |
128: | } |
129: | |
130: | /** |
131: | * @internal |
132: | * |
133: | * Get all updated model properties that are not read only |
134: | * |
135: | * @return array |
136: | */ |
137: | public function getPropertiesForUpdate() { |
138: | return array_intersect_key($this->getPropertiesForCreate(), array_flip($this->updatedProperties)); |
139: | } |
140: | |
141: | } |
142: |