Completed
Pull Request — master (#3)
by James
02:59
created
src/Contract/Core/Container.php 1 patch
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -2,75 +2,75 @@
 block discarded – undo
2 2
 namespace Intraxia\Jaxion\Contract\Core;
3 3
 
4 4
 interface Container extends \ArrayAccess, \Iterator {
5
-	/**
6
-	 * Define a new service or value on the Container.
7
-	 *
8
-	 * The alias is the name that the value will be referenced by. This can be used by both
9
-	 * the `get` method to retrieve the value or through ArrayAccess (`$container['alias']`).
10
-	 * It should be a short name used to reference the defined value. The definition can be
11
-	 * any scalar value to assign to the alias, or it can define a service object to return.
12
-	 * This can be accomplished by passing in a closure, which takes the container and returns
13
-	 * a fully constructed object. This closure will be executed every time the class is fetched.
14
-	 * If an already-instantiated object is passed in, it will be returned when fetched. A
15
-	 * Definition object will be returned for additional manipulation. Scalar values will be
16
-	 * locked automatically and can't be overridden.
17
-	 *
18
-	 * @param string|array $alias
19
-	 * @param mixed        $definition
20
-	 *
21
-	 * @return $this
22
-	 */
23
-	public function define( $alias, $definition );
5
+    /**
6
+     * Define a new service or value on the Container.
7
+     *
8
+     * The alias is the name that the value will be referenced by. This can be used by both
9
+     * the `get` method to retrieve the value or through ArrayAccess (`$container['alias']`).
10
+     * It should be a short name used to reference the defined value. The definition can be
11
+     * any scalar value to assign to the alias, or it can define a service object to return.
12
+     * This can be accomplished by passing in a closure, which takes the container and returns
13
+     * a fully constructed object. This closure will be executed every time the class is fetched.
14
+     * If an already-instantiated object is passed in, it will be returned when fetched. A
15
+     * Definition object will be returned for additional manipulation. Scalar values will be
16
+     * locked automatically and can't be overridden.
17
+     *
18
+     * @param string|array $alias
19
+     * @param mixed        $definition
20
+     *
21
+     * @return $this
22
+     */
23
+    public function define( $alias, $definition );
24 24
 
25
-	/**
26
-	 * Defines a new singleton on the Container.
27
-	 *
28
-	 * Functions identically to Container::define, except closures passed in are only executed
29
-	 * once, and the return value is reused across multiple fetches.
30
-	 *
31
-	 * @param string|array $alias
32
-	 * @param mixed        $definition
33
-	 *
34
-	 * @return $this
35
-	 */
36
-	public function share( $alias, $definition );
25
+    /**
26
+     * Defines a new singleton on the Container.
27
+     *
28
+     * Functions identically to Container::define, except closures passed in are only executed
29
+     * once, and the return value is reused across multiple fetches.
30
+     *
31
+     * @param string|array $alias
32
+     * @param mixed        $definition
33
+     *
34
+     * @return $this
35
+     */
36
+    public function share( $alias, $definition );
37 37
 
38
-	/**
39
-	 * Fetches the value for the provided alias.
40
-	 *
41
-	 * @param string $alias
42
-	 *
43
-	 * @return mixed
44
-	 */
45
-	public function fetch( $alias );
38
+    /**
39
+     * Fetches the value for the provided alias.
40
+     *
41
+     * @param string $alias
42
+     *
43
+     * @return mixed
44
+     */
45
+    public function fetch( $alias );
46 46
 
47
-	/**
48
-	 * Checks whether the provided alias exists on the container.
49
-	 *
50
-	 * @param string $alias
51
-	 *
52
-	 * @return bool
53
-	 */
54
-	public function has( $alias );
47
+    /**
48
+     * Checks whether the provided alias exists on the container.
49
+     *
50
+     * @param string $alias
51
+     *
52
+     * @return bool
53
+     */
54
+    public function has( $alias );
55 55
 
56
-	/**
57
-	 * Removes the provided alias from the container.
58
-	 *
59
-	 * @param string $alias
60
-	 *
61
-	 * @return bool
62
-	 */
63
-	public function remove( $alias );
56
+    /**
57
+     * Removes the provided alias from the container.
58
+     *
59
+     * @param string $alias
60
+     *
61
+     * @return bool
62
+     */
63
+    public function remove( $alias );
64 64
 
65
-	/**
66
-	 * Registers a service provider with the container.
67
-	 *
68
-	 * A service provider is responsible for defining and generating services that will be bound
69
-	 * into the container. This keeps the container and Application responsible solely for maintaining
70
-	 * the generated services and the API for registering them and allows for a clean interface for
71
-	 * adding new services to the container.
72
-	 *
73
-	 * @param ServiceProvider $provider
74
-	 */
75
-	public function register( ServiceProvider $provider );
65
+    /**
66
+     * Registers a service provider with the container.
67
+     *
68
+     * A service provider is responsible for defining and generating services that will be bound
69
+     * into the container. This keeps the container and Application responsible solely for maintaining
70
+     * the generated services and the API for registering them and allows for a clean interface for
71
+     * adding new services to the container.
72
+     *
73
+     * @param ServiceProvider $provider
74
+     */
75
+    public function register( ServiceProvider $provider );
76 76
 }
Please login to merge, or discard this patch.
src/Contract/Http/Filter.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -10,13 +10,13 @@
 block discarded – undo
10 10
  * @subpackage Contract\Http
11 11
  */
12 12
 interface Filter {
13
-	/**
14
-	 * Generates argument rules.
15
-	 *
16
-	 * Returns an array matching the WP-API format for argument rules,
17
-	 * including sanitization, validation, required, or defaults.
18
-	 *
19
-	 * @return array
20
-	 */
21
-	public function rules();
13
+    /**
14
+     * Generates argument rules.
15
+     *
16
+     * Returns an array matching the WP-API format for argument rules,
17
+     * including sanitization, validation, required, or defaults.
18
+     *
19
+     * @return array
20
+     */
21
+    public function rules();
22 22
 }
Please login to merge, or discard this patch.
src/Contract/Http/Guard.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -10,13 +10,13 @@
 block discarded – undo
10 10
  * @subpackage Contract\Http
11 11
  */
12 12
 interface Guard {
13
-	/**
14
-	 * Validates when the user is authorized for the route.
15
-	 *
16
-	 * Returns a boolean based on whether the current user is authorized
17
-	 * to interact with the given route.
18
-	 *
19
-	 * @return bool
20
-	 */
21
-	public function authorized();
13
+    /**
14
+     * Validates when the user is authorized for the route.
15
+     *
16
+     * Returns a boolean based on whether the current user is authorized
17
+     * to interact with the given route.
18
+     *
19
+     * @return bool
20
+     */
21
+    public function authorized();
22 22
 }
Please login to merge, or discard this patch.
src/Http/Endpoint.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -14,141 +14,141 @@
 block discarded – undo
14 14
  * @subpackage Http
15 15
  */
16 16
 class Endpoint {
17
-	/**
18
-	 * Endpoint's route.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	protected $route;
23
-
24
-	/**
25
-	 * Endpoint's HTTP verb(s).
26
-	 *
27
-	 * @var string
28
-	 */
29
-	protected $method;
30
-
31
-	/**
32
-	 * Endpoint's callback.
33
-	 *
34
-	 * @var callable
35
-	 */
36
-	protected $callback;
37
-
38
-	/**
39
-	 * Endpoint's permission guard.
40
-	 *
41
-	 * @var GuardContract
42
-	 */
43
-	protected $guard;
44
-
45
-	/**
46
-	 * Endpoint's arguments filter.
47
-	 *
48
-	 * @var FilterContract
49
-	 */
50
-	protected $filter;
51
-
52
-	/**
53
-	 * Endpoint's route prefix.
54
-	 *
55
-	 * @var string
56
-	 */
57
-	protected $prefix;
58
-
59
-	/**
60
-	 * Instantiate a new endpoint with a provided route, method, and callback.
61
-	 *
62
-	 * @param string   $route
63
-	 * @param string   $method
64
-	 * @param callable $callback
65
-	 *
66
-	 * @throws MalformedRouteException
67
-	 */
68
-	public function __construct( $route, $method, $callback ) {
69
-		if ( ! Str::starts_with( $route, '/' ) || Str::ends_with( $route, '/' ) ) {
70
-			throw new MalformedRouteException;
71
-		}
72
-
73
-		$this->route    = $route;
74
-		$this->method   = $method;
75
-		$this->callback = $callback;
76
-	}
77
-
78
-	/**
79
-	 * Generates the endpoint's route.
80
-	 *
81
-	 * Combines the prefix with the route to generate the full route string.
82
-	 *
83
-	 * @return string
84
-	 */
85
-	public function get_route() {
86
-		return ( $this->prefix ?: '' ) . $this->route;
87
-	}
88
-
89
-	/**
90
-	 * Generates the endpoint's WP-API options array.
91
-	 *
92
-	 * @return array
93
-	 */
94
-	public function get_options() {
95
-		$options = array(
96
-			'methods'  => $this->method,
97
-			'callback' => $this->callback,
98
-		);
99
-
100
-		if ( $this->guard ) {
101
-			$options['permission_callback'] = array( $this->guard, 'authorized' );
102
-		}
103
-
104
-		if ( $this->filter ) {
105
-			$options['args'] = $this->filter->rules();
106
-		}
107
-
108
-		return $options;
109
-	}
110
-
111
-	/**
112
-	 * Sets the endpoint's permission guard.
113
-	 *
114
-	 * @param GuardContract $guard
115
-	 *
116
-	 * @return $this
117
-	 */
118
-	public function set_guard( GuardContract $guard ) {
119
-		$this->guard = $guard;
120
-
121
-		return $this;
122
-	}
123
-
124
-	/**
125
-	 * Sets the endpoint's arguments filter.
126
-	 *
127
-	 * @param FilterContract $filter
128
-	 *
129
-	 * @return $this
130
-	 */
131
-	public function set_filter( FilterContract $filter ) {
132
-		$this->filter = $filter;
133
-
134
-		return $this;
135
-	}
136
-
137
-	/**
138
-	 * Sets the endpoint's prefix.
139
-	 *
140
-	 * @param string $prefix
141
-	 *
142
-	 * @return $this
143
-	 * @throws MalformedRouteException
144
-	 */
145
-	public function set_prefix( $prefix ) {
146
-		if ( ! Str::starts_with( $prefix, '/' ) || Str::ends_with( $prefix, '/' ) ) {
147
-			throw new MalformedRouteException;
148
-		}
149
-
150
-		$this->prefix = $prefix;
151
-
152
-		return $this;
153
-	}
17
+    /**
18
+     * Endpoint's route.
19
+     *
20
+     * @var string
21
+     */
22
+    protected $route;
23
+
24
+    /**
25
+     * Endpoint's HTTP verb(s).
26
+     *
27
+     * @var string
28
+     */
29
+    protected $method;
30
+
31
+    /**
32
+     * Endpoint's callback.
33
+     *
34
+     * @var callable
35
+     */
36
+    protected $callback;
37
+
38
+    /**
39
+     * Endpoint's permission guard.
40
+     *
41
+     * @var GuardContract
42
+     */
43
+    protected $guard;
44
+
45
+    /**
46
+     * Endpoint's arguments filter.
47
+     *
48
+     * @var FilterContract
49
+     */
50
+    protected $filter;
51
+
52
+    /**
53
+     * Endpoint's route prefix.
54
+     *
55
+     * @var string
56
+     */
57
+    protected $prefix;
58
+
59
+    /**
60
+     * Instantiate a new endpoint with a provided route, method, and callback.
61
+     *
62
+     * @param string   $route
63
+     * @param string   $method
64
+     * @param callable $callback
65
+     *
66
+     * @throws MalformedRouteException
67
+     */
68
+    public function __construct( $route, $method, $callback ) {
69
+        if ( ! Str::starts_with( $route, '/' ) || Str::ends_with( $route, '/' ) ) {
70
+            throw new MalformedRouteException;
71
+        }
72
+
73
+        $this->route    = $route;
74
+        $this->method   = $method;
75
+        $this->callback = $callback;
76
+    }
77
+
78
+    /**
79
+     * Generates the endpoint's route.
80
+     *
81
+     * Combines the prefix with the route to generate the full route string.
82
+     *
83
+     * @return string
84
+     */
85
+    public function get_route() {
86
+        return ( $this->prefix ?: '' ) . $this->route;
87
+    }
88
+
89
+    /**
90
+     * Generates the endpoint's WP-API options array.
91
+     *
92
+     * @return array
93
+     */
94
+    public function get_options() {
95
+        $options = array(
96
+            'methods'  => $this->method,
97
+            'callback' => $this->callback,
98
+        );
99
+
100
+        if ( $this->guard ) {
101
+            $options['permission_callback'] = array( $this->guard, 'authorized' );
102
+        }
103
+
104
+        if ( $this->filter ) {
105
+            $options['args'] = $this->filter->rules();
106
+        }
107
+
108
+        return $options;
109
+    }
110
+
111
+    /**
112
+     * Sets the endpoint's permission guard.
113
+     *
114
+     * @param GuardContract $guard
115
+     *
116
+     * @return $this
117
+     */
118
+    public function set_guard( GuardContract $guard ) {
119
+        $this->guard = $guard;
120
+
121
+        return $this;
122
+    }
123
+
124
+    /**
125
+     * Sets the endpoint's arguments filter.
126
+     *
127
+     * @param FilterContract $filter
128
+     *
129
+     * @return $this
130
+     */
131
+    public function set_filter( FilterContract $filter ) {
132
+        $this->filter = $filter;
133
+
134
+        return $this;
135
+    }
136
+
137
+    /**
138
+     * Sets the endpoint's prefix.
139
+     *
140
+     * @param string $prefix
141
+     *
142
+     * @return $this
143
+     * @throws MalformedRouteException
144
+     */
145
+    public function set_prefix( $prefix ) {
146
+        if ( ! Str::starts_with( $prefix, '/' ) || Str::ends_with( $prefix, '/' ) ) {
147
+            throw new MalformedRouteException;
148
+        }
149
+
150
+        $this->prefix = $prefix;
151
+
152
+        return $this;
153
+    }
154 154
 }
Please login to merge, or discard this patch.
src/Axolotl/Model.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -825,7 +825,7 @@
 block discarded – undo
825 825
 	 * @param string $type
826 826
 	 * @param string $local_key
827 827
 	 *
828
-	 * @return HasMany
828
+	 * @return BelongsToOne
829 829
 	 */
830 830
 	protected function belongs_to_one( $class, $type, $local_key = '' ) {
831 831
 		return new BelongsToOne( $this, $class, $type, $local_key );
Please login to merge, or discard this patch.
Indentation   +826 added lines, -826 removed lines patch added patch discarded remove patch
@@ -23,830 +23,830 @@
 block discarded – undo
23 23
  * @since      0.1.0
24 24
  */
25 25
 abstract class Model implements Serializes {
26
-	/**
27
-	 * Memoized values for class methods.
28
-	 *
29
-	 * @var array
30
-	 */
31
-	private static $memo = array();
32
-
33
-	/**
34
-	 * Model attributes.
35
-	 *
36
-	 * @var array
37
-	 */
38
-	private $attributes = array(
39
-		'table'  => array(),
40
-		'object' => null,
41
-	);
42
-
43
-	/**
44
-	 * Model's original attributes.
45
-	 *
46
-	 * @var array
47
-	 */
48
-	private $original = array(
49
-		'table'  => array(),
50
-		'object' => null,
51
-	);
52
-
53
-	/**
54
-	 * Properties which are allowed to be set on the model.
55
-	 *
56
-	 * If this array is empty, any attributes can be set on the model.
57
-	 *
58
-	 * @var string[]
59
-	 */
60
-	protected $fillable = array();
61
-
62
-	/**
63
-	 * Properties which cannot be automatically filled on the model.
64
-	 *
65
-	 * If the model is unguarded, these properties can be filled.
66
-	 *
67
-	 * @var array
68
-	 */
69
-	protected $guarded = array();
70
-
71
-	/**
72
-	 * Properties which should not be serialized.
73
-	 *
74
-	 * @var array
75
-	 */
76
-	protected $hidden = array();
77
-
78
-	/**
79
-	 * Properties which should be serialized.
80
-	 *
81
-	 * @var array
82
-	 */
83
-	protected $visible = array();
84
-
85
-	/**
86
-	 * Relations saved on the Model.
87
-	 *
88
-	 * @var array
89
-	 */
90
-	protected $related = array();
91
-
92
-	/**
93
-	 * Whether the model's properties are guarded.
94
-	 *
95
-	 * When false, allows guarded properties to be filled.
96
-	 *
97
-	 * @var bool
98
-	 */
99
-	protected $is_guarded = true;
100
-
101
-	/**
102
-	 * Whether the Model is having its relations filled.
103
-	 *
104
-	 * @var bool
105
-	 */
106
-	protected $filling = false;
107
-
108
-	/**
109
-	 * Constructs a new model with provided attributes.
110
-	 *
111
-	 * If 'object' is passed as one of the attributes, the underlying post
112
-	 * will be overwritten.
113
-	 *
114
-	 * @param array <string, mixed> $attributes
115
-	 */
116
-	public function __construct( array $attributes = array() ) {
117
-		$this->maybe_boot();
118
-		$this->sync_original();
119
-
120
-		if ( $this->uses_wp_object() ) {
121
-			$this->create_wp_object();
122
-		}
123
-
124
-		$this->refresh( $attributes );
125
-	}
126
-
127
-	/**
128
-	 * Refreshes the model's current attributes with the provided array.
129
-	 *
130
-	 * The model's attributes will match what was provided in the array,
131
-	 * and any attributes not passed
132
-	 *
133
-	 * @param array $attributes
134
-	 */
135
-	public function refresh( array $attributes ) {
136
-		$this->clear();
137
-
138
-		foreach ( $attributes as $name => $value ) {
139
-			$this->set_attribute( $name, $value );
140
-		}
141
-	}
142
-
143
-	/**
144
-	 * Get the model's table attributes.
145
-	 *
146
-	 * Returns the array of for the model that will either need to be
147
-	 * saved in postmeta or a separate table.
148
-	 *
149
-	 * @return array
150
-	 */
151
-	public function get_table_attributes() {
152
-		return $this->attributes['table'];
153
-	}
154
-
155
-	/**
156
-	 * Get the model's original attributes.
157
-	 *
158
-	 * @return array
159
-	 */
160
-	public function get_original_table_attributes() {
161
-		return $this->original['table'];
162
-	}
163
-
164
-	/**
165
-	 * Retrieve an array of the attributes on the model
166
-	 * that have changed compared to the model's
167
-	 * original data.
168
-	 *
169
-	 * @return array
170
-	 */
171
-	public function get_changed_table_attributes() {
172
-		$changed = array();
173
-
174
-		foreach ( $this->get_table_attributes() as $attribute ) {
175
-			if ( $this->get_attribute( $attribute ) !==
176
-			     $this->get_original_attribute( $attribute )
177
-			) {
178
-				$changed[ $attribute ] = $this->get_attribute( $attribute );
179
-			}
180
-		}
181
-
182
-		return $changed;
183
-	}
184
-
185
-	/**
186
-	 * Get the model's underlying post.
187
-	 *
188
-	 * Returns the underlying WP_Post object for the model, representing
189
-	 * the data that will be save in the wp_posts table.
190
-	 *
191
-	 * @return false|WP_Post|WP_Term
192
-	 */
193
-	public function get_underlying_wp_object() {
194
-		if ( isset( $this->attributes['object'] ) ) {
195
-			return $this->attributes['object'];
196
-		}
197
-
198
-		return false;
199
-	}
200
-
201
-	/**
202
-	 * Get the model's original underlying post.
203
-	 *
204
-	 * @return WP_Post
205
-	 */
206
-	public function get_original_underlying_wp_object() {
207
-		return $this->original['object'];
208
-	}
209
-
210
-	/**
211
-	 * Get the model attributes on the WordPress object
212
-	 * that have changed compared to the model's
213
-	 * original attributes.
214
-	 *
215
-	 * @return array
216
-	 */
217
-	public function get_changed_wp_object_attributes() {
218
-		$changed = array();
219
-
220
-		foreach ( $this->get_wp_object_keys() as $key ) {
221
-			if ( $this->get_attribute( $key ) !==
222
-			     $this->get_original_attribute( $key )
223
-			) {
224
-				$changed[ $key ] = $this->get_attribute( $key );
225
-			}
226
-		}
227
-
228
-		return $changed;
229
-	}
230
-
231
-	/**
232
-	 * Magic __set method.
233
-	 *
234
-	 * Passes the name and value to set_attribute, which is where the magic happens.
235
-	 *
236
-	 * @param string $name
237
-	 * @param mixed  $value
238
-	 */
239
-	public function __set( $name, $value ) {
240
-		$this->set_attribute( $name, $value );
241
-	}
242
-
243
-	/**
244
-	 * Sets the model attributes.
245
-	 *
246
-	 * Checks whether the model attribute can be set, check if it
247
-	 * maps to the WP_Post property, otherwise, assigns it to the
248
-	 * table attribute array.
249
-	 *
250
-	 * @param string $name
251
-	 * @param mixed  $value
252
-	 *
253
-	 * @return $this
254
-	 *
255
-	 * @throws GuardedPropertyException
256
-	 */
257
-	public function set_attribute( $name, $value ) {
258
-		if ( 'object' === $name ) {
259
-			return $this->override_wp_object( $value );
260
-		}
261
-
262
-		if ( ! $this->is_fillable( $name ) ) {
263
-			throw new GuardedPropertyException;
264
-		}
265
-
266
-		if ( $method = $this->has_map_method( $name ) ) {
267
-			$this->attributes['object']->{$this->{$method}()} = $value;
268
-		} else {
269
-			$this->attributes['table'][ $name ] = $value;
270
-		}
271
-
272
-		return $this;
273
-	}
274
-
275
-	/**
276
-	 * Retrieves all the attribute keys for the model.
277
-	 *
278
-	 * @return array
279
-	 */
280
-	public function get_attribute_keys() {
281
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
282
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
283
-		}
284
-
285
-		return self::$memo[ get_called_class() ][ __METHOD__ ]
286
-			= array_merge(
287
-				$this->fillable,
288
-				$this->guarded,
289
-				$this->get_compute_methods(),
290
-				$this->get_related_methods()
291
-			);
292
-	}
293
-
294
-	/**
295
-	 * Retrieves the attribute keys that aren't mapped to a post.
296
-	 *
297
-	 * @return array
298
-	 */
299
-	public function get_table_keys() {
300
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
301
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
302
-		}
303
-
304
-		$keys = array();
305
-
306
-		foreach ( $this->get_attribute_keys() as $key ) {
307
-			if ( ! $this->has_map_method( $key ) &&
308
-			     ! $this->has_compute_method( $key ) &&
309
-			     ! $this->has_related_method( $key )
310
-			) {
311
-				$keys[] = $key;
312
-			}
313
-		}
314
-
315
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
316
-	}
317
-
318
-	/**
319
-	 * Retrieves the attribute keys that are mapped to a post.
320
-	 *
321
-	 * @return array
322
-	 */
323
-	public function get_wp_object_keys() {
324
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
325
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
326
-		}
327
-
328
-		$keys = array();
329
-
330
-		foreach ( $this->get_attribute_keys() as $key ) {
331
-			if ( $this->has_map_method( $key ) ) {
332
-				$keys[] = $key;
333
-			}
334
-		}
335
-
336
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
337
-	}
338
-
339
-	/**
340
-	 * Returns the model's keys that are computed at call time.
341
-	 *
342
-	 * @return array
343
-	 */
344
-	public function get_computed_keys() {
345
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
346
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
347
-		}
348
-
349
-		$keys = array();
350
-
351
-		foreach ( $this->get_attribute_keys() as $key ) {
352
-			if ( $this->has_compute_method( $key ) ) {
353
-				$keys[] = $key;
354
-			}
355
-		}
356
-
357
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
358
-	}
359
-
360
-	/**
361
-	 * Returns the model's keys that are related to other Models.
362
-	 *
363
-	 * @return array
364
-	 */
365
-	public function get_related_keys() {
366
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
367
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
368
-		}
369
-
370
-		$keys = array();
371
-
372
-		foreach ( $this->get_attribute_keys() as $key ) {
373
-			if ( $this->has_related_method( $key ) ) {
374
-				$keys[] = $key;
375
-			}
376
-		}
377
-
378
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
379
-	}
380
-
381
-	/**
382
-	 * Serializes the model's public data into an array.
383
-	 *
384
-	 * @return array
385
-	 */
386
-	public function serialize() {
387
-		$attributes = array();
388
-
389
-		if ( $this->visible ) {
390
-			// If visible attributes are set, we'll only reveal those.
391
-			foreach ( $this->visible as $key ) {
392
-				$attributes[ $key ] = $this->get_attribute( $key );
393
-			}
394
-		} elseif ( $this->hidden ) {
395
-			// If hidden attributes are set, we'll grab everything and hide those.
396
-			foreach ( $this->get_attribute_keys() as $key ) {
397
-				if ( ! in_array( $key, $this->hidden ) ) {
398
-					$attributes[ $key ] = $this->get_attribute( $key );
399
-				}
400
-			}
401
-		} else {
402
-			// If nothing is hidden/visible, we'll grab and reveal everything.
403
-			foreach ( $this->get_attribute_keys() as $key ) {
404
-				$attributes[ $key ] = $this->get_attribute( $key );
405
-			}
406
-		}
407
-
408
-		return array_map( function ( $attribute ) {
409
-			if ( $attribute instanceof Serializes ) {
410
-				return $attribute->serialize();
411
-			}
412
-
413
-			return $attribute;
414
-		}, $attributes );
415
-	}
416
-
417
-	/**
418
-	 * Syncs the current attributes to the model's original.
419
-	 *
420
-	 * @return $this
421
-	 */
422
-	public function sync_original() {
423
-		$this->original = $this->attributes;
424
-
425
-		if ( $this->attributes['object'] ) {
426
-			$this->original['object'] = clone $this->attributes['object'];
427
-		}
428
-
429
-		return $this;
430
-	}
431
-
432
-	/**
433
-	 * Checks if a given attribute is mass-fillable.
434
-	 *
435
-	 * Returns true if the attribute can be filled, false if it can't.
436
-	 *
437
-	 * @param string $name
438
-	 *
439
-	 * @return bool
440
-	 */
441
-	private function is_fillable( $name ) {
442
-		// If this model isn't guarded, everything is fillable.
443
-		if ( ! $this->is_guarded ) {
444
-			return true;
445
-		}
446
-
447
-		// If it's in the fillable array, then it's fillable.
448
-		if ( in_array( $name, $this->fillable ) ) {
449
-			return true;
450
-		}
451
-
452
-		// If it's explicitly guarded, then it's not fillable.
453
-		if ( in_array( $name, $this->guarded ) ) {
454
-			return false;
455
-		}
456
-
457
-		// If fillable hasn't been defined, then everything else fillable.
458
-		return ! $this->fillable;
459
-	}
460
-
461
-	/**
462
-	 * Overrides the current WP_Post with a provided one.
463
-	 *
464
-	 * Resets the post's default values and stores it in the attributes.
465
-	 *
466
-	 * @param WP_Post $value
467
-	 *
468
-	 * @return $this
469
-	 */
470
-	private function override_wp_object( $value ) {
471
-		$this->attributes['object'] = $this->set_wp_object_constants( $value );
472
-
473
-		return $this;
474
-	}
475
-
476
-	/**
477
-	 * Create and set with a new blank post.
478
-	 *
479
-	 * Creates a new WP_Post object, assigns it the default attributes,
480
-	 * and stores it in the attributes.
481
-	 *
482
-	 * @throws LogicException
483
-	 */
484
-	private function create_wp_object() {
485
-		switch ( true ) {
486
-			case $this instanceof UsesWordPressPost:
487
-				$object = new WP_Post( (object) array() );
488
-				break;
489
-			case $this instanceof UsesWordPressTerm:
490
-				$object = new WP_Term( (object) array() );
491
-				break;
492
-			default:
493
-				throw new LogicException;
494
-				break;
495
-		}
496
-
497
-		$this->attributes['object'] = $this->set_wp_object_constants( $object );
498
-	}
499
-
500
-	/**
501
-	 * Enforces values on the post that can't change.
502
-	 *
503
-	 * Primarily, this is used to make sure the post_type always maps
504
-	 * to the model's "$type" property, but this can all be overridden
505
-	 * by the developer to enforce other values in the model.
506
-	 *
507
-	 * @param object $object
508
-	 *
509
-	 * @return object
510
-	 */
511
-	protected function set_wp_object_constants( $object ) {
512
-		if ( $this instanceof UsesWordPressPost ) {
513
-			$object->post_type = $this::get_post_type();
514
-		}
515
-
516
-		if ( $this instanceof UsesWordPressTerm ) {
517
-			$object->taxonomy = $this::get_taxonomy();
518
-		}
519
-
520
-		return $object;
521
-	}
522
-
523
-	/**
524
-	 * Magic __get method.
525
-	 *
526
-	 * Passes the name and value to get_attribute, which is where the magic happens.
527
-	 *
528
-	 * @param string $name
529
-	 *
530
-	 * @return mixed
531
-	 */
532
-	public function __get( $name ) {
533
-		return $this->get_attribute( $name );
534
-	}
535
-
536
-	/**
537
-	 * Retrieves the model attribute.
538
-	 *
539
-	 * @param string $name
540
-	 *
541
-	 * @return mixed
542
-	 *
543
-	 * @throws PropertyDoesNotExistException If property isn't found.
544
-	 */
545
-	public function get_attribute( $name ) {
546
-		if ( $method = $this->has_map_method( $name ) ) {
547
-			$value = $this->attributes['object']->{$this->{$method}()};
548
-		} elseif ( $method = $this->has_compute_method( $name ) ) {
549
-			$value = $this->{$method}();
550
-		} else if ( $method = $this->has_related_method( $name ) ) {
551
-			$value = $this->get_related( $this->{$method}()->get_sha() );
552
-		} else {
553
-			if ( ! isset( $this->attributes['table'][ $name ] ) ) {
554
-				throw new PropertyDoesNotExistException;
555
-			}
556
-
557
-			$value = $this->attributes['table'][ $name ];
558
-		}
559
-
560
-		return $value;
561
-	}
562
-
563
-	/**
564
-	 * Retrieve the model's original attribute value.
565
-	 *
566
-	 * @param string $name
567
-	 *
568
-	 * @return mixed
569
-	 *
570
-	 * @throws PropertyDoesNotExistException If property isn't found.
571
-	 */
572
-	public function get_original_attribute( $name ) {
573
-		$original = new static( $this->original );
574
-
575
-		return $original->get_attribute( $name );
576
-	}
577
-
578
-	/**
579
-	 * Fetches the Model's primary ID, depending on the model
580
-	 * implementation.
581
-	 *
582
-	 * @return int
583
-	 *
584
-	 * @throws LogicException
585
-	 */
586
-	public function get_primary_id() {
587
-		if ( $this instanceof UsesWordPressPost ) {
588
-			return $this->get_underlying_wp_object()->ID;
589
-		}
590
-
591
-		if ( $this instanceof UsesWordPressTerm ) {
592
-			return $this->get_underlying_wp_object()->term_id;
593
-		}
594
-
595
-		// Model w/o wp_object not yet supported.
596
-		throw new LogicException;
597
-	}
598
-
599
-	/**
600
-	 * Generates the table foreign key, depending on the model
601
-	 * implementation.
602
-	 *
603
-	 * @return string
604
-	 *
605
-	 * @throws LogicException
606
-	 */
607
-	public function get_foreign_key() {
608
-		if ( $this instanceof UsesWordPressPost ) {
609
-			return 'post_id';
610
-		}
611
-
612
-		// Model w/o wp_object not yet supported.
613
-		throw new LogicException;
614
-	}
615
-
616
-	/**
617
-	 * Gets the related Model or Collection for the given sha.
618
-	 *
619
-	 * @param string $sha
620
-	 *
621
-	 * @return Model|Collection
622
-	 */
623
-	public function get_related( $sha ) {
624
-		return $this->related[ $sha ];
625
-	}
626
-
627
-	/**
628
-	 * Sets the related Model or Collection for the given sha.
629
-	 *
630
-	 * @param string           $sha
631
-	 * @param Model|Collection $target
632
-	 *
633
-	 * @throws RuntimeException
634
-	 */
635
-	public function set_related( $sha, $target ) {
636
-		if ( ! ( $target instanceof Model ) && ! ( $target instanceof Collection ) ) {
637
-			throw new RuntimeException;
638
-		}
639
-
640
-		$this->related[ $sha ] = $target;
641
-	}
642
-
643
-	/**
644
-	 * Checks whether the attribute has a map method.
645
-	 *
646
-	 * This is used to determine whether the attribute maps to a
647
-	 * property on the underlying WP_Post object. Returns the
648
-	 * method if one exists, returns false if it doesn't.
649
-	 *
650
-	 * @param string $name
651
-	 *
652
-	 * @return false|string
653
-	 */
654
-	protected function has_map_method( $name ) {
655
-		if ( method_exists( $this, $method = "map_{$name}" ) ) {
656
-			return $method;
657
-		}
658
-
659
-		return false;
660
-	}
661
-
662
-	/**
663
-	 * Checks whether the attribute has a compute method.
664
-	 *
665
-	 * This is used to determine if the attribute should be computed
666
-	 * from other attributes.
667
-	 *
668
-	 * @param string $name
669
-	 *
670
-	 * @return false|string
671
-	 */
672
-	protected function has_compute_method( $name ) {
673
-		if ( method_exists( $this, $method = "compute_{$name}" ) ) {
674
-			return $method;
675
-		}
676
-
677
-		return false;
678
-	}
679
-
680
-	/**
681
-	 * Checks whether the attribute has a compute method.
682
-	 *
683
-	 * This is used to determine if the attribute should be computed
684
-	 * from other attributes.
685
-	 *
686
-	 * @param string $name
687
-	 *
688
-	 * @return false|string
689
-	 */
690
-	protected function has_related_method( $name ) {
691
-		if ( method_exists( $this, $method = "related_{$name}" ) ) {
692
-			return $method;
693
-		}
694
-
695
-		return false;
696
-	}
697
-
698
-	/**
699
-	 * Clears all the current attributes from the model.
700
-	 *
701
-	 * This does not touch the model's original attributes, and will
702
-	 * only clear fillable attributes, unless the model is unguarded.
703
-	 *
704
-	 * @return $this
705
-	 */
706
-	public function clear() {
707
-		$keys = $this->get_attribute_keys();
708
-
709
-		foreach ( $keys as $key ) {
710
-			try {
711
-				$this->set_attribute( $key, null );
712
-			} catch ( GuardedPropertyException $e ) {
713
-				// We won't clear out guarded attributes.
714
-			}
715
-		}
716
-
717
-		return $this;
718
-	}
719
-
720
-	/**
721
-	 * Unguards the model.
722
-	 *
723
-	 * Sets the model to be unguarded, allowing the filling of
724
-	 * guarded attributes.
725
-	 */
726
-	public function unguard() {
727
-		$this->is_guarded = false;
728
-	}
729
-
730
-	/**
731
-	 * Reguards the model.
732
-	 *
733
-	 * Sets the model to be guarded, preventing filling of
734
-	 * guarded attributes.
735
-	 */
736
-	public function reguard() {
737
-		$this->is_guarded = true;
738
-	}
739
-
740
-	/**
741
-	 * Retrieves all the compute methods on the model.
742
-	 *
743
-	 * @return array
744
-	 */
745
-	protected function get_compute_methods() {
746
-		$methods = get_class_methods( get_called_class() );
747
-		$methods = array_filter( $methods, function ( $method ) {
748
-			return strrpos( $method, 'compute_', - strlen( $method ) ) !== false;
749
-		} );
750
-		$methods = array_map( function ( $method ) {
751
-			return substr( $method, strlen( 'compute_' ) );
752
-		}, $methods );
753
-
754
-		return $methods;
755
-	}
756
-
757
-	/**
758
-	 * Retrieves all the related methods on the model.
759
-	 *
760
-	 * @return array
761
-	 */
762
-	protected function get_related_methods() {
763
-		$methods = get_class_methods( get_called_class() );
764
-		$methods = array_filter( $methods, function ( $method ) {
765
-			return strrpos( $method, 'related_', - strlen( $method ) ) !== false;
766
-		} );
767
-		$methods = array_map( function ( $method ) {
768
-			return substr( $method, strlen( 'related_' ) );
769
-		}, $methods );
770
-
771
-		return $methods;
772
-	}
773
-
774
-	/**
775
-	 * Returns whether this relation has already been filled on the model.
776
-	 *
777
-	 * @param string $relation
778
-	 *
779
-	 * @return bool
780
-	 */
781
-	public function relation_is_filled( $relation ) {
782
-		$sha = $this
783
-			->{$this->has_related_method( $relation )}()
784
-			->get_sha();
785
-
786
-		return isset( $this->related[ $sha ] );
787
-	}
788
-
789
-	/**
790
-	 * Returns whether the Model is currently having
791
-	 * its relationships filled.
792
-	 *
793
-	 * @return bool
794
-	 */
795
-	public function is_filling() {
796
-		return $this->filling;
797
-	}
798
-
799
-	/**
800
-	 * Sets whether the Model is having its relationships filled.
801
-	 *
802
-	 * @param bool $is_filling
803
-	 */
804
-	public function set_filling( $is_filling ) {
805
-		$this->filling = $is_filling;
806
-	}
807
-
808
-	/**
809
-	 * Returns a HasMany relationship for the model.
810
-	 *
811
-	 * @param string $class
812
-	 * @param string $type
813
-	 * @param string $foreign_key
814
-	 *
815
-	 * @return HasMany
816
-	 */
817
-	protected function has_many( $class, $type, $foreign_key ) {
818
-		return new HasMany( $this, $class, $type, $foreign_key );
819
-	}
820
-
821
-	/**
822
-	 * Returns a BelongsToOne relationship for the model.
823
-	 *
824
-	 * @param string $class
825
-	 * @param string $type
826
-	 * @param string $local_key
827
-	 *
828
-	 * @return HasMany
829
-	 */
830
-	protected function belongs_to_one( $class, $type, $local_key = '' ) {
831
-		return new BelongsToOne( $this, $class, $type, $local_key );
832
-	}
833
-
834
-	/**
835
-	 * Sets up the memo array for the creating model.
836
-	 */
837
-	private function maybe_boot() {
838
-		if ( ! isset( self::$memo[ get_called_class() ] ) ) {
839
-			self::$memo[ get_called_class() ] = array();
840
-		}
841
-	}
842
-
843
-	/**
844
-	 * Whether this Model uses an underlying WordPress object.
845
-	 *
846
-	 * @return bool
847
-	 */
848
-	protected function uses_wp_object() {
849
-		return $this instanceof UsesWordPressPost ||
850
-			$this instanceof UsesWordPressTerm;
851
-	}
26
+    /**
27
+     * Memoized values for class methods.
28
+     *
29
+     * @var array
30
+     */
31
+    private static $memo = array();
32
+
33
+    /**
34
+     * Model attributes.
35
+     *
36
+     * @var array
37
+     */
38
+    private $attributes = array(
39
+        'table'  => array(),
40
+        'object' => null,
41
+    );
42
+
43
+    /**
44
+     * Model's original attributes.
45
+     *
46
+     * @var array
47
+     */
48
+    private $original = array(
49
+        'table'  => array(),
50
+        'object' => null,
51
+    );
52
+
53
+    /**
54
+     * Properties which are allowed to be set on the model.
55
+     *
56
+     * If this array is empty, any attributes can be set on the model.
57
+     *
58
+     * @var string[]
59
+     */
60
+    protected $fillable = array();
61
+
62
+    /**
63
+     * Properties which cannot be automatically filled on the model.
64
+     *
65
+     * If the model is unguarded, these properties can be filled.
66
+     *
67
+     * @var array
68
+     */
69
+    protected $guarded = array();
70
+
71
+    /**
72
+     * Properties which should not be serialized.
73
+     *
74
+     * @var array
75
+     */
76
+    protected $hidden = array();
77
+
78
+    /**
79
+     * Properties which should be serialized.
80
+     *
81
+     * @var array
82
+     */
83
+    protected $visible = array();
84
+
85
+    /**
86
+     * Relations saved on the Model.
87
+     *
88
+     * @var array
89
+     */
90
+    protected $related = array();
91
+
92
+    /**
93
+     * Whether the model's properties are guarded.
94
+     *
95
+     * When false, allows guarded properties to be filled.
96
+     *
97
+     * @var bool
98
+     */
99
+    protected $is_guarded = true;
100
+
101
+    /**
102
+     * Whether the Model is having its relations filled.
103
+     *
104
+     * @var bool
105
+     */
106
+    protected $filling = false;
107
+
108
+    /**
109
+     * Constructs a new model with provided attributes.
110
+     *
111
+     * If 'object' is passed as one of the attributes, the underlying post
112
+     * will be overwritten.
113
+     *
114
+     * @param array <string, mixed> $attributes
115
+     */
116
+    public function __construct( array $attributes = array() ) {
117
+        $this->maybe_boot();
118
+        $this->sync_original();
119
+
120
+        if ( $this->uses_wp_object() ) {
121
+            $this->create_wp_object();
122
+        }
123
+
124
+        $this->refresh( $attributes );
125
+    }
126
+
127
+    /**
128
+     * Refreshes the model's current attributes with the provided array.
129
+     *
130
+     * The model's attributes will match what was provided in the array,
131
+     * and any attributes not passed
132
+     *
133
+     * @param array $attributes
134
+     */
135
+    public function refresh( array $attributes ) {
136
+        $this->clear();
137
+
138
+        foreach ( $attributes as $name => $value ) {
139
+            $this->set_attribute( $name, $value );
140
+        }
141
+    }
142
+
143
+    /**
144
+     * Get the model's table attributes.
145
+     *
146
+     * Returns the array of for the model that will either need to be
147
+     * saved in postmeta or a separate table.
148
+     *
149
+     * @return array
150
+     */
151
+    public function get_table_attributes() {
152
+        return $this->attributes['table'];
153
+    }
154
+
155
+    /**
156
+     * Get the model's original attributes.
157
+     *
158
+     * @return array
159
+     */
160
+    public function get_original_table_attributes() {
161
+        return $this->original['table'];
162
+    }
163
+
164
+    /**
165
+     * Retrieve an array of the attributes on the model
166
+     * that have changed compared to the model's
167
+     * original data.
168
+     *
169
+     * @return array
170
+     */
171
+    public function get_changed_table_attributes() {
172
+        $changed = array();
173
+
174
+        foreach ( $this->get_table_attributes() as $attribute ) {
175
+            if ( $this->get_attribute( $attribute ) !==
176
+                    $this->get_original_attribute( $attribute )
177
+            ) {
178
+                $changed[ $attribute ] = $this->get_attribute( $attribute );
179
+            }
180
+        }
181
+
182
+        return $changed;
183
+    }
184
+
185
+    /**
186
+     * Get the model's underlying post.
187
+     *
188
+     * Returns the underlying WP_Post object for the model, representing
189
+     * the data that will be save in the wp_posts table.
190
+     *
191
+     * @return false|WP_Post|WP_Term
192
+     */
193
+    public function get_underlying_wp_object() {
194
+        if ( isset( $this->attributes['object'] ) ) {
195
+            return $this->attributes['object'];
196
+        }
197
+
198
+        return false;
199
+    }
200
+
201
+    /**
202
+     * Get the model's original underlying post.
203
+     *
204
+     * @return WP_Post
205
+     */
206
+    public function get_original_underlying_wp_object() {
207
+        return $this->original['object'];
208
+    }
209
+
210
+    /**
211
+     * Get the model attributes on the WordPress object
212
+     * that have changed compared to the model's
213
+     * original attributes.
214
+     *
215
+     * @return array
216
+     */
217
+    public function get_changed_wp_object_attributes() {
218
+        $changed = array();
219
+
220
+        foreach ( $this->get_wp_object_keys() as $key ) {
221
+            if ( $this->get_attribute( $key ) !==
222
+                    $this->get_original_attribute( $key )
223
+            ) {
224
+                $changed[ $key ] = $this->get_attribute( $key );
225
+            }
226
+        }
227
+
228
+        return $changed;
229
+    }
230
+
231
+    /**
232
+     * Magic __set method.
233
+     *
234
+     * Passes the name and value to set_attribute, which is where the magic happens.
235
+     *
236
+     * @param string $name
237
+     * @param mixed  $value
238
+     */
239
+    public function __set( $name, $value ) {
240
+        $this->set_attribute( $name, $value );
241
+    }
242
+
243
+    /**
244
+     * Sets the model attributes.
245
+     *
246
+     * Checks whether the model attribute can be set, check if it
247
+     * maps to the WP_Post property, otherwise, assigns it to the
248
+     * table attribute array.
249
+     *
250
+     * @param string $name
251
+     * @param mixed  $value
252
+     *
253
+     * @return $this
254
+     *
255
+     * @throws GuardedPropertyException
256
+     */
257
+    public function set_attribute( $name, $value ) {
258
+        if ( 'object' === $name ) {
259
+            return $this->override_wp_object( $value );
260
+        }
261
+
262
+        if ( ! $this->is_fillable( $name ) ) {
263
+            throw new GuardedPropertyException;
264
+        }
265
+
266
+        if ( $method = $this->has_map_method( $name ) ) {
267
+            $this->attributes['object']->{$this->{$method}()} = $value;
268
+        } else {
269
+            $this->attributes['table'][ $name ] = $value;
270
+        }
271
+
272
+        return $this;
273
+    }
274
+
275
+    /**
276
+     * Retrieves all the attribute keys for the model.
277
+     *
278
+     * @return array
279
+     */
280
+    public function get_attribute_keys() {
281
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
282
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
283
+        }
284
+
285
+        return self::$memo[ get_called_class() ][ __METHOD__ ]
286
+            = array_merge(
287
+                $this->fillable,
288
+                $this->guarded,
289
+                $this->get_compute_methods(),
290
+                $this->get_related_methods()
291
+            );
292
+    }
293
+
294
+    /**
295
+     * Retrieves the attribute keys that aren't mapped to a post.
296
+     *
297
+     * @return array
298
+     */
299
+    public function get_table_keys() {
300
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
301
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
302
+        }
303
+
304
+        $keys = array();
305
+
306
+        foreach ( $this->get_attribute_keys() as $key ) {
307
+            if ( ! $this->has_map_method( $key ) &&
308
+                 ! $this->has_compute_method( $key ) &&
309
+                 ! $this->has_related_method( $key )
310
+            ) {
311
+                $keys[] = $key;
312
+            }
313
+        }
314
+
315
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
316
+    }
317
+
318
+    /**
319
+     * Retrieves the attribute keys that are mapped to a post.
320
+     *
321
+     * @return array
322
+     */
323
+    public function get_wp_object_keys() {
324
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
325
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
326
+        }
327
+
328
+        $keys = array();
329
+
330
+        foreach ( $this->get_attribute_keys() as $key ) {
331
+            if ( $this->has_map_method( $key ) ) {
332
+                $keys[] = $key;
333
+            }
334
+        }
335
+
336
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
337
+    }
338
+
339
+    /**
340
+     * Returns the model's keys that are computed at call time.
341
+     *
342
+     * @return array
343
+     */
344
+    public function get_computed_keys() {
345
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
346
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
347
+        }
348
+
349
+        $keys = array();
350
+
351
+        foreach ( $this->get_attribute_keys() as $key ) {
352
+            if ( $this->has_compute_method( $key ) ) {
353
+                $keys[] = $key;
354
+            }
355
+        }
356
+
357
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
358
+    }
359
+
360
+    /**
361
+     * Returns the model's keys that are related to other Models.
362
+     *
363
+     * @return array
364
+     */
365
+    public function get_related_keys() {
366
+        if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
367
+            return self::$memo[ get_called_class() ][ __METHOD__ ];
368
+        }
369
+
370
+        $keys = array();
371
+
372
+        foreach ( $this->get_attribute_keys() as $key ) {
373
+            if ( $this->has_related_method( $key ) ) {
374
+                $keys[] = $key;
375
+            }
376
+        }
377
+
378
+        return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
379
+    }
380
+
381
+    /**
382
+     * Serializes the model's public data into an array.
383
+     *
384
+     * @return array
385
+     */
386
+    public function serialize() {
387
+        $attributes = array();
388
+
389
+        if ( $this->visible ) {
390
+            // If visible attributes are set, we'll only reveal those.
391
+            foreach ( $this->visible as $key ) {
392
+                $attributes[ $key ] = $this->get_attribute( $key );
393
+            }
394
+        } elseif ( $this->hidden ) {
395
+            // If hidden attributes are set, we'll grab everything and hide those.
396
+            foreach ( $this->get_attribute_keys() as $key ) {
397
+                if ( ! in_array( $key, $this->hidden ) ) {
398
+                    $attributes[ $key ] = $this->get_attribute( $key );
399
+                }
400
+            }
401
+        } else {
402
+            // If nothing is hidden/visible, we'll grab and reveal everything.
403
+            foreach ( $this->get_attribute_keys() as $key ) {
404
+                $attributes[ $key ] = $this->get_attribute( $key );
405
+            }
406
+        }
407
+
408
+        return array_map( function ( $attribute ) {
409
+            if ( $attribute instanceof Serializes ) {
410
+                return $attribute->serialize();
411
+            }
412
+
413
+            return $attribute;
414
+        }, $attributes );
415
+    }
416
+
417
+    /**
418
+     * Syncs the current attributes to the model's original.
419
+     *
420
+     * @return $this
421
+     */
422
+    public function sync_original() {
423
+        $this->original = $this->attributes;
424
+
425
+        if ( $this->attributes['object'] ) {
426
+            $this->original['object'] = clone $this->attributes['object'];
427
+        }
428
+
429
+        return $this;
430
+    }
431
+
432
+    /**
433
+     * Checks if a given attribute is mass-fillable.
434
+     *
435
+     * Returns true if the attribute can be filled, false if it can't.
436
+     *
437
+     * @param string $name
438
+     *
439
+     * @return bool
440
+     */
441
+    private function is_fillable( $name ) {
442
+        // If this model isn't guarded, everything is fillable.
443
+        if ( ! $this->is_guarded ) {
444
+            return true;
445
+        }
446
+
447
+        // If it's in the fillable array, then it's fillable.
448
+        if ( in_array( $name, $this->fillable ) ) {
449
+            return true;
450
+        }
451
+
452
+        // If it's explicitly guarded, then it's not fillable.
453
+        if ( in_array( $name, $this->guarded ) ) {
454
+            return false;
455
+        }
456
+
457
+        // If fillable hasn't been defined, then everything else fillable.
458
+        return ! $this->fillable;
459
+    }
460
+
461
+    /**
462
+     * Overrides the current WP_Post with a provided one.
463
+     *
464
+     * Resets the post's default values and stores it in the attributes.
465
+     *
466
+     * @param WP_Post $value
467
+     *
468
+     * @return $this
469
+     */
470
+    private function override_wp_object( $value ) {
471
+        $this->attributes['object'] = $this->set_wp_object_constants( $value );
472
+
473
+        return $this;
474
+    }
475
+
476
+    /**
477
+     * Create and set with a new blank post.
478
+     *
479
+     * Creates a new WP_Post object, assigns it the default attributes,
480
+     * and stores it in the attributes.
481
+     *
482
+     * @throws LogicException
483
+     */
484
+    private function create_wp_object() {
485
+        switch ( true ) {
486
+            case $this instanceof UsesWordPressPost:
487
+                $object = new WP_Post( (object) array() );
488
+                break;
489
+            case $this instanceof UsesWordPressTerm:
490
+                $object = new WP_Term( (object) array() );
491
+                break;
492
+            default:
493
+                throw new LogicException;
494
+                break;
495
+        }
496
+
497
+        $this->attributes['object'] = $this->set_wp_object_constants( $object );
498
+    }
499
+
500
+    /**
501
+     * Enforces values on the post that can't change.
502
+     *
503
+     * Primarily, this is used to make sure the post_type always maps
504
+     * to the model's "$type" property, but this can all be overridden
505
+     * by the developer to enforce other values in the model.
506
+     *
507
+     * @param object $object
508
+     *
509
+     * @return object
510
+     */
511
+    protected function set_wp_object_constants( $object ) {
512
+        if ( $this instanceof UsesWordPressPost ) {
513
+            $object->post_type = $this::get_post_type();
514
+        }
515
+
516
+        if ( $this instanceof UsesWordPressTerm ) {
517
+            $object->taxonomy = $this::get_taxonomy();
518
+        }
519
+
520
+        return $object;
521
+    }
522
+
523
+    /**
524
+     * Magic __get method.
525
+     *
526
+     * Passes the name and value to get_attribute, which is where the magic happens.
527
+     *
528
+     * @param string $name
529
+     *
530
+     * @return mixed
531
+     */
532
+    public function __get( $name ) {
533
+        return $this->get_attribute( $name );
534
+    }
535
+
536
+    /**
537
+     * Retrieves the model attribute.
538
+     *
539
+     * @param string $name
540
+     *
541
+     * @return mixed
542
+     *
543
+     * @throws PropertyDoesNotExistException If property isn't found.
544
+     */
545
+    public function get_attribute( $name ) {
546
+        if ( $method = $this->has_map_method( $name ) ) {
547
+            $value = $this->attributes['object']->{$this->{$method}()};
548
+        } elseif ( $method = $this->has_compute_method( $name ) ) {
549
+            $value = $this->{$method}();
550
+        } else if ( $method = $this->has_related_method( $name ) ) {
551
+            $value = $this->get_related( $this->{$method}()->get_sha() );
552
+        } else {
553
+            if ( ! isset( $this->attributes['table'][ $name ] ) ) {
554
+                throw new PropertyDoesNotExistException;
555
+            }
556
+
557
+            $value = $this->attributes['table'][ $name ];
558
+        }
559
+
560
+        return $value;
561
+    }
562
+
563
+    /**
564
+     * Retrieve the model's original attribute value.
565
+     *
566
+     * @param string $name
567
+     *
568
+     * @return mixed
569
+     *
570
+     * @throws PropertyDoesNotExistException If property isn't found.
571
+     */
572
+    public function get_original_attribute( $name ) {
573
+        $original = new static( $this->original );
574
+
575
+        return $original->get_attribute( $name );
576
+    }
577
+
578
+    /**
579
+     * Fetches the Model's primary ID, depending on the model
580
+     * implementation.
581
+     *
582
+     * @return int
583
+     *
584
+     * @throws LogicException
585
+     */
586
+    public function get_primary_id() {
587
+        if ( $this instanceof UsesWordPressPost ) {
588
+            return $this->get_underlying_wp_object()->ID;
589
+        }
590
+
591
+        if ( $this instanceof UsesWordPressTerm ) {
592
+            return $this->get_underlying_wp_object()->term_id;
593
+        }
594
+
595
+        // Model w/o wp_object not yet supported.
596
+        throw new LogicException;
597
+    }
598
+
599
+    /**
600
+     * Generates the table foreign key, depending on the model
601
+     * implementation.
602
+     *
603
+     * @return string
604
+     *
605
+     * @throws LogicException
606
+     */
607
+    public function get_foreign_key() {
608
+        if ( $this instanceof UsesWordPressPost ) {
609
+            return 'post_id';
610
+        }
611
+
612
+        // Model w/o wp_object not yet supported.
613
+        throw new LogicException;
614
+    }
615
+
616
+    /**
617
+     * Gets the related Model or Collection for the given sha.
618
+     *
619
+     * @param string $sha
620
+     *
621
+     * @return Model|Collection
622
+     */
623
+    public function get_related( $sha ) {
624
+        return $this->related[ $sha ];
625
+    }
626
+
627
+    /**
628
+     * Sets the related Model or Collection for the given sha.
629
+     *
630
+     * @param string           $sha
631
+     * @param Model|Collection $target
632
+     *
633
+     * @throws RuntimeException
634
+     */
635
+    public function set_related( $sha, $target ) {
636
+        if ( ! ( $target instanceof Model ) && ! ( $target instanceof Collection ) ) {
637
+            throw new RuntimeException;
638
+        }
639
+
640
+        $this->related[ $sha ] = $target;
641
+    }
642
+
643
+    /**
644
+     * Checks whether the attribute has a map method.
645
+     *
646
+     * This is used to determine whether the attribute maps to a
647
+     * property on the underlying WP_Post object. Returns the
648
+     * method if one exists, returns false if it doesn't.
649
+     *
650
+     * @param string $name
651
+     *
652
+     * @return false|string
653
+     */
654
+    protected function has_map_method( $name ) {
655
+        if ( method_exists( $this, $method = "map_{$name}" ) ) {
656
+            return $method;
657
+        }
658
+
659
+        return false;
660
+    }
661
+
662
+    /**
663
+     * Checks whether the attribute has a compute method.
664
+     *
665
+     * This is used to determine if the attribute should be computed
666
+     * from other attributes.
667
+     *
668
+     * @param string $name
669
+     *
670
+     * @return false|string
671
+     */
672
+    protected function has_compute_method( $name ) {
673
+        if ( method_exists( $this, $method = "compute_{$name}" ) ) {
674
+            return $method;
675
+        }
676
+
677
+        return false;
678
+    }
679
+
680
+    /**
681
+     * Checks whether the attribute has a compute method.
682
+     *
683
+     * This is used to determine if the attribute should be computed
684
+     * from other attributes.
685
+     *
686
+     * @param string $name
687
+     *
688
+     * @return false|string
689
+     */
690
+    protected function has_related_method( $name ) {
691
+        if ( method_exists( $this, $method = "related_{$name}" ) ) {
692
+            return $method;
693
+        }
694
+
695
+        return false;
696
+    }
697
+
698
+    /**
699
+     * Clears all the current attributes from the model.
700
+     *
701
+     * This does not touch the model's original attributes, and will
702
+     * only clear fillable attributes, unless the model is unguarded.
703
+     *
704
+     * @return $this
705
+     */
706
+    public function clear() {
707
+        $keys = $this->get_attribute_keys();
708
+
709
+        foreach ( $keys as $key ) {
710
+            try {
711
+                $this->set_attribute( $key, null );
712
+            } catch ( GuardedPropertyException $e ) {
713
+                // We won't clear out guarded attributes.
714
+            }
715
+        }
716
+
717
+        return $this;
718
+    }
719
+
720
+    /**
721
+     * Unguards the model.
722
+     *
723
+     * Sets the model to be unguarded, allowing the filling of
724
+     * guarded attributes.
725
+     */
726
+    public function unguard() {
727
+        $this->is_guarded = false;
728
+    }
729
+
730
+    /**
731
+     * Reguards the model.
732
+     *
733
+     * Sets the model to be guarded, preventing filling of
734
+     * guarded attributes.
735
+     */
736
+    public function reguard() {
737
+        $this->is_guarded = true;
738
+    }
739
+
740
+    /**
741
+     * Retrieves all the compute methods on the model.
742
+     *
743
+     * @return array
744
+     */
745
+    protected function get_compute_methods() {
746
+        $methods = get_class_methods( get_called_class() );
747
+        $methods = array_filter( $methods, function ( $method ) {
748
+            return strrpos( $method, 'compute_', - strlen( $method ) ) !== false;
749
+        } );
750
+        $methods = array_map( function ( $method ) {
751
+            return substr( $method, strlen( 'compute_' ) );
752
+        }, $methods );
753
+
754
+        return $methods;
755
+    }
756
+
757
+    /**
758
+     * Retrieves all the related methods on the model.
759
+     *
760
+     * @return array
761
+     */
762
+    protected function get_related_methods() {
763
+        $methods = get_class_methods( get_called_class() );
764
+        $methods = array_filter( $methods, function ( $method ) {
765
+            return strrpos( $method, 'related_', - strlen( $method ) ) !== false;
766
+        } );
767
+        $methods = array_map( function ( $method ) {
768
+            return substr( $method, strlen( 'related_' ) );
769
+        }, $methods );
770
+
771
+        return $methods;
772
+    }
773
+
774
+    /**
775
+     * Returns whether this relation has already been filled on the model.
776
+     *
777
+     * @param string $relation
778
+     *
779
+     * @return bool
780
+     */
781
+    public function relation_is_filled( $relation ) {
782
+        $sha = $this
783
+            ->{$this->has_related_method( $relation )}()
784
+            ->get_sha();
785
+
786
+        return isset( $this->related[ $sha ] );
787
+    }
788
+
789
+    /**
790
+     * Returns whether the Model is currently having
791
+     * its relationships filled.
792
+     *
793
+     * @return bool
794
+     */
795
+    public function is_filling() {
796
+        return $this->filling;
797
+    }
798
+
799
+    /**
800
+     * Sets whether the Model is having its relationships filled.
801
+     *
802
+     * @param bool $is_filling
803
+     */
804
+    public function set_filling( $is_filling ) {
805
+        $this->filling = $is_filling;
806
+    }
807
+
808
+    /**
809
+     * Returns a HasMany relationship for the model.
810
+     *
811
+     * @param string $class
812
+     * @param string $type
813
+     * @param string $foreign_key
814
+     *
815
+     * @return HasMany
816
+     */
817
+    protected function has_many( $class, $type, $foreign_key ) {
818
+        return new HasMany( $this, $class, $type, $foreign_key );
819
+    }
820
+
821
+    /**
822
+     * Returns a BelongsToOne relationship for the model.
823
+     *
824
+     * @param string $class
825
+     * @param string $type
826
+     * @param string $local_key
827
+     *
828
+     * @return HasMany
829
+     */
830
+    protected function belongs_to_one( $class, $type, $local_key = '' ) {
831
+        return new BelongsToOne( $this, $class, $type, $local_key );
832
+    }
833
+
834
+    /**
835
+     * Sets up the memo array for the creating model.
836
+     */
837
+    private function maybe_boot() {
838
+        if ( ! isset( self::$memo[ get_called_class() ] ) ) {
839
+            self::$memo[ get_called_class() ] = array();
840
+        }
841
+    }
842
+
843
+    /**
844
+     * Whether this Model uses an underlying WordPress object.
845
+     *
846
+     * @return bool
847
+     */
848
+    protected function uses_wp_object() {
849
+        return $this instanceof UsesWordPressPost ||
850
+            $this instanceof UsesWordPressTerm;
851
+    }
852 852
 }
Please login to merge, or discard this patch.
src/Axolotl/Relationship/BelongsToOne.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -90,7 +90,7 @@
 block discarded – undo
90 90
 	/**
91 91
 	 * Gets the ID value for the target model to search by.
92 92
 	 *
93
-	 * @return int|false
93
+	 * @return integer
94 94
 	 *
95 95
 	 * @throws LogicException
96 96
 	 */
Please login to merge, or discard this patch.
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -13,110 +13,110 @@
 block discarded – undo
13 13
  * @subpackage Axolotl\Relationship
14 14
  */
15 15
 class BelongsToOne extends Root {
16
-	/**
17
-	 * Relationship's primary key name
18
-	 * on related model.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	protected $local_key = '';
23
-
24
-	/**
25
-	 * HasMany constructor.
26
-	 *
27
-	 * @param Model  $model
28
-	 * @param string $class
29
-	 * @param string $type
30
-	 * @param string $local_key
31
-	 */
32
-	public function __construct( Model $model, $class, $type, $local_key ) {
33
-		$this->local_key = $local_key;
34
-
35
-		parent::__construct( $model, $class, $type );
36
-	}
37
-
38
-	/**
39
-	 * {@inheritDoc}
40
-	 *
41
-	 * @param EntityManager $database
42
-	 *
43
-	 * @throws LogicException
44
-	 */
45
-	public function attach_relation( EntityManager $database ) {
46
-		if ( $this->get_model()->is_filling() ) {
47
-			return;
48
-		}
49
-
50
-		$this->get_model()->set_filling( true );
51
-
52
-		$id = $this->make_target_id();
53
-
54
-		if ( $id ) {
55
-			$target = $database->find(
56
-				$this->get_class(),
57
-				$id
58
-			);
59
-
60
-			$this->set_target( $target );
61
-		}
62
-
63
-		$this->get_model()->set_filling( false );
64
-	}
65
-
66
-	/**
67
-	 * {@inheritDoc}
68
-	 *
69
-	 * @return string
70
-	 */
71
-	public function get_sha() {
72
-		return sha1(
73
-			__CLASS__ .
74
-			get_class( $this->model ) .
75
-			$this->class .
76
-			$this->type .
77
-			$this->local_key
78
-		);
79
-	}
80
-
81
-	/**
82
-	 * Get the relationship's local key.
83
-	 *
84
-	 * @return string
85
-	 */
86
-	public function get_local_key() {
87
-		return $this->local_key;
88
-	}
89
-
90
-	/**
91
-	 * Gets the ID value for the target model to search by.
92
-	 *
93
-	 * @return int|false
94
-	 *
95
-	 * @throws LogicException
96
-	 */
97
-	protected function make_target_id() {
98
-		$class = $this->get_class();
99
-
100
-		switch ( $this->get_relationship_type() ) {
101
-			case 'post_post':
102
-				return $this->get_model()
103
-					->get_underlying_wp_object()
104
-					->{$this->local_key};
105
-			case 'post_term':
106
-				$terms = wp_get_post_terms(
107
-					$this->get_model()
108
-						->get_underlying_wp_object()
109
-						->ID,
110
-					$class::get_taxonomy()
111
-				);
112
-
113
-				if ( ! $terms || is_wp_error( $terms ) ) {
114
-					return false;
115
-				}
116
-
117
-				return $terms[0]->term_id;
118
-			default:
119
-				throw new LogicException;
120
-		}
121
-	}
16
+    /**
17
+     * Relationship's primary key name
18
+     * on related model.
19
+     *
20
+     * @var string
21
+     */
22
+    protected $local_key = '';
23
+
24
+    /**
25
+     * HasMany constructor.
26
+     *
27
+     * @param Model  $model
28
+     * @param string $class
29
+     * @param string $type
30
+     * @param string $local_key
31
+     */
32
+    public function __construct( Model $model, $class, $type, $local_key ) {
33
+        $this->local_key = $local_key;
34
+
35
+        parent::__construct( $model, $class, $type );
36
+    }
37
+
38
+    /**
39
+     * {@inheritDoc}
40
+     *
41
+     * @param EntityManager $database
42
+     *
43
+     * @throws LogicException
44
+     */
45
+    public function attach_relation( EntityManager $database ) {
46
+        if ( $this->get_model()->is_filling() ) {
47
+            return;
48
+        }
49
+
50
+        $this->get_model()->set_filling( true );
51
+
52
+        $id = $this->make_target_id();
53
+
54
+        if ( $id ) {
55
+            $target = $database->find(
56
+                $this->get_class(),
57
+                $id
58
+            );
59
+
60
+            $this->set_target( $target );
61
+        }
62
+
63
+        $this->get_model()->set_filling( false );
64
+    }
65
+
66
+    /**
67
+     * {@inheritDoc}
68
+     *
69
+     * @return string
70
+     */
71
+    public function get_sha() {
72
+        return sha1(
73
+            __CLASS__ .
74
+            get_class( $this->model ) .
75
+            $this->class .
76
+            $this->type .
77
+            $this->local_key
78
+        );
79
+    }
80
+
81
+    /**
82
+     * Get the relationship's local key.
83
+     *
84
+     * @return string
85
+     */
86
+    public function get_local_key() {
87
+        return $this->local_key;
88
+    }
89
+
90
+    /**
91
+     * Gets the ID value for the target model to search by.
92
+     *
93
+     * @return int|false
94
+     *
95
+     * @throws LogicException
96
+     */
97
+    protected function make_target_id() {
98
+        $class = $this->get_class();
99
+
100
+        switch ( $this->get_relationship_type() ) {
101
+            case 'post_post':
102
+                return $this->get_model()
103
+                    ->get_underlying_wp_object()
104
+                    ->{$this->local_key};
105
+            case 'post_term':
106
+                $terms = wp_get_post_terms(
107
+                    $this->get_model()
108
+                        ->get_underlying_wp_object()
109
+                        ->ID,
110
+                    $class::get_taxonomy()
111
+                );
112
+
113
+                if ( ! $terms || is_wp_error( $terms ) ) {
114
+                    return false;
115
+                }
116
+
117
+                return $terms[0]->term_id;
118
+            default:
119
+                throw new LogicException;
120
+        }
121
+    }
122 122
 }
Please login to merge, or discard this patch.
src/Axolotl/Repository/AbstractRepository.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -159,7 +159,7 @@
 block discarded – undo
159 159
 	/**
160 160
 	 * Fills the provided Model with attributes from its custom table.
161 161
 	 *
162
-	 * @param Model|UsesCustomTable $model
162
+	 * @param Model $model
163 163
 	 */
164 164
 	protected function fill_table_attrs_from_table( Model $model ) {
165 165
 		$sql[] = "SELECT * FROM {$this->make_table_name( $model )}";
Please login to merge, or discard this patch.
Indentation   +196 added lines, -196 removed lines patch added patch discarded remove patch
@@ -17,200 +17,200 @@
 block discarded – undo
17 17
  * @subpackage Axolotl\Repository
18 18
  */
19 19
 abstract class AbstractRepository {
20
-	/**
21
-	 * All models found by Repositories.
22
-	 *
23
-	 * @var array
24
-	 */
25
-	protected static $found = array();
26
-
27
-	/**
28
-	 * Reset the array of found model
29
-	 */
30
-	public static function free() {
31
-		self::$found = array();
32
-	}
33
-
34
-	/**
35
-	 * Retrieves the retrieved model by class and id.
36
-	 *
37
-	 * @param string $class
38
-	 * @param int    $id
39
-	 *
40
-	 * @return bool
41
-	 */
42
-	protected static function get_found( $class, $id ) {
43
-		if ( isset( self::$found[ $class ][ $id ] ) ) {
44
-			return self::$found[ $class ][ $id ];
45
-		}
46
-
47
-		return false;
48
-	}
49
-
50
-	/**
51
-	 * EntityManger service.
52
-	 *
53
-	 * @var EntityManager
54
-	 */
55
-	protected $database;
56
-
57
-	/**
58
-	 * Model class for this repository.
59
-	 *
60
-	 * @var string
61
-	 */
62
-	protected $class;
63
-
64
-	/**
65
-	 * EntityManager prefix.
66
-	 *
67
-	 * @var string
68
-	 */
69
-	protected $prefix;
70
-
71
-	/**
72
-	 * WP_Query instance.
73
-	 *
74
-	 * @var WP_Query
75
-	 */
76
-	protected $main;
77
-
78
-	/**
79
-	 * WordPress Database connection.
80
-	 *
81
-	 * @var wpdb
82
-	 */
83
-	protected $wpdb;
84
-
85
-	/**
86
-	 * AbstractRepository constructor.
87
-	 *
88
-	 * @param EntityManager $database
89
-	 * @param string        $class
90
-	 */
91
-	public function __construct( EntityManager $database, $class ) {
92
-		$this->database = $database;
93
-		$this->class    = $class;
94
-		$this->prefix   = $database->get_prefix();
95
-		$this->main     = $database->get_main_query();
96
-		$this->wpdb     = $database->get_wpdb();
97
-
98
-		if ( ! isset( self::$found[ $class ] ) ) {
99
-			self::$found[ $class ] = array();
100
-		}
101
-	}
102
-
103
-	/**
104
-	 * Get a single model of the repository class with the given ID.
105
-	 *
106
-	 * @param int $id ID of the model.
107
-	 *
108
-	 * @return Model|WP_Error
109
-	 */
110
-	abstract public function find( $id );
111
-
112
-	/**
113
-	 * Finds all the models of the repository class for the given params.
114
-	 *
115
-	 * @param array $params Params to constrain the find.
116
-	 *
117
-	 * @return Collection|WP_Error
118
-	 */
119
-	abstract public function find_by( array $params = array() );
120
-
121
-	/**
122
-	 * Create and saves a new model of the repository class
123
-	 * with the given data.
124
-	 *
125
-	 * @param array $data
126
-	 *
127
-	 * @return Model|WP_Error
128
-	 */
129
-	abstract public function create( array $data = array() );
130
-
131
-	/**
132
-	 * Updates a model with its latest data.
133
-	 *
134
-	 * @param Model $model
135
-	 *
136
-	 * @return Model|WP_Error
137
-	 */
138
-	abstract public function persist( Model $model );
139
-
140
-	/**
141
-	 * Delete the provided Model.
142
-	 *
143
-	 * @param Model $model
144
-	 * @param bool  $force
145
-	 *
146
-	 * @return Model|WP_Error
147
-	 */
148
-	abstract public function delete( Model $model, $force = false );
149
-
150
-	/**
151
-	 * Registers the found Model with the EntityManager.
152
-	 *
153
-	 * @param Model $model
154
-	 */
155
-	public function register_model( Model $model ) {
156
-		self::$found[ $this->class ][ $model->get_primary_id() ] = $model;
157
-	}
158
-
159
-	/**
160
-	 * Fills the provided Model with attributes from its custom table.
161
-	 *
162
-	 * @param Model|UsesCustomTable $model
163
-	 */
164
-	protected function fill_table_attrs_from_table( Model $model ) {
165
-		$sql[] = "SELECT * FROM {$this->make_table_name( $model )}";
166
-		$sql[] = "WHERE {$model->get_foreign_key()} = %d";
167
-
168
-		$sql = $this->wpdb->prepare(
169
-			implode( ' ', $sql ),
170
-			$model->get_primary_id()
171
-		);
172
-
173
-		$row = $this->wpdb->get_row( $sql, ARRAY_A );
174
-
175
-		$model->unguard();
176
-
177
-		foreach ( $row as $key => $value ) {
178
-			$model->set_attribute( $key, $value );
179
-		}
180
-
181
-		$model->reguard();
182
-	}
183
-
184
-	/**
185
-	 * Saves the Model's changed table attributes to its table.
186
-	 *
187
-	 * @param Model $model
188
-	 *
189
-	 * @throws LogicException
190
-	 */
191
-	protected function save_table_attributes_to_table( Model $model ) {
192
-		throw new LogicException;
193
-	}
194
-
195
-	/**
196
-	 * Deletes the Model's table attributes from its table.
197
-	 *
198
-	 * @param Model $model
199
-	 *
200
-	 * @throws LogicException
201
-	 */
202
-	protected function delete_table_attributes_from_table( Model $model ) {
203
-		throw new LogicException;
204
-	}
205
-
206
-	/**
207
-	 * Creates the table name string for the provided Model.
208
-	 *
209
-	 * @param UsesCustomTable $model
210
-	 *
211
-	 * @return string
212
-	 */
213
-	protected function make_table_name( UsesCustomTable $model ) {
214
-		return "{$this->wpdb->prefix}{$this->prefix}_{$model::get_table_name()}";
215
-	}
20
+    /**
21
+     * All models found by Repositories.
22
+     *
23
+     * @var array
24
+     */
25
+    protected static $found = array();
26
+
27
+    /**
28
+     * Reset the array of found model
29
+     */
30
+    public static function free() {
31
+        self::$found = array();
32
+    }
33
+
34
+    /**
35
+     * Retrieves the retrieved model by class and id.
36
+     *
37
+     * @param string $class
38
+     * @param int    $id
39
+     *
40
+     * @return bool
41
+     */
42
+    protected static function get_found( $class, $id ) {
43
+        if ( isset( self::$found[ $class ][ $id ] ) ) {
44
+            return self::$found[ $class ][ $id ];
45
+        }
46
+
47
+        return false;
48
+    }
49
+
50
+    /**
51
+     * EntityManger service.
52
+     *
53
+     * @var EntityManager
54
+     */
55
+    protected $database;
56
+
57
+    /**
58
+     * Model class for this repository.
59
+     *
60
+     * @var string
61
+     */
62
+    protected $class;
63
+
64
+    /**
65
+     * EntityManager prefix.
66
+     *
67
+     * @var string
68
+     */
69
+    protected $prefix;
70
+
71
+    /**
72
+     * WP_Query instance.
73
+     *
74
+     * @var WP_Query
75
+     */
76
+    protected $main;
77
+
78
+    /**
79
+     * WordPress Database connection.
80
+     *
81
+     * @var wpdb
82
+     */
83
+    protected $wpdb;
84
+
85
+    /**
86
+     * AbstractRepository constructor.
87
+     *
88
+     * @param EntityManager $database
89
+     * @param string        $class
90
+     */
91
+    public function __construct( EntityManager $database, $class ) {
92
+        $this->database = $database;
93
+        $this->class    = $class;
94
+        $this->prefix   = $database->get_prefix();
95
+        $this->main     = $database->get_main_query();
96
+        $this->wpdb     = $database->get_wpdb();
97
+
98
+        if ( ! isset( self::$found[ $class ] ) ) {
99
+            self::$found[ $class ] = array();
100
+        }
101
+    }
102
+
103
+    /**
104
+     * Get a single model of the repository class with the given ID.
105
+     *
106
+     * @param int $id ID of the model.
107
+     *
108
+     * @return Model|WP_Error
109
+     */
110
+    abstract public function find( $id );
111
+
112
+    /**
113
+     * Finds all the models of the repository class for the given params.
114
+     *
115
+     * @param array $params Params to constrain the find.
116
+     *
117
+     * @return Collection|WP_Error
118
+     */
119
+    abstract public function find_by( array $params = array() );
120
+
121
+    /**
122
+     * Create and saves a new model of the repository class
123
+     * with the given data.
124
+     *
125
+     * @param array $data
126
+     *
127
+     * @return Model|WP_Error
128
+     */
129
+    abstract public function create( array $data = array() );
130
+
131
+    /**
132
+     * Updates a model with its latest data.
133
+     *
134
+     * @param Model $model
135
+     *
136
+     * @return Model|WP_Error
137
+     */
138
+    abstract public function persist( Model $model );
139
+
140
+    /**
141
+     * Delete the provided Model.
142
+     *
143
+     * @param Model $model
144
+     * @param bool  $force
145
+     *
146
+     * @return Model|WP_Error
147
+     */
148
+    abstract public function delete( Model $model, $force = false );
149
+
150
+    /**
151
+     * Registers the found Model with the EntityManager.
152
+     *
153
+     * @param Model $model
154
+     */
155
+    public function register_model( Model $model ) {
156
+        self::$found[ $this->class ][ $model->get_primary_id() ] = $model;
157
+    }
158
+
159
+    /**
160
+     * Fills the provided Model with attributes from its custom table.
161
+     *
162
+     * @param Model|UsesCustomTable $model
163
+     */
164
+    protected function fill_table_attrs_from_table( Model $model ) {
165
+        $sql[] = "SELECT * FROM {$this->make_table_name( $model )}";
166
+        $sql[] = "WHERE {$model->get_foreign_key()} = %d";
167
+
168
+        $sql = $this->wpdb->prepare(
169
+            implode( ' ', $sql ),
170
+            $model->get_primary_id()
171
+        );
172
+
173
+        $row = $this->wpdb->get_row( $sql, ARRAY_A );
174
+
175
+        $model->unguard();
176
+
177
+        foreach ( $row as $key => $value ) {
178
+            $model->set_attribute( $key, $value );
179
+        }
180
+
181
+        $model->reguard();
182
+    }
183
+
184
+    /**
185
+     * Saves the Model's changed table attributes to its table.
186
+     *
187
+     * @param Model $model
188
+     *
189
+     * @throws LogicException
190
+     */
191
+    protected function save_table_attributes_to_table( Model $model ) {
192
+        throw new LogicException;
193
+    }
194
+
195
+    /**
196
+     * Deletes the Model's table attributes from its table.
197
+     *
198
+     * @param Model $model
199
+     *
200
+     * @throws LogicException
201
+     */
202
+    protected function delete_table_attributes_from_table( Model $model ) {
203
+        throw new LogicException;
204
+    }
205
+
206
+    /**
207
+     * Creates the table name string for the provided Model.
208
+     *
209
+     * @param UsesCustomTable $model
210
+     *
211
+     * @return string
212
+     */
213
+    protected function make_table_name( UsesCustomTable $model ) {
214
+        return "{$this->wpdb->prefix}{$this->prefix}_{$model::get_table_name()}";
215
+    }
216 216
 }
Please login to merge, or discard this patch.
src/Axolotl/Repository/AbstractWordPress.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -239,7 +239,7 @@
 block discarded – undo
239 239
 	 *
240 240
 	 * @param Model $model
241 241
 	 *
242
-	 * @return int|WP_Error
242
+	 * @return integer
243 243
 	 */
244 244
 	abstract protected function save_wp_object( Model $model );
245 245
 
Please login to merge, or discard this patch.
Indentation   +276 added lines, -276 removed lines patch added patch discarded remove patch
@@ -16,280 +16,280 @@
 block discarded – undo
16 16
  * @subpackage Axolotl\Repository
17 17
  */
18 18
 abstract class AbstractWordPress extends AbstractRepository {
19
-	/**
20
-	 * {@inheritDoc}
21
-	 *
22
-	 * @param int $id
23
-	 */
24
-	public function find( $id ) {
25
-		if ( $model = self::get_found( $this->class, $id ) ) {
26
-			return $model;
27
-		}
28
-
29
-		$object = $this->get_wp_object_by_id( $id );
30
-
31
-		if ( ! $object ) {
32
-			return new WP_Error(
33
-				'not_found',
34
-				__( 'Entity not found', 'jaxion' )
35
-			);
36
-		}
37
-
38
-		return $this->make_model_from_wp_object( $object );
39
-	}
40
-
41
-	/**
42
-	 * {@inheritDoc}
43
-	 *
44
-	 * @param array $params
45
-	 *
46
-	 * @return Collection
47
-	 */
48
-	public function find_by( array $params = array() ) {
49
-		$collection = new Collection(
50
-			array(),
51
-			array( 'model' => $this->class )
52
-		);
53
-
54
-		foreach ( $this->get_wp_objects_by_params( $params ) as $object ) {
55
-			$collection->add(
56
-				$this->make_model_from_wp_object( $object )
57
-			);
58
-		}
59
-
60
-		return $collection;
61
-	}
62
-
63
-	/**
64
-	 * {@inheritDoc}
65
-	 *
66
-	 * @param array $data
67
-	 *
68
-	 * @return Model|WP_Error
69
-	 */
70
-	public function create( array $data = array() ) {
71
-		/**
72
-		 * Model object.
73
-		 *
74
-		 * @var Model $model
75
-		 */
76
-		$model = new $this->class( $data );
77
-
78
-		$id = $this->save_wp_object( $model );
79
-
80
-		if ( is_wp_error( $id ) ) {
81
-			return $id;
82
-		}
83
-
84
-		$model->set_attribute(
85
-			'object',
86
-			$this->get_wp_object_by_id( $id )
87
-		);
88
-
89
-		$this->save_table_attributes( $model );
90
-		$model->sync_original();
91
-
92
-		return $model;
93
-	}
94
-
95
-	/**
96
-	 * {@inheritDoc}
97
-	 *
98
-	 * @param Model $model
99
-	 *
100
-	 * @return Model|WP_Error
101
-	 */
102
-	public function persist( Model $model ) {
103
-		$id = $this->save_wp_object( $model );
104
-
105
-		if ( is_wp_error( $id ) ) {
106
-			return $id;
107
-		}
108
-
109
-		$model->set_attribute(
110
-			'object',
111
-			$this->get_wp_object_by_id( $id )
112
-		);
113
-
114
-		$this->save_table_attributes( $model );
115
-		$model->sync_original();
116
-
117
-		return $model;
118
-	}
119
-
120
-	/**
121
-	 * {@inheritDoc}
122
-	 *
123
-	 * @param Model $model
124
-	 * @param bool  $force
125
-	 *
126
-	 * @return Model|WP_Error
127
-	 */
128
-	public function delete( Model $model, $force = false ) {
129
-		$result = $this->delete_wp_object( $model, $force );
130
-
131
-		if ( is_wp_error( $result ) ) {
132
-			return $result;
133
-		}
134
-
135
-		if ( $force ) {
136
-			$this->delete_table_attributes( $model );
137
-		}
138
-
139
-		return $model;
140
-	}
141
-
142
-	/**
143
-	 * Builds the provided model class from the a wp object.
144
-	 *
145
-	 * @param WP_Post|WP_Term $object
146
-	 *
147
-	 * @return Model
148
-	 */
149
-	protected function make_model_from_wp_object( $object ) {
150
-		if ( $model = self::get_found( $this->class, $this->get_wp_object_id( $object ) ) ) {
151
-			return $model;
152
-		}
153
-
154
-		/**
155
-		 * Found Model.
156
-		 *
157
-		 * @var Model $model
158
-		 */
159
-		$model = new $this->class( array( 'object' => $object ) );
160
-
161
-		if ( $model instanceof UsesCustomTable ) {
162
-			$this->fill_table_attrs_from_table( $model );
163
-		} else {
164
-			$this->fill_table_attrs_from_meta( $model );
165
-		}
166
-
167
-		return $model;
168
-	}
169
-
170
-	/**
171
-	 * Save the table attributes for the provided model
172
-	 * to either a custom table or the Model's object's
173
-	 * metadata.
174
-	 *
175
-	 * @param Model $model
176
-	 */
177
-	protected function save_table_attributes( Model $model ) {
178
-		if ( $model instanceof UsesCustomTable ) {
179
-			$this->save_table_attributes_to_table( $model );
180
-		} else {
181
-			$this->save_table_attributes_to_meta( $model );
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * Delete the table attributes for the provided model
187
-	 * from either a custom table or the Model's object's
188
-	 * metadata.
189
-	 *
190
-	 * @param Model $model
191
-	 */
192
-	protected function delete_table_attributes( Model $model ) {
193
-		if ( $model instanceof UsesCustomTable ) {
194
-			$this->delete_table_attributes_from_table( $model );
195
-		} else {
196
-			$this->delete_table_attributes_from_meta( $model );
197
-		}
198
-	}
199
-
200
-	/**
201
-	 * Generates the unique meta key for
202
-	 * a WordPress model's data.
203
-	 *
204
-	 * @param string $key
205
-	 *
206
-	 * @return string
207
-	 */
208
-	protected function make_meta_key( $key ) {
209
-		return "_{$this->prefix}_{$key}";
210
-	}
211
-
212
-	/**
213
-	 * Retrieves an WordPress object by the provided id.
214
-	 *
215
-	 * @param int $id
216
-	 *
217
-	 * @return object|false
218
-	 */
219
-	abstract protected function get_wp_object_by_id( $id );
220
-
221
-	/**
222
-	 * Retrieves an array of WordPress objects by the provided params.
223
-	 *
224
-	 * @param array $params
225
-	 *
226
-	 * @return array
227
-	 */
228
-	abstract protected function get_wp_objects_by_params( $params );
229
-
230
-	/**
231
-	 * Fills the provided Model with its meta attributes.
232
-	 *
233
-	 * @param Model $model
234
-	 */
235
-	abstract protected function fill_table_attrs_from_meta( Model $model );
236
-
237
-	/**
238
-	 * Save the provided WordPress object to the WordPress database.
239
-	 *
240
-	 * @param Model $model
241
-	 *
242
-	 * @return int|WP_Error
243
-	 */
244
-	abstract protected function save_wp_object( Model $model );
245
-
246
-	/**
247
-	 * Saves the provided Model's changed table attributes
248
-	 * to the appropriate meta table.
249
-	 *
250
-	 * @param Model $model
251
-	 */
252
-	abstract protected function save_table_attributes_to_meta( Model $model );
253
-
254
-	/**
255
-	 * Delete the WordPress object associated with the
256
-	 * provided model. If `$force` is true, the object
257
-	 * will be deleted directly rather than
258
-	 *
259
-	 * @param Model $model
260
-	 * @param bool  $force
261
-	 */
262
-	abstract protected function delete_wp_object( Model $model, $force = false );
263
-
264
-	/**
265
-	 * Delete all of the metadata associated with the object on
266
-	 * the provided model.
267
-	 *
268
-	 * @param Model $model
269
-	 */
270
-	abstract protected function delete_table_attributes_from_meta( Model $model );
271
-
272
-	/**
273
-	 * Gets the primary ID of the provided WordPress object.
274
-	 *
275
-	 * @param WP_Post|WP_Term $object
276
-	 *
277
-	 * @return int|null
278
-	 *
279
-	 * @throws LogicException
280
-	 */
281
-	protected function get_wp_object_id( $object ) {
282
-		if ( $object instanceof WP_Post ) {
283
-			return $object->ID;
284
-		}
285
-
286
-		if ( $object instanceof WP_Term ||
287
-		     // This is required for WP4.3- compatibility, when they returned stdClass.
288
-		     ( $object instanceof \stdClass && property_exists( $object, 'term_id' ) )
289
-		) {
290
-			return $object->term_id;
291
-		}
292
-
293
-		throw new LogicException;
294
-	}
19
+    /**
20
+     * {@inheritDoc}
21
+     *
22
+     * @param int $id
23
+     */
24
+    public function find( $id ) {
25
+        if ( $model = self::get_found( $this->class, $id ) ) {
26
+            return $model;
27
+        }
28
+
29
+        $object = $this->get_wp_object_by_id( $id );
30
+
31
+        if ( ! $object ) {
32
+            return new WP_Error(
33
+                'not_found',
34
+                __( 'Entity not found', 'jaxion' )
35
+            );
36
+        }
37
+
38
+        return $this->make_model_from_wp_object( $object );
39
+    }
40
+
41
+    /**
42
+     * {@inheritDoc}
43
+     *
44
+     * @param array $params
45
+     *
46
+     * @return Collection
47
+     */
48
+    public function find_by( array $params = array() ) {
49
+        $collection = new Collection(
50
+            array(),
51
+            array( 'model' => $this->class )
52
+        );
53
+
54
+        foreach ( $this->get_wp_objects_by_params( $params ) as $object ) {
55
+            $collection->add(
56
+                $this->make_model_from_wp_object( $object )
57
+            );
58
+        }
59
+
60
+        return $collection;
61
+    }
62
+
63
+    /**
64
+     * {@inheritDoc}
65
+     *
66
+     * @param array $data
67
+     *
68
+     * @return Model|WP_Error
69
+     */
70
+    public function create( array $data = array() ) {
71
+        /**
72
+         * Model object.
73
+         *
74
+         * @var Model $model
75
+         */
76
+        $model = new $this->class( $data );
77
+
78
+        $id = $this->save_wp_object( $model );
79
+
80
+        if ( is_wp_error( $id ) ) {
81
+            return $id;
82
+        }
83
+
84
+        $model->set_attribute(
85
+            'object',
86
+            $this->get_wp_object_by_id( $id )
87
+        );
88
+
89
+        $this->save_table_attributes( $model );
90
+        $model->sync_original();
91
+
92
+        return $model;
93
+    }
94
+
95
+    /**
96
+     * {@inheritDoc}
97
+     *
98
+     * @param Model $model
99
+     *
100
+     * @return Model|WP_Error
101
+     */
102
+    public function persist( Model $model ) {
103
+        $id = $this->save_wp_object( $model );
104
+
105
+        if ( is_wp_error( $id ) ) {
106
+            return $id;
107
+        }
108
+
109
+        $model->set_attribute(
110
+            'object',
111
+            $this->get_wp_object_by_id( $id )
112
+        );
113
+
114
+        $this->save_table_attributes( $model );
115
+        $model->sync_original();
116
+
117
+        return $model;
118
+    }
119
+
120
+    /**
121
+     * {@inheritDoc}
122
+     *
123
+     * @param Model $model
124
+     * @param bool  $force
125
+     *
126
+     * @return Model|WP_Error
127
+     */
128
+    public function delete( Model $model, $force = false ) {
129
+        $result = $this->delete_wp_object( $model, $force );
130
+
131
+        if ( is_wp_error( $result ) ) {
132
+            return $result;
133
+        }
134
+
135
+        if ( $force ) {
136
+            $this->delete_table_attributes( $model );
137
+        }
138
+
139
+        return $model;
140
+    }
141
+
142
+    /**
143
+     * Builds the provided model class from the a wp object.
144
+     *
145
+     * @param WP_Post|WP_Term $object
146
+     *
147
+     * @return Model
148
+     */
149
+    protected function make_model_from_wp_object( $object ) {
150
+        if ( $model = self::get_found( $this->class, $this->get_wp_object_id( $object ) ) ) {
151
+            return $model;
152
+        }
153
+
154
+        /**
155
+         * Found Model.
156
+         *
157
+         * @var Model $model
158
+         */
159
+        $model = new $this->class( array( 'object' => $object ) );
160
+
161
+        if ( $model instanceof UsesCustomTable ) {
162
+            $this->fill_table_attrs_from_table( $model );
163
+        } else {
164
+            $this->fill_table_attrs_from_meta( $model );
165
+        }
166
+
167
+        return $model;
168
+    }
169
+
170
+    /**
171
+     * Save the table attributes for the provided model
172
+     * to either a custom table or the Model's object's
173
+     * metadata.
174
+     *
175
+     * @param Model $model
176
+     */
177
+    protected function save_table_attributes( Model $model ) {
178
+        if ( $model instanceof UsesCustomTable ) {
179
+            $this->save_table_attributes_to_table( $model );
180
+        } else {
181
+            $this->save_table_attributes_to_meta( $model );
182
+        }
183
+    }
184
+
185
+    /**
186
+     * Delete the table attributes for the provided model
187
+     * from either a custom table or the Model's object's
188
+     * metadata.
189
+     *
190
+     * @param Model $model
191
+     */
192
+    protected function delete_table_attributes( Model $model ) {
193
+        if ( $model instanceof UsesCustomTable ) {
194
+            $this->delete_table_attributes_from_table( $model );
195
+        } else {
196
+            $this->delete_table_attributes_from_meta( $model );
197
+        }
198
+    }
199
+
200
+    /**
201
+     * Generates the unique meta key for
202
+     * a WordPress model's data.
203
+     *
204
+     * @param string $key
205
+     *
206
+     * @return string
207
+     */
208
+    protected function make_meta_key( $key ) {
209
+        return "_{$this->prefix}_{$key}";
210
+    }
211
+
212
+    /**
213
+     * Retrieves an WordPress object by the provided id.
214
+     *
215
+     * @param int $id
216
+     *
217
+     * @return object|false
218
+     */
219
+    abstract protected function get_wp_object_by_id( $id );
220
+
221
+    /**
222
+     * Retrieves an array of WordPress objects by the provided params.
223
+     *
224
+     * @param array $params
225
+     *
226
+     * @return array
227
+     */
228
+    abstract protected function get_wp_objects_by_params( $params );
229
+
230
+    /**
231
+     * Fills the provided Model with its meta attributes.
232
+     *
233
+     * @param Model $model
234
+     */
235
+    abstract protected function fill_table_attrs_from_meta( Model $model );
236
+
237
+    /**
238
+     * Save the provided WordPress object to the WordPress database.
239
+     *
240
+     * @param Model $model
241
+     *
242
+     * @return int|WP_Error
243
+     */
244
+    abstract protected function save_wp_object( Model $model );
245
+
246
+    /**
247
+     * Saves the provided Model's changed table attributes
248
+     * to the appropriate meta table.
249
+     *
250
+     * @param Model $model
251
+     */
252
+    abstract protected function save_table_attributes_to_meta( Model $model );
253
+
254
+    /**
255
+     * Delete the WordPress object associated with the
256
+     * provided model. If `$force` is true, the object
257
+     * will be deleted directly rather than
258
+     *
259
+     * @param Model $model
260
+     * @param bool  $force
261
+     */
262
+    abstract protected function delete_wp_object( Model $model, $force = false );
263
+
264
+    /**
265
+     * Delete all of the metadata associated with the object on
266
+     * the provided model.
267
+     *
268
+     * @param Model $model
269
+     */
270
+    abstract protected function delete_table_attributes_from_meta( Model $model );
271
+
272
+    /**
273
+     * Gets the primary ID of the provided WordPress object.
274
+     *
275
+     * @param WP_Post|WP_Term $object
276
+     *
277
+     * @return int|null
278
+     *
279
+     * @throws LogicException
280
+     */
281
+    protected function get_wp_object_id( $object ) {
282
+        if ( $object instanceof WP_Post ) {
283
+            return $object->ID;
284
+        }
285
+
286
+        if ( $object instanceof WP_Term ||
287
+             // This is required for WP4.3- compatibility, when they returned stdClass.
288
+             ( $object instanceof \stdClass && property_exists( $object, 'term_id' ) )
289
+        ) {
290
+            return $object->term_id;
291
+        }
292
+
293
+        throw new LogicException;
294
+    }
295 295
 }
Please login to merge, or discard this patch.
src/Http/Filter.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -12,134 +12,134 @@
 block discarded – undo
12 12
  * @subpackage Http
13 13
  */
14 14
 class Filter implements FilterContract {
15
-	/**
16
-	 * Filter rules.
17
-	 *
18
-	 * @var array
19
-	 */
20
-	protected $rules;
21
-
22
-	/**
23
-	 * Instantiates a new filter with the provided rules array.
24
-	 *
25
-	 * @param array $rules
26
-	 */
27
-	public function __construct( $rules = array() ) {
28
-		$this->rules = $rules;
29
-	}
30
-
31
-	/**
32
-	 * Generates argument rules.
33
-	 *
34
-	 * Returns an array matching the WP-API format for argument rules,
35
-	 * including sanitization, validation, required, or defaults.
36
-	 *
37
-	 * @return array
38
-	 */
39
-	public function rules() {
40
-		$args = array();
41
-
42
-		foreach ( $this->rules as $arg => $validation ) {
43
-			if ( ! $validation || ! is_string( $validation ) ) {
44
-				continue;
45
-			}
46
-
47
-			$args[ $arg ] = $this->parse_validation( $validation );
48
-		}
49
-
50
-		return $args;
51
-	}
52
-
53
-	/**
54
-	 * Parses a validation string into a WP-API compatible rule.
55
-	 *
56
-	 * @param string $validation
57
-	 *
58
-	 * @return array
59
-	 *
60
-	 * @todo The next rule added needs to refactor this process.
61
-	 */
62
-	protected function parse_validation( $validation ) {
63
-		$validation = explode( '|', $validation );
64
-
65
-		$rules = array();
66
-
67
-		foreach ( $validation as $rule ) {
68
-			if ( 0 === strpos( $rule, 'default' ) ) {
69
-				$rule_arr = explode( ':', $rule );
70
-
71
-				$rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
72
-			}
73
-
74
-			if ( 0 === strpos( $rule, 'oneof' ) ) {
75
-				list( $rule, $values ) = explode( ':', $rule );
76
-
77
-				$values   = explode( ',', $values );
78
-				$callback = function ( $value ) use ( $values ) {
79
-					if ( in_array( $value, $values, true ) ) {
80
-						return true;
81
-					}
82
-
83
-					return false;
84
-				};
85
-
86
-				$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
87
-			}
88
-
89
-			switch ( $rule ) {
90
-				case 'required':
91
-					$rules['required'] = true;
92
-					break;
93
-				case 'integer':
94
-					$callback                   = array( $this, 'validate_integer' );
95
-					$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
96
-
97
-					$callback                   = array( $this, 'make_integer' );
98
-					$rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
99
-					break;
100
-			}
101
-		}
102
-
103
-		return $rules;
104
-	}
105
-
106
-	/**
107
-	 * Validate that provided value is an integer.
108
-	 *
109
-	 * @param mixed $value
110
-	 *
111
-	 * @return bool
112
-	 */
113
-	public function validate_integer( $value ) {
114
-		return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
115
-	}
116
-
117
-	/**
118
-	 * Casts a provided value to an integer.
119
-	 *
120
-	 * @param mixed $value
121
-	 *
122
-	 * @return int
123
-	 */
124
-	public function make_integer( $value ) {
125
-		return (int) $value;
126
-	}
127
-
128
-	/**
129
-	 * Creates a new callback that connects the previous and next callback.
130
-	 *
131
-	 * @param callable $previous
132
-	 * @param callable $next
133
-	 *
134
-	 * @return \Closure;
135
-	 */
136
-	private function add_callback( $previous, $next ) {
137
-		return function ( $value ) use ( $previous, $next ) {
138
-			if ( call_user_func( $previous, $value ) ) {
139
-				return call_user_func( $next, $value );
140
-			}
141
-
142
-			return false;
143
-		};
144
-	}
15
+    /**
16
+     * Filter rules.
17
+     *
18
+     * @var array
19
+     */
20
+    protected $rules;
21
+
22
+    /**
23
+     * Instantiates a new filter with the provided rules array.
24
+     *
25
+     * @param array $rules
26
+     */
27
+    public function __construct( $rules = array() ) {
28
+        $this->rules = $rules;
29
+    }
30
+
31
+    /**
32
+     * Generates argument rules.
33
+     *
34
+     * Returns an array matching the WP-API format for argument rules,
35
+     * including sanitization, validation, required, or defaults.
36
+     *
37
+     * @return array
38
+     */
39
+    public function rules() {
40
+        $args = array();
41
+
42
+        foreach ( $this->rules as $arg => $validation ) {
43
+            if ( ! $validation || ! is_string( $validation ) ) {
44
+                continue;
45
+            }
46
+
47
+            $args[ $arg ] = $this->parse_validation( $validation );
48
+        }
49
+
50
+        return $args;
51
+    }
52
+
53
+    /**
54
+     * Parses a validation string into a WP-API compatible rule.
55
+     *
56
+     * @param string $validation
57
+     *
58
+     * @return array
59
+     *
60
+     * @todo The next rule added needs to refactor this process.
61
+     */
62
+    protected function parse_validation( $validation ) {
63
+        $validation = explode( '|', $validation );
64
+
65
+        $rules = array();
66
+
67
+        foreach ( $validation as $rule ) {
68
+            if ( 0 === strpos( $rule, 'default' ) ) {
69
+                $rule_arr = explode( ':', $rule );
70
+
71
+                $rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
72
+            }
73
+
74
+            if ( 0 === strpos( $rule, 'oneof' ) ) {
75
+                list( $rule, $values ) = explode( ':', $rule );
76
+
77
+                $values   = explode( ',', $values );
78
+                $callback = function ( $value ) use ( $values ) {
79
+                    if ( in_array( $value, $values, true ) ) {
80
+                        return true;
81
+                    }
82
+
83
+                    return false;
84
+                };
85
+
86
+                $rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
87
+            }
88
+
89
+            switch ( $rule ) {
90
+                case 'required':
91
+                    $rules['required'] = true;
92
+                    break;
93
+                case 'integer':
94
+                    $callback                   = array( $this, 'validate_integer' );
95
+                    $rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
96
+
97
+                    $callback                   = array( $this, 'make_integer' );
98
+                    $rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
99
+                    break;
100
+            }
101
+        }
102
+
103
+        return $rules;
104
+    }
105
+
106
+    /**
107
+     * Validate that provided value is an integer.
108
+     *
109
+     * @param mixed $value
110
+     *
111
+     * @return bool
112
+     */
113
+    public function validate_integer( $value ) {
114
+        return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
115
+    }
116
+
117
+    /**
118
+     * Casts a provided value to an integer.
119
+     *
120
+     * @param mixed $value
121
+     *
122
+     * @return int
123
+     */
124
+    public function make_integer( $value ) {
125
+        return (int) $value;
126
+    }
127
+
128
+    /**
129
+     * Creates a new callback that connects the previous and next callback.
130
+     *
131
+     * @param callable $previous
132
+     * @param callable $next
133
+     *
134
+     * @return \Closure;
135
+     */
136
+    private function add_callback( $previous, $next ) {
137
+        return function ( $value ) use ( $previous, $next ) {
138
+            if ( call_user_func( $previous, $value ) ) {
139
+                return call_user_func( $next, $value );
140
+            }
141
+
142
+            return false;
143
+        };
144
+    }
145 145
 }
Please login to merge, or discard this patch.