1: <?php
2:
3: namespace Xu\Model;
4:
5: use ArrayAccess;
6: use ReflectionClass;
7: use Xu\Foundation\Jsonable;
8:
9: abstract class Model extends Jsonable implements ArrayAccess {
10:
11: 12: 13: 14: 15:
16: protected $attributes = [];
17:
18: 19: 20: 21: 22: 23: 24:
25: public function __isset( $key ) {
26: $attributes = $this->get_attributes_array();
27: return isset( $attributes[$key] );
28: }
29:
30: 31: 32: 33: 34: 35: 36:
37: public function __get( $key ) {
38: switch ( $key ) {
39: case 'id':
40: case 'ID':
41: return get_the_ID() ?: null;
42: case 'post':
43: return get_post();
44: default:
45: break;
46: }
47:
48: if ( $attribute = $this->get( $key ) ) {
49: return $attribute;
50: }
51: }
52:
53: 54: 55: 56: 57: 58:
59: public function __set( $key, $value ) {
60: $this->attributes[$key] = $value;
61: }
62:
63: 64: 65: 66: 67:
68: public function __unset( $key ) {
69: unset( $this->attributes[$key] );
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79:
80: public function __call( $method, array $parameters = [] ) {
81: return call_user_func_array( $method, $parameters );
82: }
83:
84: 85: 86: 87: 88: 89: 90: 91:
92: public static function __callStatic( $method, array $parameters = [] ) {
93: $instance = new static;
94: return call_user_func_array( [$instance, $method], $arguments );
95: }
96:
97: 98: 99: 100: 101: 102: 103:
104: public static function collection( $items = [] ) {
105: $items = is_array( $items ) ? $items : [$items];
106:
107: $items = array_map( function ( $item ) {
108: if ( $item instanceof Model ) {
109: return $item;
110: }
111:
112: if ( ! is_array( $item ) && ! is_object( $item ) ) {
113: return;
114: }
115:
116: if ( is_object( $item ) && get_class( $item ) !== 'stdClass' ) {
117: return;
118: }
119:
120: return static::create( (array) $item );
121: }, $items );
122:
123: return new Collection( array_filter( $items ) );
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public static function create( array $attributes = [] ) {
134: $model = new static;
135: $model->set_attributes( $attributes );
136: return $model;
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146:
147: public function get( $key, $default = null ) {
148: if ( empty( $key ) || ! is_string( $key ) ) {
149: return;
150: }
151:
152: $key = explode( '.', $key );
153: $attributes = $this->get_attributes_array();
154:
155: foreach ( $key as $part ) {
156: if ( $attributes instanceof Model === false && ( ! is_array( $attributes ) || ! array_key_exists( $part, $attributes ) ) ) {
157: return $default;
158: }
159:
160: $attributes = $attributes[$part];
161: }
162:
163: if ( is_null( $attributes ) ) {
164: return $default;
165: }
166:
167: return $attributes;
168: }
169:
170: 171: 172: 173: 174:
175: public function get_attributes() {
176: return [];
177: }
178:
179: 180: 181: 182: 183:
184: public function get_attributes_array() {
185: $attributes = $this->get_attributes();
186: $attributes = is_array( $attributes ) ? $attributes : [];
187: return array_merge( $attributes, $this->attributes );
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: public function get_model( $model ) {
198: return xu_get_model( $model );
199: }
200:
201: 202: 203: 204: 205:
206: public static function model() {
207: $reflection = new ReflectionClass( static::class );
208: return $reflection->newInstanceArgs( func_get_args() );
209: }
210:
211: 212: 213: 214: 215: 216: 217:
218: public function offsetExists( $offset ) {
219: return isset( $this->$offset );
220: }
221:
222: 223: 224: 225: 226: 227: 228:
229: public function offsetGet( $offset ) {
230: return $this->$offset;
231: }
232:
233: 234: 235: 236: 237: 238:
239: public function offsetSet( $offset, $value ) {
240: $this->$offset = $value;
241: }
242:
243: 244: 245: 246: 247:
248: public function offsetUnset( $offset ) {
249: unset( $this->$offset );
250: }
251:
252: 253: 254: 255: 256: 257:
258: public function set( $key, $value ) {
259: $this->$key = $value;
260: }
261:
262: 263: 264: 265: 266:
267: public function set_attributes( array $attributes ) {
268: foreach ( $attributes as $key => $value ) {
269: $this->$key = $value;
270: }
271: }
272:
273: 274: 275: 276: 277:
278: public function to_array() {
279: return $this->get_attributes_array();
280: }
281: }
282: