-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.model.php
More file actions
166 lines (145 loc) · 3.87 KB
/
Copy pathclass.model.php
File metadata and controls
166 lines (145 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Sleepy;
require_once('class.hooks.php');
/**
* Models the data for a View to consume
*
* ## Usage
*
* ~~~ php
* $m = new Model();
* $m->name = "John Doe";
* ~~~
*
* ## Changelog
*
* ### Version 1.1
* * Add filters to the getter and setters
* * filters change if Model is extended
*
* ### Version 1.0
* * Initial release
*
* @date May 17, 2019
* @author Jaime A. Rodriguez <hi.i.am.jaime@gmail.com>
* @version 1.1
* @license http://opensource.org/licenses/MIT
*/
class Model implements \Iterator {
private $position = 0;
private $data = array();
public function rewind() {
$this->position = 0;
}
public function current() {
$keys = array_keys($this->data);
return $this->data[$keys[$this->position]];
}
public function key() {
$keys = array_keys($this->data);
return $keys[$this->position];
}
public function next() {
++$this->position;
}
public function valid() {
$keys = array_keys($this->data);
return isset($keys[$this->position]);
}
/**
* Return the name of the Class
*
* @return void
*/
private function clean_class() {
$className = get_class($this);
if ($pos = strrpos($className, '\\')) {
return substr($className, $pos + 1);
}
return $className;
}
/**
* Contructor
*
* @param array $props an array of properties to streamline adding them
*/
public function __construct($props = []) {
$this->position = 0;
if (class_exists('\Sleepy\Hook')) {
Hook::addAction($this->clean_class() . '_preprocess');
}
foreach($props as $property => $value) {
if (class_exists('\Sleepy\Hook')) {
$this->data[$property] = Hook::addFilter($this->clean_class() . '_set_' . $property, $value);
$this->data[$property] = Hook::addFilter($this->clean_class() . '_set_property', $value);
} else {
$this->data[$property] = $value;
}
}
}
/**
* When the Model is destructed
*/
public function __destruct() {
if (class_exists('\Sleepy\Hook')) {
Hook::addAction($this->clean_class() . '_postprocess');
}
}
/**
* Getter for all properties
*/
public function __get($property) {
if (isset($this->data[$property])) {
if (class_exists('\Sleepy\Hook')) {
$output = Hook::addFilter($this->clean_class() . '_get_' . $property, $this->data[$property]);
$output = Hook::addFilter($this->clean_class() . '_get_property', $output);
return $output;
} else {
return $this->data[$property];
}
}
}
/**
* Setter for all properties
*/
public function __set($property, $value) {
if (class_exists('\Sleepy\Hook')) {
$this->data[$property] = Hook::addFilter($this->clean_class() . '_set_' . $property, $value);
$this->data[$property] = Hook::addFilter($this->clean_class() . '_set_property', $value);
} else {
$this->data[$property] = $value;
}
}
/**
* Output for var_dump should be from $this->data
*
* @return array
*/
public function __debugInfo() {
return $this->data;
}
/**
* When used as a string, output JSON of $this->data
*
* @return string
*/
public function __toString() {
return $this->toJson($this->data);
}
/**
* When we invoke the object as a function
*
* @return void
*/
public function __invoke() {
return (object) $this->data;
}
/**
* Return the data as JSON
*
* @return void
*/
public function toJson() {
return json_encode($this->data);
}
}