Completed
Pull Request — master (#3)
by James
02:59
created
src/Contract/Core/Container.php 2 patches
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.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -20,7 +20,7 @@  discard block
 block discarded – undo
20 20
 	 *
21 21
 	 * @return $this
22 22
 	 */
23
-	public function define( $alias, $definition );
23
+	public function define($alias, $definition);
24 24
 
25 25
 	/**
26 26
 	 * Defines a new singleton on the Container.
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
 	 *
34 34
 	 * @return $this
35 35
 	 */
36
-	public function share( $alias, $definition );
36
+	public function share($alias, $definition);
37 37
 
38 38
 	/**
39 39
 	 * Fetches the value for the provided alias.
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
 	 *
43 43
 	 * @return mixed
44 44
 	 */
45
-	public function fetch( $alias );
45
+	public function fetch($alias);
46 46
 
47 47
 	/**
48 48
 	 * Checks whether the provided alias exists on the container.
@@ -51,7 +51,7 @@  discard block
 block discarded – undo
51 51
 	 *
52 52
 	 * @return bool
53 53
 	 */
54
-	public function has( $alias );
54
+	public function has($alias);
55 55
 
56 56
 	/**
57 57
 	 * Removes the provided alias from the container.
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 	 *
61 61
 	 * @return bool
62 62
 	 */
63
-	public function remove( $alias );
63
+	public function remove($alias);
64 64
 
65 65
 	/**
66 66
 	 * Registers a service provider with the container.
@@ -72,5 +72,5 @@  discard block
 block discarded – undo
72 72
 	 *
73 73
 	 * @param ServiceProvider $provider
74 74
 	 */
75
-	public function register( ServiceProvider $provider );
75
+	public function register(ServiceProvider $provider);
76 76
 }
Please login to merge, or discard this patch.
src/Contract/Assets/Register.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -13,21 +13,21 @@
 block discarded – undo
13 13
 	 *
14 14
 	 * @param bool $debug
15 15
 	 */
16
-	public function set_debug( $debug );
16
+	public function set_debug($debug);
17 17
 
18 18
 	/**
19 19
 	 * Provides a method to register new scripts outside of the constructor.
20 20
 	 *
21 21
 	 * @param $script
22 22
 	 */
23
-	public function register_script( $script );
23
+	public function register_script($script);
24 24
 
25 25
 	/**
26 26
 	 * Provides a method to register new styles outside of the constructor.
27 27
 	 *
28 28
 	 * @param $style
29 29
 	 */
30
-	public function register_style( $style );
30
+	public function register_style($style);
31 31
 
32 32
 	/**
33 33
 	 * Enqueues the web & shared scripts on the Register.
Please login to merge, or discard this patch.
Indentation   +44 added lines, -44 removed lines patch added patch discarded remove patch
@@ -4,48 +4,48 @@
 block discarded – undo
4 4
 use Intraxia\Jaxion\Contract\Core\HasActions;
5 5
 
6 6
 interface Register extends HasActions {
7
-	/**
8
-	 * Enable debug mode for the enqueued assets.
9
-	 *
10
-	 * Debug mode will enqueue unminified versions of the registered assets.
11
-	 * Primarily, this is intended to be used along with WordPress's `SCRIPT_DEBUG`
12
-	 * constant, which enables unminified core assets to be enqueued.
13
-	 *
14
-	 * @param bool $debug
15
-	 */
16
-	public function set_debug( $debug );
17
-
18
-	/**
19
-	 * Provides a method to register new scripts outside of the constructor.
20
-	 *
21
-	 * @param array $script
22
-	 */
23
-	public function register_script( $script );
24
-
25
-	/**
26
-	 * Provides a method to register new styles outside of the constructor.
27
-	 *
28
-	 * @param array $style
29
-	 */
30
-	public function register_style( $style );
31
-
32
-	/**
33
-	 * Enqueues the web & shared scripts on the Register.
34
-	 */
35
-	public function enqueue_web_scripts();
36
-
37
-	/**
38
-	 * Enqueues the web & shared styles on the Register.
39
-	 */
40
-	public function enqueue_web_styles();
41
-
42
-	/**
43
-	 * Enqueues the admin & shared scripts on the Register.
44
-	 */
45
-	public function enqueue_admin_scripts();
46
-
47
-	/**
48
-	 * Enqueues the admin & shared styles on the Register.
49
-	 */
50
-	public function enqueue_admin_styles();
7
+    /**
8
+     * Enable debug mode for the enqueued assets.
9
+     *
10
+     * Debug mode will enqueue unminified versions of the registered assets.
11
+     * Primarily, this is intended to be used along with WordPress's `SCRIPT_DEBUG`
12
+     * constant, which enables unminified core assets to be enqueued.
13
+     *
14
+     * @param bool $debug
15
+     */
16
+    public function set_debug( $debug );
17
+
18
+    /**
19
+     * Provides a method to register new scripts outside of the constructor.
20
+     *
21
+     * @param array $script
22
+     */
23
+    public function register_script( $script );
24
+
25
+    /**
26
+     * Provides a method to register new styles outside of the constructor.
27
+     *
28
+     * @param array $style
29
+     */
30
+    public function register_style( $style );
31
+
32
+    /**
33
+     * Enqueues the web & shared scripts on the Register.
34
+     */
35
+    public function enqueue_web_scripts();
36
+
37
+    /**
38
+     * Enqueues the web & shared styles on the Register.
39
+     */
40
+    public function enqueue_web_styles();
41
+
42
+    /**
43
+     * Enqueues the admin & shared scripts on the Register.
44
+     */
45
+    public function enqueue_admin_scripts();
46
+
47
+    /**
48
+     * Enqueues the admin & shared styles on the Register.
49
+     */
50
+    public function enqueue_admin_styles();
51 51
 }
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/Router.php 2 patches
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
 	 *
72 72
 	 * @param string $vendor
73 73
 	 */
74
-	public function set_vendor( $vendor ) {
74
+	public function set_vendor($vendor) {
75 75
 		$this->vendor = $vendor;
76 76
 	}
77 77
 
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 	 *
81 81
 	 * @param int $version
82 82
 	 */
83
-	public function set_version( $version ) {
83
+	public function set_version($version) {
84 84
 		$this->version = $version;
85 85
 	}
86 86
 
@@ -94,15 +94,15 @@  discard block
 block discarded – undo
94 94
 	 * @throws VersionNotSetException
95 95
 	 */
96 96
 	public function register() {
97
-		if ( ! $this->vendor ) {
97
+		if (!$this->vendor) {
98 98
 			throw new VendorNotSetException;
99 99
 		}
100 100
 
101
-		if ( ! $this->version ) {
101
+		if (!$this->version) {
102 102
 			throw new VersionNotSetException;
103 103
 		}
104 104
 
105
-		foreach ( $this->endpoints as $endpoint ) {
105
+		foreach ($this->endpoints as $endpoint) {
106 106
 			register_rest_route(
107 107
 				$this->get_namespace(),
108 108
 				$endpoint->get_route(),
@@ -120,13 +120,13 @@  discard block
 block discarded – undo
120 120
 	 * @param array    $options
121 121
 	 * @param callable $callback
122 122
 	 */
123
-	public function group( array $options, $callback ) {
123
+	public function group(array $options, $callback) {
124 124
 		$router = new static;
125 125
 
126
-		call_user_func( $callback, $router );
126
+		call_user_func($callback, $router);
127 127
 
128
-		foreach ( $router->get_endpoints() as $endpoint ) {
129
-			$this->endpoints[] = $this->set_options( $endpoint, $options );
128
+		foreach ($router->get_endpoints() as $endpoint) {
129
+			$this->endpoints[] = $this->set_options($endpoint, $options);
130 130
 		}
131 131
 	}
132 132
 
@@ -145,26 +145,26 @@  discard block
 block discarded – undo
145 145
 	 * @throws MissingArgumentException
146 146
 	 * @throws InvalidArgumentException
147 147
 	 */
148
-	public function __call( $name, $arguments ) {
149
-		if ( ! in_array( $name, array_keys( $this->methods ) ) ) {
148
+	public function __call($name, $arguments) {
149
+		if (!in_array($name, array_keys($this->methods))) {
150 150
 			throw new UnknownMethodException;
151 151
 		}
152 152
 
153 153
 		// array_merge ensures we have 3 elements
154
-		list( $route, $callback, $options ) = array_merge( $arguments, array( null, null, null ) );
154
+		list($route, $callback, $options) = array_merge($arguments, array(null, null, null));
155 155
 
156
-		if ( ! $route || ! $callback ) {
156
+		if (!$route || !$callback) {
157 157
 			throw new MissingArgumentException;
158 158
 		}
159 159
 
160
-		if ( ! is_callable( $callback ) ) {
160
+		if (!is_callable($callback)) {
161 161
 			throw new InvalidArgumentException;
162 162
 		}
163 163
 
164
-		$endpoint = new Endpoint( $route, $this->methods[ $name ], $callback );
164
+		$endpoint = new Endpoint($route, $this->methods[$name], $callback);
165 165
 
166
-		if ( $options && is_array( $options ) ) {
167
-			$endpoint = $this->set_options( $endpoint, $options );
166
+		if ($options && is_array($options)) {
167
+			$endpoint = $this->set_options($endpoint, $options);
168 168
 		}
169 169
 
170 170
 		return $this->endpoints[] = $endpoint;
@@ -181,17 +181,17 @@  discard block
 block discarded – undo
181 181
 	 * @return Endpoint
182 182
 	 * @throws MalformedRouteException
183 183
 	 */
184
-	protected function set_options( Endpoint $endpoint, array $options ) {
185
-		if ( isset( $options['guard'] ) ) {
186
-			$endpoint->set_guard( $options['guard'] );
184
+	protected function set_options(Endpoint $endpoint, array $options) {
185
+		if (isset($options['guard'])) {
186
+			$endpoint->set_guard($options['guard']);
187 187
 		}
188 188
 
189
-		if ( isset( $options['filter'] ) ) {
190
-			$endpoint->set_filter( $options['filter'] );
189
+		if (isset($options['filter'])) {
190
+			$endpoint->set_filter($options['filter']);
191 191
 		}
192 192
 
193
-		if ( isset( $options['prefix'] ) ) {
194
-			$endpoint->set_prefix( $options['prefix'] );
193
+		if (isset($options['prefix'])) {
194
+			$endpoint->set_prefix($options['prefix']);
195 195
 		}
196 196
 
197 197
 		return $endpoint;
Please login to merge, or discard this patch.
Indentation   +206 added lines, -206 removed lines patch added patch discarded remove patch
@@ -21,210 +21,210 @@
 block discarded – undo
21 21
  * @method Endpoint all(string $route, callable $callback, array $options = array())
22 22
  */
23 23
 class Router implements HasActions {
24
-	/**
25
-	 * Resource's vendor prefix.
26
-	 *
27
-	 * @var string
28
-	 */
29
-	protected $vendor;
30
-
31
-	/**
32
-	 * Resource's version.
33
-	 *
34
-	 * @var int
35
-	 */
36
-	protected $version;
37
-
38
-	/**
39
-	 * Valid methods and their HTTP verb(s).
40
-	 *
41
-	 * @var array
42
-	 */
43
-	private $methods = array(
44
-		'get'      => 'GET',
45
-		'post'     => 'POST',
46
-		'put'      => 'PUT',
47
-		'patch'    => 'PATCH',
48
-		'delete'   => 'DELETE',
49
-		'editable' => 'POST, PUT, PATCH',
50
-		'all'      => 'GET, POST, PUT, PATCH, DELETE',
51
-	);
52
-
53
-	/**
54
-	 * Endpoints registered for the resource.
55
-	 *
56
-	 * @var Endpoint[]
57
-	 */
58
-	protected $endpoints = array();
59
-
60
-	/**
61
-	 * Returns all the resource endpoints.
62
-	 *
63
-	 * @return Endpoint[]
64
-	 */
65
-	public function get_endpoints() {
66
-		return $this->endpoints;
67
-	}
68
-
69
-	/**
70
-	 * Sets the resource's vendor prefix.
71
-	 *
72
-	 * @param string $vendor
73
-	 *
74
-	 * @return $this
75
-	 */
76
-	public function set_vendor( $vendor ) {
77
-		$this->vendor = $vendor;
78
-
79
-		return $this;
80
-	}
81
-
82
-	/**
83
-	 * Sets the resource's version.
84
-	 *
85
-	 * @param int $version
86
-	 *
87
-	 * @return $this
88
-	 */
89
-	public function set_version( $version ) {
90
-		$this->version = $version;
91
-
92
-		return $this;
93
-	}
94
-
95
-	/**
96
-	 * Registers all of the routes with the WP-API.
97
-	 *
98
-	 * Runs on the `rest_api_init` hook. Registers all of the routes loaded
99
-	 * on the router into the WordPress REST API.
100
-	 *
101
-	 * @throws VendorNotSetException
102
-	 * @throws VersionNotSetException
103
-	 */
104
-	public function register() {
105
-		if ( ! $this->vendor ) {
106
-			throw new VendorNotSetException;
107
-		}
108
-
109
-		if ( ! $this->version ) {
110
-			throw new VersionNotSetException;
111
-		}
112
-
113
-		foreach ( $this->endpoints as $endpoint ) {
114
-			register_rest_route(
115
-				$this->get_namespace(),
116
-				$endpoint->get_route(),
117
-				$endpoint->get_options()
118
-			);
119
-		}
120
-	}
121
-
122
-	/**
123
-	 * Registers a set of routes with a shared set of options.
124
-	 *
125
-	 * Allows you to group routes together with shared set of options, including
126
-	 * a route prefix, shared guards, and common parameter validation or sanitization.
127
-	 *
128
-	 * @param array    $options
129
-	 * @param callable $callback
130
-	 */
131
-	public function group( array $options, $callback ) {
132
-		$router = new static;
133
-
134
-		call_user_func( $callback, $router );
135
-
136
-		foreach ( $router->get_endpoints() as $endpoint ) {
137
-			$this->endpoints[] = $this->set_options( $endpoint, $options );
138
-		}
139
-	}
140
-
141
-	/**
142
-	 * Magic __call method.
143
-	 *
144
-	 * All of the endpoints registration method calls pass through here. This validates whether the method
145
-	 * is a valid endpoint type to register, and creates a new endpoint with the passed options.
146
-	 *
147
-	 * @param string $name
148
-	 * @param array  $arguments
149
-	 *
150
-	 * @return Endpoint
151
-	 *
152
-	 * @throws UnknownMethodException
153
-	 * @throws MissingArgumentException
154
-	 * @throws InvalidArgumentException
155
-	 */
156
-	public function __call( $name, $arguments ) {
157
-		if ( ! in_array( $name, array_keys( $this->methods ) ) ) {
158
-			throw new UnknownMethodException;
159
-		}
160
-
161
-		// array_merge ensures we have 3 elements
162
-		list( $route, $callback, $options ) = array_merge( $arguments, array( null, null, null ) );
163
-
164
-		if ( ! $route || ! $callback ) {
165
-			throw new MissingArgumentException;
166
-		}
167
-
168
-		if ( ! is_callable( $callback ) ) {
169
-			throw new InvalidArgumentException;
170
-		}
171
-
172
-		$endpoint = new Endpoint( $route, $this->methods[ $name ], $callback );
173
-
174
-		if ( $options && is_array( $options ) ) {
175
-			$endpoint = $this->set_options( $endpoint, $options );
176
-		}
177
-
178
-		return $this->endpoints[] = $endpoint;
179
-	}
180
-
181
-	/**
182
-	 * Sets the passed options on the endpoint.
183
-	 *
184
-	 * Only sets endpoints matching setters in the Endpoint class.
185
-	 *
186
-	 * @param Endpoint $endpoint
187
-	 * @param array    $options
188
-	 *
189
-	 * @return Endpoint
190
-	 * @throws MalformedRouteException
191
-	 */
192
-	protected function set_options( Endpoint $endpoint, array $options ) {
193
-		if ( isset( $options['guard'] ) ) {
194
-			$endpoint->set_guard( $options['guard'] );
195
-		}
196
-
197
-		if ( isset( $options['filter'] ) ) {
198
-			$endpoint->set_filter( $options['filter'] );
199
-		}
200
-
201
-		if ( isset( $options['prefix'] ) ) {
202
-			$endpoint->set_prefix( $options['prefix'] );
203
-		}
204
-
205
-		return $endpoint;
206
-	}
207
-
208
-	/**
209
-	 * Generates the resource's namespace.
210
-	 *
211
-	 * @return string
212
-	 */
213
-	protected function get_namespace() {
214
-		return $this->vendor . '/v' . $this->version;
215
-	}
216
-
217
-	/**
218
-	 * {@inheritDoc}
219
-	 *
220
-	 * @return array[]
221
-	 */
222
-	public function action_hooks() {
223
-		return array(
224
-			array(
225
-				'method' => 'register',
226
-				'hook'   => 'rest_api_init',
227
-			),
228
-		);
229
-	}
24
+    /**
25
+     * Resource's vendor prefix.
26
+     *
27
+     * @var string
28
+     */
29
+    protected $vendor;
30
+
31
+    /**
32
+     * Resource's version.
33
+     *
34
+     * @var int
35
+     */
36
+    protected $version;
37
+
38
+    /**
39
+     * Valid methods and their HTTP verb(s).
40
+     *
41
+     * @var array
42
+     */
43
+    private $methods = array(
44
+        'get'      => 'GET',
45
+        'post'     => 'POST',
46
+        'put'      => 'PUT',
47
+        'patch'    => 'PATCH',
48
+        'delete'   => 'DELETE',
49
+        'editable' => 'POST, PUT, PATCH',
50
+        'all'      => 'GET, POST, PUT, PATCH, DELETE',
51
+    );
52
+
53
+    /**
54
+     * Endpoints registered for the resource.
55
+     *
56
+     * @var Endpoint[]
57
+     */
58
+    protected $endpoints = array();
59
+
60
+    /**
61
+     * Returns all the resource endpoints.
62
+     *
63
+     * @return Endpoint[]
64
+     */
65
+    public function get_endpoints() {
66
+        return $this->endpoints;
67
+    }
68
+
69
+    /**
70
+     * Sets the resource's vendor prefix.
71
+     *
72
+     * @param string $vendor
73
+     *
74
+     * @return $this
75
+     */
76
+    public function set_vendor( $vendor ) {
77
+        $this->vendor = $vendor;
78
+
79
+        return $this;
80
+    }
81
+
82
+    /**
83
+     * Sets the resource's version.
84
+     *
85
+     * @param int $version
86
+     *
87
+     * @return $this
88
+     */
89
+    public function set_version( $version ) {
90
+        $this->version = $version;
91
+
92
+        return $this;
93
+    }
94
+
95
+    /**
96
+     * Registers all of the routes with the WP-API.
97
+     *
98
+     * Runs on the `rest_api_init` hook. Registers all of the routes loaded
99
+     * on the router into the WordPress REST API.
100
+     *
101
+     * @throws VendorNotSetException
102
+     * @throws VersionNotSetException
103
+     */
104
+    public function register() {
105
+        if ( ! $this->vendor ) {
106
+            throw new VendorNotSetException;
107
+        }
108
+
109
+        if ( ! $this->version ) {
110
+            throw new VersionNotSetException;
111
+        }
112
+
113
+        foreach ( $this->endpoints as $endpoint ) {
114
+            register_rest_route(
115
+                $this->get_namespace(),
116
+                $endpoint->get_route(),
117
+                $endpoint->get_options()
118
+            );
119
+        }
120
+    }
121
+
122
+    /**
123
+     * Registers a set of routes with a shared set of options.
124
+     *
125
+     * Allows you to group routes together with shared set of options, including
126
+     * a route prefix, shared guards, and common parameter validation or sanitization.
127
+     *
128
+     * @param array    $options
129
+     * @param callable $callback
130
+     */
131
+    public function group( array $options, $callback ) {
132
+        $router = new static;
133
+
134
+        call_user_func( $callback, $router );
135
+
136
+        foreach ( $router->get_endpoints() as $endpoint ) {
137
+            $this->endpoints[] = $this->set_options( $endpoint, $options );
138
+        }
139
+    }
140
+
141
+    /**
142
+     * Magic __call method.
143
+     *
144
+     * All of the endpoints registration method calls pass through here. This validates whether the method
145
+     * is a valid endpoint type to register, and creates a new endpoint with the passed options.
146
+     *
147
+     * @param string $name
148
+     * @param array  $arguments
149
+     *
150
+     * @return Endpoint
151
+     *
152
+     * @throws UnknownMethodException
153
+     * @throws MissingArgumentException
154
+     * @throws InvalidArgumentException
155
+     */
156
+    public function __call( $name, $arguments ) {
157
+        if ( ! in_array( $name, array_keys( $this->methods ) ) ) {
158
+            throw new UnknownMethodException;
159
+        }
160
+
161
+        // array_merge ensures we have 3 elements
162
+        list( $route, $callback, $options ) = array_merge( $arguments, array( null, null, null ) );
163
+
164
+        if ( ! $route || ! $callback ) {
165
+            throw new MissingArgumentException;
166
+        }
167
+
168
+        if ( ! is_callable( $callback ) ) {
169
+            throw new InvalidArgumentException;
170
+        }
171
+
172
+        $endpoint = new Endpoint( $route, $this->methods[ $name ], $callback );
173
+
174
+        if ( $options && is_array( $options ) ) {
175
+            $endpoint = $this->set_options( $endpoint, $options );
176
+        }
177
+
178
+        return $this->endpoints[] = $endpoint;
179
+    }
180
+
181
+    /**
182
+     * Sets the passed options on the endpoint.
183
+     *
184
+     * Only sets endpoints matching setters in the Endpoint class.
185
+     *
186
+     * @param Endpoint $endpoint
187
+     * @param array    $options
188
+     *
189
+     * @return Endpoint
190
+     * @throws MalformedRouteException
191
+     */
192
+    protected function set_options( Endpoint $endpoint, array $options ) {
193
+        if ( isset( $options['guard'] ) ) {
194
+            $endpoint->set_guard( $options['guard'] );
195
+        }
196
+
197
+        if ( isset( $options['filter'] ) ) {
198
+            $endpoint->set_filter( $options['filter'] );
199
+        }
200
+
201
+        if ( isset( $options['prefix'] ) ) {
202
+            $endpoint->set_prefix( $options['prefix'] );
203
+        }
204
+
205
+        return $endpoint;
206
+    }
207
+
208
+    /**
209
+     * Generates the resource's namespace.
210
+     *
211
+     * @return string
212
+     */
213
+    protected function get_namespace() {
214
+        return $this->vendor . '/v' . $this->version;
215
+    }
216
+
217
+    /**
218
+     * {@inheritDoc}
219
+     *
220
+     * @return array[]
221
+     */
222
+    public function action_hooks() {
223
+        return array(
224
+            array(
225
+                'method' => 'register',
226
+                'hook'   => 'rest_api_init',
227
+            ),
228
+        );
229
+    }
230 230
 }
Please login to merge, or discard this patch.
src/Http/Endpoint.php 2 patches
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.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -65,8 +65,8 @@  discard block
 block discarded – undo
65 65
 	 *
66 66
 	 * @throws MalformedRouteException
67 67
 	 */
68
-	public function __construct( $route, $method, $callback ) {
69
-		if ( ! Str::starts_with( $route, '/' ) || Str::ends_with( $route, '/' ) ) {
68
+	public function __construct($route, $method, $callback) {
69
+		if (!Str::starts_with($route, '/') || Str::ends_with($route, '/')) {
70 70
 			throw new MalformedRouteException;
71 71
 		}
72 72
 
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 	 * @return string
84 84
 	 */
85 85
 	public function get_route() {
86
-		return ( $this->prefix ?: '' ) . $this->route;
86
+		return ($this->prefix ?: '') . $this->route;
87 87
 	}
88 88
 
89 89
 	/**
@@ -97,11 +97,11 @@  discard block
 block discarded – undo
97 97
 			'callback' => $this->callback,
98 98
 		);
99 99
 
100
-		if ( $this->guard ) {
101
-			$options['permission_callback'] = array( $this->guard, 'authorized' );
100
+		if ($this->guard) {
101
+			$options['permission_callback'] = array($this->guard, 'authorized');
102 102
 		}
103 103
 
104
-		if ( $this->filter ) {
104
+		if ($this->filter) {
105 105
 			$options['args'] = $this->filter->rules();
106 106
 		}
107 107
 
@@ -115,7 +115,7 @@  discard block
 block discarded – undo
115 115
 	 *
116 116
 	 * @return $this
117 117
 	 */
118
-	public function set_guard( GuardContract $guard ) {
118
+	public function set_guard(GuardContract $guard) {
119 119
 		$this->guard = $guard;
120 120
 
121 121
 		return $this;
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
 	 *
129 129
 	 * @return $this
130 130
 	 */
131
-	public function set_filter( FilterContract $filter ) {
131
+	public function set_filter(FilterContract $filter) {
132 132
 		$this->filter = $filter;
133 133
 
134 134
 		return $this;
@@ -142,8 +142,8 @@  discard block
 block discarded – undo
142 142
 	 * @return $this
143 143
 	 * @throws MalformedRouteException
144 144
 	 */
145
-	public function set_prefix( $prefix ) {
146
-		if ( ! Str::starts_with( $prefix, '/' ) || Str::ends_with( $prefix, '/' ) ) {
145
+	public function set_prefix($prefix) {
146
+		if (!Str::starts_with($prefix, '/') || Str::ends_with($prefix, '/')) {
147 147
 			throw new MalformedRouteException;
148 148
 		}
149 149
 
Please login to merge, or discard this patch.
src/Http/Guard.php 3 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -1,7 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 namespace Intraxia\Jaxion\Model;
3 3
 
4
-use Intraxia\Jaxion\Utility\Str;
5 4
 use stdClass;
6 5
 use WP_Post;
7 6
 
Please login to merge, or discard this patch.
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -15,86 +15,86 @@
 block discarded – undo
15 15
  * @subpackage Http
16 16
  */
17 17
 class Guard implements GuardContract {
18
-	/**
19
-	 * Default options.
20
-	 *
21
-	 * @var array
22
-	 */
23
-	protected $defaults = array(
24
-		'rule'     => 'public',
25
-		'callback' => false,
26
-	);
18
+    /**
19
+     * Default options.
20
+     *
21
+     * @var array
22
+     */
23
+    protected $defaults = array(
24
+        'rule'     => 'public',
25
+        'callback' => false,
26
+    );
27 27
 
28
-	/**
29
-	 * Guard options.
30
-	 *
31
-	 * @var array
32
-	 */
33
-	protected $options;
28
+    /**
29
+     * Guard options.
30
+     *
31
+     * @var array
32
+     */
33
+    protected $options;
34 34
 
35
-	/**
36
-	 * Instantiate a new Guard with provided options.
37
-	 *
38
-	 * @param array $options
39
-	 */
40
-	public function __construct( array $options = array() ) {
41
-		$this->options = $this->set_defaults( $options );
42
-	}
35
+    /**
36
+     * Instantiate a new Guard with provided options.
37
+     *
38
+     * @param array $options
39
+     */
40
+    public function __construct( array $options = array() ) {
41
+        $this->options = $this->set_defaults( $options );
42
+    }
43 43
 
44
-	/**
45
-	 * Validates whether the current user is authorized.
46
-	 *
47
-	 * @return true|WP_Error
48
-	 */
49
-	public function authorized() {
50
-		// if the rule is public, always authorized
51
-		if ( 'public' === $this->options['rule'] ) {
52
-			return true;
53
-		}
44
+    /**
45
+     * Validates whether the current user is authorized.
46
+     *
47
+     * @return true|WP_Error
48
+     */
49
+    public function authorized() {
50
+        // if the rule is public, always authorized
51
+        if ( 'public' === $this->options['rule'] ) {
52
+            return true;
53
+        }
54 54
 
55
-		// enable passing in callback
56
-		if ( 'callback' === $this->options['rule'] && is_callable( $this->options['callback'] ) ) {
57
-			return call_user_func( $this->options['callback'] );
58
-		}
55
+        // enable passing in callback
56
+        if ( 'callback' === $this->options['rule'] && is_callable( $this->options['callback'] ) ) {
57
+            return call_user_func( $this->options['callback'] );
58
+        }
59 59
 
60
-		// map rule to method
61
-		if ( method_exists( $this, $method = $this->options['rule'] ) ) {
62
-			return $this->{$method}();
63
-		}
60
+        // map rule to method
61
+        if ( method_exists( $this, $method = $this->options['rule'] ) ) {
62
+            return $this->{$method}();
63
+        }
64 64
 
65
-		// disable in rule is misconfigused
66
-		// @todo set up internal translations
67
-		// @todo also, this error message kinda sucks
68
-		return new WP_Error( '500', __( 'Guard failure', 'jaxion' ) );
69
-	}
65
+        // disable in rule is misconfigused
66
+        // @todo set up internal translations
67
+        // @todo also, this error message kinda sucks
68
+        return new WP_Error( '500', __( 'Guard failure', 'jaxion' ) );
69
+    }
70 70
 
71
-	/**
72
-	 * Checks whether the current user can edit other's posts.
73
-	 *
74
-	 * @return bool|WP_Error
75
-	 */
76
-	protected function can_edit_others_posts() {
77
-		return current_user_can( 'edit_others_posts' ) ?: new WP_Error( 'unauthorized', __( 'Unauthorized user', 'jaxion' ), array( 'status' => 401 ) );
78
-	}
71
+    /**
72
+     * Checks whether the current user can edit other's posts.
73
+     *
74
+     * @return bool|WP_Error
75
+     */
76
+    protected function can_edit_others_posts() {
77
+        return current_user_can( 'edit_others_posts' ) ?: new WP_Error( 'unauthorized', __( 'Unauthorized user', 'jaxion' ), array( 'status' => 401 ) );
78
+    }
79 79
 
80
-	/**
81
-	 * Checks whether the user is currently logged in.
82
-	 *
83
-	 * @return bool|WP_Error
84
-	 */
85
-	protected function user_logged_in() {
86
-		return is_user_logged_in() ?: new WP_Error( 'unauthorized', __( 'Unauthorized user', 'jaxion' ), array( 'status' => 401 ) );
87
-	}
80
+    /**
81
+     * Checks whether the user is currently logged in.
82
+     *
83
+     * @return bool|WP_Error
84
+     */
85
+    protected function user_logged_in() {
86
+        return is_user_logged_in() ?: new WP_Error( 'unauthorized', __( 'Unauthorized user', 'jaxion' ), array( 'status' => 401 ) );
87
+    }
88 88
 
89
-	/**
90
-	 * Sets the default params for the Guard options.
91
-	 *
92
-	 * @param array $options
93
-	 *
94
-	 * @return array
95
-	 */
96
-	protected function set_defaults( $options ) {
97
-		// these are the valid options
98
-		return wp_parse_args( $options, $this->defaults );
99
-	}
89
+    /**
90
+     * Sets the default params for the Guard options.
91
+     *
92
+     * @param array $options
93
+     *
94
+     * @return array
95
+     */
96
+    protected function set_defaults( $options ) {
97
+        // these are the valid options
98
+        return wp_parse_args( $options, $this->defaults );
99
+    }
100 100
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -37,8 +37,8 @@  discard block
 block discarded – undo
37 37
 	 *
38 38
 	 * @param array $options
39 39
 	 */
40
-	public function __construct( array $options = array() ) {
41
-		$this->options = $this->set_defaults( $options );
40
+	public function __construct(array $options = array()) {
41
+		$this->options = $this->set_defaults($options);
42 42
 	}
43 43
 
44 44
 	/**
@@ -48,24 +48,24 @@  discard block
 block discarded – undo
48 48
 	 */
49 49
 	public function authorized() {
50 50
 		// if the rule is public, always authorized
51
-		if ( 'public' === $this->options['rule'] ) {
51
+		if ('public' === $this->options['rule']) {
52 52
 			return true;
53 53
 		}
54 54
 
55 55
 		// enable passing in callback
56
-		if ( 'callback' === $this->options['rule'] && is_callable( $this->options['callback'] ) ) {
57
-			return call_user_func( $this->options['callback'] );
56
+		if ('callback' === $this->options['rule'] && is_callable($this->options['callback'])) {
57
+			return call_user_func($this->options['callback']);
58 58
 		}
59 59
 
60 60
 		// map rule to method
61
-		if ( method_exists( $this, $method = $this->options['rule'] ) ) {
61
+		if (method_exists($this, $method = $this->options['rule'])) {
62 62
 			return $this->{$method}();
63 63
 		}
64 64
 
65 65
 		// disable in rule is misconfigused
66 66
 		// @todo set up internal translations
67 67
 		// @todo also, this error message kinda sucks
68
-		return new WP_Error( '500', __( 'Guard failure', 'jaxion' ) );
68
+		return new WP_Error('500', __('Guard failure', 'jaxion'));
69 69
 	}
70 70
 
71 71
 	/**
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 	 * @return bool|WP_Error
75 75
 	 */
76 76
 	protected function can_edit_others_posts() {
77
-		return current_user_can( 'edit_others_posts' ) ?: new WP_Error( 'unauthorized', __( 'Unauthorized user', 'jaxion' ), array( 'status' => 401 ) );
77
+		return current_user_can('edit_others_posts') ?: new WP_Error('unauthorized', __('Unauthorized user', 'jaxion'), array('status' => 401));
78 78
 	}
79 79
 
80 80
 	/**
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 	 * @return bool|WP_Error
84 84
 	 */
85 85
 	protected function user_logged_in() {
86
-		return is_user_logged_in() ?: new WP_Error( 'unauthorized', __( 'Unauthorized user', 'jaxion' ), array( 'status' => 401 ) );
86
+		return is_user_logged_in() ?: new WP_Error('unauthorized', __('Unauthorized user', 'jaxion'), array('status' => 401));
87 87
 	}
88 88
 
89 89
 	/**
@@ -93,8 +93,8 @@  discard block
 block discarded – undo
93 93
 	 *
94 94
 	 * @return array
95 95
 	 */
96
-	protected function set_defaults( $options ) {
96
+	protected function set_defaults($options) {
97 97
 		// these are the valid options
98
-		return wp_parse_args( $options, $this->defaults );
98
+		return wp_parse_args($options, $this->defaults);
99 99
 	}
100 100
 }
Please login to merge, or discard this patch.
src/Axolotl/EntityManager.php 3 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -3,7 +3,6 @@
 block discarded – undo
3 3
 
4 4
 use Intraxia\Jaxion\Axolotl\Relationship\Root as Relationship;
5 5
 use Intraxia\Jaxion\Axolotl\Repository\AbstractRepository;
6
-use Intraxia\Jaxion\Axolotl\Repository\CustomTable as CustomTableRepository;
7 6
 use Intraxia\Jaxion\Axolotl\Repository\WordPressPost as WordPressPostRepository;
8 7
 use Intraxia\Jaxion\Axolotl\Repository\WordPressTerm as WordPressTermRepository;
9 8
 use Intraxia\Jaxion\Contract\Axolotl\EntityManager as EntityManagerContract;
Please login to merge, or discard this patch.
Indentation   +241 added lines, -241 removed lines patch added patch discarded remove patch
@@ -22,245 +22,245 @@
 block discarded – undo
22 22
  * @subpackage Axolotl
23 23
  */
24 24
 class EntityManager implements EntityManagerContract {
25
-	/**
26
-	 * Post meta prefix.
27
-	 *
28
-	 * @var string
29
-	 */
30
-	protected $prefix;
31
-
32
-	/**
33
-	 * WP_Query instance.
34
-	 *
35
-	 * @var WP_Query
36
-	 */
37
-	protected $main;
38
-
39
-	/**
40
-	 * Global WPDB instance.
41
-	 *
42
-	 * @var wpdb
43
-	 */
44
-	protected $wpdb;
45
-
46
-	/**
47
-	 * EntityManager constructor.
48
-	 *
49
-	 * @param WP_Query $main
50
-	 * @param string   $prefix Post meta prefix.
51
-	 */
52
-	public function __construct( WP_Query $main, $prefix ) {
53
-		global $wpdb;
54
-
55
-		$this->wpdb   = $wpdb;
56
-		$this->main   = $main;
57
-		$this->prefix = $prefix;
58
-	}
59
-
60
-	/**
61
-	 * {@inheritDoc}
62
-	 *
63
-	 * @param string $class
64
-	 * @param int    $id
65
-	 *
66
-	 * @return Model|WP_Error
67
-	 *
68
-	 * @throws LogicException
69
-	 */
70
-	public function find( $class, $id ) {
71
-		$repository = $this->get_repository( $class );
72
-		$model      = $repository->find( $id );
73
-
74
-		if ( is_wp_error( $model ) ) {
75
-			return $model;
76
-		}
77
-
78
-		$this->handle_model( $repository, $model );
79
-
80
-		return $model;
81
-	}
82
-
83
-	/**
84
-	 * {@inheritDoc}
85
-	 *
86
-	 * @param string $class
87
-	 * @param array  $params
88
-	 *
89
-	 * @return Collection
90
-	 *
91
-	 * @throws LogicException
92
-	 */
93
-	public function find_by( $class, $params = array() ) {
94
-		$repository = $this->get_repository( $class );
95
-		$collection = $repository->find_by( $params );
96
-
97
-		foreach ( $collection as $model ) {
98
-			$this->handle_model( $repository, $model );
99
-		}
100
-
101
-		return $collection;
102
-	}
103
-
104
-	/**
105
-	 * {@inheritDoc}
106
-	 *
107
-	 * @param string $class
108
-	 * @param array  $data
109
-	 *
110
-	 * @return Model|WP_Error
111
-	 */
112
-	public function create( $class, $data = array() ) {
113
-		$repository = $this->get_repository( $class );
114
-		$model      = $repository->create( $data );
115
-
116
-		$this->handle_model( $repository, $model );
117
-
118
-		return $model;
119
-	}
120
-
121
-	/**
122
-	 * {@inheritDoc}
123
-	 *
124
-	 * @param Model $model
125
-	 *
126
-	 * @return Model|WP_Error
127
-	 */
128
-	public function persist( Model $model ) {
129
-		return $this->get_repository( get_class( $model ) )->persist( $model );
130
-	}
131
-
132
-	/**
133
-	 * {@inheritDoc}
134
-	 *
135
-	 * @param Model $model
136
-	 * @param bool  $force
137
-	 *
138
-	 * @return Model|WP_Error
139
-	 */
140
-	public function delete( Model $model, $force = false ) {
141
-		return $this->get_repository( get_class( $model ) )->delete( $model, $force );
142
-	}
143
-
144
-	/**
145
-	 * {@inheritDoc}
146
-	 */
147
-	public function free() {
148
-		AbstractRepository::free();
149
-	}
150
-
151
-	/**
152
-	 * Get the EntityManager prefix.
153
-	 *
154
-	 * @return string
155
-	 */
156
-	public function get_prefix() {
157
-		return $this->prefix;
158
-	}
159
-
160
-	/**
161
-	 * Get the main WP_Query instance.
162
-	 *
163
-	 * @return WP_Query
164
-	 */
165
-	public function get_main_query() {
166
-		return $this->main;
167
-	}
168
-
169
-	/**
170
-	 * Get the wpdb connection instance.
171
-	 *
172
-	 * @return wpdb
173
-	 */
174
-	public function get_wpdb() {
175
-		return $this->wpdb;
176
-	}
177
-
178
-	/**
179
-	 * Retrieves the repository for the given class.
180
-	 *
181
-	 * @param string $class
182
-	 *
183
-	 * @return Repository\AbstractRepository
184
-	 *
185
-	 * @throws LogicException
186
-	 */
187
-	protected function get_repository( $class ) {
188
-		// We can only use Axolotl models.
189
-		if ( ! is_subclass_of( $class, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
190
-			throw new LogicException;
191
-		}
192
-
193
-		if ( is_subclass_of(
194
-			$class,
195
-			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost'
196
-		) ) {
197
-			return new WordPressPostRepository( $this, $class );
198
-		}
199
-
200
-		if ( is_subclass_of(
201
-			$class,
202
-			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressTerm'
203
-		) ) {
204
-			return new WordPressTermRepository( $this, $class );
205
-		}
206
-
207
-		if ( is_subclass_of(
208
-			$class,
209
-			'Intraxia\Jaxion\Contract\Axolotl\UsesCustomTable'
210
-		) ) {
211
-			throw new LogicException;
212
-		}
213
-
214
-		// If a model doesn't have a backing data source,
215
-		// the developer needs to fix this immediately.
216
-		throw new LogicException;
217
-	}
218
-
219
-	/**
220
-	 * Ensures the model is registered with the model and fills its relations.
221
-	 *
222
-	 * @param AbstractRepository $repository
223
-	 * @param Model              $model
224
-	 */
225
-	protected function handle_model( AbstractRepository $repository, Model $model ) {
226
-		$repository->register_model( $model );
227
-
228
-		if ( $model instanceof HasEagerRelationships ) {
229
-			$this->fill_related( $model, $model::get_eager_relationships() );
230
-		}
231
-	}
232
-
233
-	/**
234
-	 * Fills the Model with the provided relations.
235
-	 *
236
-	 * If no relations are provided, all relations are filled.
237
-	 *
238
-	 * @param Model $model
239
-	 * @param array $relations
240
-	 *
241
-	 * @throws LogicException
242
-	 */
243
-	protected function fill_related( Model $model, array $relations = array() ) {
244
-		if ( ! $relations ) {
245
-			$relations = $model->get_related_keys();
246
-		}
247
-
248
-		foreach ( $relations as $relation ) {
249
-			if ( ! in_array( $relation, $model->get_related_keys() ) ) {
250
-				throw new LogicException;
251
-			}
252
-
253
-			if ( $model->relation_is_filled( $relation ) ) {
254
-				continue;
255
-			}
256
-
257
-			/**
258
-			 * Model relationship.
259
-			 *
260
-			 * @var Relationship $relation
261
-			 */
262
-			$relation = $model->{"related_{$relation}"}();
263
-			$relation->attach_relation( $this );
264
-		}
265
-	}
25
+    /**
26
+     * Post meta prefix.
27
+     *
28
+     * @var string
29
+     */
30
+    protected $prefix;
31
+
32
+    /**
33
+     * WP_Query instance.
34
+     *
35
+     * @var WP_Query
36
+     */
37
+    protected $main;
38
+
39
+    /**
40
+     * Global WPDB instance.
41
+     *
42
+     * @var wpdb
43
+     */
44
+    protected $wpdb;
45
+
46
+    /**
47
+     * EntityManager constructor.
48
+     *
49
+     * @param WP_Query $main
50
+     * @param string   $prefix Post meta prefix.
51
+     */
52
+    public function __construct( WP_Query $main, $prefix ) {
53
+        global $wpdb;
54
+
55
+        $this->wpdb   = $wpdb;
56
+        $this->main   = $main;
57
+        $this->prefix = $prefix;
58
+    }
59
+
60
+    /**
61
+     * {@inheritDoc}
62
+     *
63
+     * @param string $class
64
+     * @param int    $id
65
+     *
66
+     * @return Model|WP_Error
67
+     *
68
+     * @throws LogicException
69
+     */
70
+    public function find( $class, $id ) {
71
+        $repository = $this->get_repository( $class );
72
+        $model      = $repository->find( $id );
73
+
74
+        if ( is_wp_error( $model ) ) {
75
+            return $model;
76
+        }
77
+
78
+        $this->handle_model( $repository, $model );
79
+
80
+        return $model;
81
+    }
82
+
83
+    /**
84
+     * {@inheritDoc}
85
+     *
86
+     * @param string $class
87
+     * @param array  $params
88
+     *
89
+     * @return Collection
90
+     *
91
+     * @throws LogicException
92
+     */
93
+    public function find_by( $class, $params = array() ) {
94
+        $repository = $this->get_repository( $class );
95
+        $collection = $repository->find_by( $params );
96
+
97
+        foreach ( $collection as $model ) {
98
+            $this->handle_model( $repository, $model );
99
+        }
100
+
101
+        return $collection;
102
+    }
103
+
104
+    /**
105
+     * {@inheritDoc}
106
+     *
107
+     * @param string $class
108
+     * @param array  $data
109
+     *
110
+     * @return Model|WP_Error
111
+     */
112
+    public function create( $class, $data = array() ) {
113
+        $repository = $this->get_repository( $class );
114
+        $model      = $repository->create( $data );
115
+
116
+        $this->handle_model( $repository, $model );
117
+
118
+        return $model;
119
+    }
120
+
121
+    /**
122
+     * {@inheritDoc}
123
+     *
124
+     * @param Model $model
125
+     *
126
+     * @return Model|WP_Error
127
+     */
128
+    public function persist( Model $model ) {
129
+        return $this->get_repository( get_class( $model ) )->persist( $model );
130
+    }
131
+
132
+    /**
133
+     * {@inheritDoc}
134
+     *
135
+     * @param Model $model
136
+     * @param bool  $force
137
+     *
138
+     * @return Model|WP_Error
139
+     */
140
+    public function delete( Model $model, $force = false ) {
141
+        return $this->get_repository( get_class( $model ) )->delete( $model, $force );
142
+    }
143
+
144
+    /**
145
+     * {@inheritDoc}
146
+     */
147
+    public function free() {
148
+        AbstractRepository::free();
149
+    }
150
+
151
+    /**
152
+     * Get the EntityManager prefix.
153
+     *
154
+     * @return string
155
+     */
156
+    public function get_prefix() {
157
+        return $this->prefix;
158
+    }
159
+
160
+    /**
161
+     * Get the main WP_Query instance.
162
+     *
163
+     * @return WP_Query
164
+     */
165
+    public function get_main_query() {
166
+        return $this->main;
167
+    }
168
+
169
+    /**
170
+     * Get the wpdb connection instance.
171
+     *
172
+     * @return wpdb
173
+     */
174
+    public function get_wpdb() {
175
+        return $this->wpdb;
176
+    }
177
+
178
+    /**
179
+     * Retrieves the repository for the given class.
180
+     *
181
+     * @param string $class
182
+     *
183
+     * @return Repository\AbstractRepository
184
+     *
185
+     * @throws LogicException
186
+     */
187
+    protected function get_repository( $class ) {
188
+        // We can only use Axolotl models.
189
+        if ( ! is_subclass_of( $class, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
190
+            throw new LogicException;
191
+        }
192
+
193
+        if ( is_subclass_of(
194
+            $class,
195
+            'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost'
196
+        ) ) {
197
+            return new WordPressPostRepository( $this, $class );
198
+        }
199
+
200
+        if ( is_subclass_of(
201
+            $class,
202
+            'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressTerm'
203
+        ) ) {
204
+            return new WordPressTermRepository( $this, $class );
205
+        }
206
+
207
+        if ( is_subclass_of(
208
+            $class,
209
+            'Intraxia\Jaxion\Contract\Axolotl\UsesCustomTable'
210
+        ) ) {
211
+            throw new LogicException;
212
+        }
213
+
214
+        // If a model doesn't have a backing data source,
215
+        // the developer needs to fix this immediately.
216
+        throw new LogicException;
217
+    }
218
+
219
+    /**
220
+     * Ensures the model is registered with the model and fills its relations.
221
+     *
222
+     * @param AbstractRepository $repository
223
+     * @param Model              $model
224
+     */
225
+    protected function handle_model( AbstractRepository $repository, Model $model ) {
226
+        $repository->register_model( $model );
227
+
228
+        if ( $model instanceof HasEagerRelationships ) {
229
+            $this->fill_related( $model, $model::get_eager_relationships() );
230
+        }
231
+    }
232
+
233
+    /**
234
+     * Fills the Model with the provided relations.
235
+     *
236
+     * If no relations are provided, all relations are filled.
237
+     *
238
+     * @param Model $model
239
+     * @param array $relations
240
+     *
241
+     * @throws LogicException
242
+     */
243
+    protected function fill_related( Model $model, array $relations = array() ) {
244
+        if ( ! $relations ) {
245
+            $relations = $model->get_related_keys();
246
+        }
247
+
248
+        foreach ( $relations as $relation ) {
249
+            if ( ! in_array( $relation, $model->get_related_keys() ) ) {
250
+                throw new LogicException;
251
+            }
252
+
253
+            if ( $model->relation_is_filled( $relation ) ) {
254
+                continue;
255
+            }
256
+
257
+            /**
258
+             * Model relationship.
259
+             *
260
+             * @var Relationship $relation
261
+             */
262
+            $relation = $model->{"related_{$relation}"}();
263
+            $relation->attach_relation( $this );
264
+        }
265
+    }
266 266
 }
Please login to merge, or discard this patch.
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -49,7 +49,7 @@  discard block
 block discarded – undo
49 49
 	 * @param WP_Query $main
50 50
 	 * @param string   $prefix Post meta prefix.
51 51
 	 */
52
-	public function __construct( WP_Query $main, $prefix ) {
52
+	public function __construct(WP_Query $main, $prefix) {
53 53
 		global $wpdb;
54 54
 
55 55
 		$this->wpdb   = $wpdb;
@@ -67,15 +67,15 @@  discard block
 block discarded – undo
67 67
 	 *
68 68
 	 * @throws LogicException
69 69
 	 */
70
-	public function find( $class, $id ) {
71
-		$repository = $this->get_repository( $class );
72
-		$model      = $repository->find( $id );
70
+	public function find($class, $id) {
71
+		$repository = $this->get_repository($class);
72
+		$model      = $repository->find($id);
73 73
 
74
-		if ( is_wp_error( $model ) ) {
74
+		if (is_wp_error($model)) {
75 75
 			return $model;
76 76
 		}
77 77
 
78
-		$this->handle_model( $repository, $model );
78
+		$this->handle_model($repository, $model);
79 79
 
80 80
 		return $model;
81 81
 	}
@@ -90,12 +90,12 @@  discard block
 block discarded – undo
90 90
 	 *
91 91
 	 * @throws LogicException
92 92
 	 */
93
-	public function find_by( $class, $params = array() ) {
94
-		$repository = $this->get_repository( $class );
95
-		$collection = $repository->find_by( $params );
93
+	public function find_by($class, $params = array()) {
94
+		$repository = $this->get_repository($class);
95
+		$collection = $repository->find_by($params);
96 96
 
97
-		foreach ( $collection as $model ) {
98
-			$this->handle_model( $repository, $model );
97
+		foreach ($collection as $model) {
98
+			$this->handle_model($repository, $model);
99 99
 		}
100 100
 
101 101
 		return $collection;
@@ -109,11 +109,11 @@  discard block
 block discarded – undo
109 109
 	 *
110 110
 	 * @return Model|WP_Error
111 111
 	 */
112
-	public function create( $class, $data = array() ) {
113
-		$repository = $this->get_repository( $class );
114
-		$model      = $repository->create( $data );
112
+	public function create($class, $data = array()) {
113
+		$repository = $this->get_repository($class);
114
+		$model      = $repository->create($data);
115 115
 
116
-		$this->handle_model( $repository, $model );
116
+		$this->handle_model($repository, $model);
117 117
 
118 118
 		return $model;
119 119
 	}
@@ -125,8 +125,8 @@  discard block
 block discarded – undo
125 125
 	 *
126 126
 	 * @return Model|WP_Error
127 127
 	 */
128
-	public function persist( Model $model ) {
129
-		return $this->get_repository( get_class( $model ) )->persist( $model );
128
+	public function persist(Model $model) {
129
+		return $this->get_repository(get_class($model))->persist($model);
130 130
 	}
131 131
 
132 132
 	/**
@@ -137,8 +137,8 @@  discard block
 block discarded – undo
137 137
 	 *
138 138
 	 * @return Model|WP_Error
139 139
 	 */
140
-	public function delete( Model $model, $force = false ) {
141
-		return $this->get_repository( get_class( $model ) )->delete( $model, $force );
140
+	public function delete(Model $model, $force = false) {
141
+		return $this->get_repository(get_class($model))->delete($model, $force);
142 142
 	}
143 143
 
144 144
 	/**
@@ -184,30 +184,30 @@  discard block
 block discarded – undo
184 184
 	 *
185 185
 	 * @throws LogicException
186 186
 	 */
187
-	protected function get_repository( $class ) {
187
+	protected function get_repository($class) {
188 188
 		// We can only use Axolotl models.
189
-		if ( ! is_subclass_of( $class, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
189
+		if (!is_subclass_of($class, 'Intraxia\Jaxion\Axolotl\Model')) {
190 190
 			throw new LogicException;
191 191
 		}
192 192
 
193
-		if ( is_subclass_of(
193
+		if (is_subclass_of(
194 194
 			$class,
195 195
 			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost'
196
-		) ) {
197
-			return new WordPressPostRepository( $this, $class );
196
+		)) {
197
+			return new WordPressPostRepository($this, $class);
198 198
 		}
199 199
 
200
-		if ( is_subclass_of(
200
+		if (is_subclass_of(
201 201
 			$class,
202 202
 			'Intraxia\Jaxion\Contract\Axolotl\UsesWordPressTerm'
203
-		) ) {
204
-			return new WordPressTermRepository( $this, $class );
203
+		)) {
204
+			return new WordPressTermRepository($this, $class);
205 205
 		}
206 206
 
207
-		if ( is_subclass_of(
207
+		if (is_subclass_of(
208 208
 			$class,
209 209
 			'Intraxia\Jaxion\Contract\Axolotl\UsesCustomTable'
210
-		) ) {
210
+		)) {
211 211
 			throw new LogicException;
212 212
 		}
213 213
 
@@ -222,11 +222,11 @@  discard block
 block discarded – undo
222 222
 	 * @param AbstractRepository $repository
223 223
 	 * @param Model              $model
224 224
 	 */
225
-	protected function handle_model( AbstractRepository $repository, Model $model ) {
226
-		$repository->register_model( $model );
225
+	protected function handle_model(AbstractRepository $repository, Model $model) {
226
+		$repository->register_model($model);
227 227
 
228
-		if ( $model instanceof HasEagerRelationships ) {
229
-			$this->fill_related( $model, $model::get_eager_relationships() );
228
+		if ($model instanceof HasEagerRelationships) {
229
+			$this->fill_related($model, $model::get_eager_relationships());
230 230
 		}
231 231
 	}
232 232
 
@@ -240,17 +240,17 @@  discard block
 block discarded – undo
240 240
 	 *
241 241
 	 * @throws LogicException
242 242
 	 */
243
-	protected function fill_related( Model $model, array $relations = array() ) {
244
-		if ( ! $relations ) {
243
+	protected function fill_related(Model $model, array $relations = array()) {
244
+		if (!$relations) {
245 245
 			$relations = $model->get_related_keys();
246 246
 		}
247 247
 
248
-		foreach ( $relations as $relation ) {
249
-			if ( ! in_array( $relation, $model->get_related_keys() ) ) {
248
+		foreach ($relations as $relation) {
249
+			if (!in_array($relation, $model->get_related_keys())) {
250 250
 				throw new LogicException;
251 251
 			}
252 252
 
253
-			if ( $model->relation_is_filled( $relation ) ) {
253
+			if ($model->relation_is_filled($relation)) {
254 254
 				continue;
255 255
 			}
256 256
 
@@ -260,7 +260,7 @@  discard block
 block discarded – undo
260 260
 			 * @var Relationship $relation
261 261
 			 */
262 262
 			$relation = $model->{"related_{$relation}"}();
263
-			$relation->attach_relation( $this );
263
+			$relation->attach_relation($this);
264 264
 		}
265 265
 	}
266 266
 }
Please login to merge, or discard this patch.
src/Axolotl/Model.php 3 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.
Spacing   +126 added lines, -126 removed lines patch added patch discarded remove patch
@@ -113,15 +113,15 @@  discard block
 block discarded – undo
113 113
 	 *
114 114
 	 * @param array <string, mixed> $attributes
115 115
 	 */
116
-	public function __construct( array $attributes = array() ) {
116
+	public function __construct(array $attributes = array()) {
117 117
 		$this->maybe_boot();
118 118
 		$this->sync_original();
119 119
 
120
-		if ( $this->uses_wp_object() ) {
120
+		if ($this->uses_wp_object()) {
121 121
 			$this->create_wp_object();
122 122
 		}
123 123
 
124
-		$this->refresh( $attributes );
124
+		$this->refresh($attributes);
125 125
 	}
126 126
 
127 127
 	/**
@@ -132,11 +132,11 @@  discard block
 block discarded – undo
132 132
 	 *
133 133
 	 * @param array $attributes
134 134
 	 */
135
-	public function refresh( array $attributes ) {
135
+	public function refresh(array $attributes) {
136 136
 		$this->clear();
137 137
 
138
-		foreach ( $attributes as $name => $value ) {
139
-			$this->set_attribute( $name, $value );
138
+		foreach ($attributes as $name => $value) {
139
+			$this->set_attribute($name, $value);
140 140
 		}
141 141
 	}
142 142
 
@@ -171,11 +171,11 @@  discard block
 block discarded – undo
171 171
 	public function get_changed_table_attributes() {
172 172
 		$changed = array();
173 173
 
174
-		foreach ( $this->get_table_attributes() as $attribute ) {
175
-			if ( $this->get_attribute( $attribute ) !==
176
-			     $this->get_original_attribute( $attribute )
174
+		foreach ($this->get_table_attributes() as $attribute) {
175
+			if ($this->get_attribute($attribute) !==
176
+			     $this->get_original_attribute($attribute)
177 177
 			) {
178
-				$changed[ $attribute ] = $this->get_attribute( $attribute );
178
+				$changed[$attribute] = $this->get_attribute($attribute);
179 179
 			}
180 180
 		}
181 181
 
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
 	 * @return false|WP_Post|WP_Term
192 192
 	 */
193 193
 	public function get_underlying_wp_object() {
194
-		if ( isset( $this->attributes['object'] ) ) {
194
+		if (isset($this->attributes['object'])) {
195 195
 			return $this->attributes['object'];
196 196
 		}
197 197
 
@@ -217,11 +217,11 @@  discard block
 block discarded – undo
217 217
 	public function get_changed_wp_object_attributes() {
218 218
 		$changed = array();
219 219
 
220
-		foreach ( $this->get_wp_object_keys() as $key ) {
221
-			if ( $this->get_attribute( $key ) !==
222
-			     $this->get_original_attribute( $key )
220
+		foreach ($this->get_wp_object_keys() as $key) {
221
+			if ($this->get_attribute($key) !==
222
+			     $this->get_original_attribute($key)
223 223
 			) {
224
-				$changed[ $key ] = $this->get_attribute( $key );
224
+				$changed[$key] = $this->get_attribute($key);
225 225
 			}
226 226
 		}
227 227
 
@@ -236,8 +236,8 @@  discard block
 block discarded – undo
236 236
 	 * @param string $name
237 237
 	 * @param mixed  $value
238 238
 	 */
239
-	public function __set( $name, $value ) {
240
-		$this->set_attribute( $name, $value );
239
+	public function __set($name, $value) {
240
+		$this->set_attribute($name, $value);
241 241
 	}
242 242
 
243 243
 	/**
@@ -254,19 +254,19 @@  discard block
 block discarded – undo
254 254
 	 *
255 255
 	 * @throws GuardedPropertyException
256 256
 	 */
257
-	public function set_attribute( $name, $value ) {
258
-		if ( 'object' === $name ) {
259
-			return $this->override_wp_object( $value );
257
+	public function set_attribute($name, $value) {
258
+		if ('object' === $name) {
259
+			return $this->override_wp_object($value);
260 260
 		}
261 261
 
262
-		if ( ! $this->is_fillable( $name ) ) {
262
+		if (!$this->is_fillable($name)) {
263 263
 			throw new GuardedPropertyException;
264 264
 		}
265 265
 
266
-		if ( $method = $this->has_map_method( $name ) ) {
266
+		if ($method = $this->has_map_method($name)) {
267 267
 			$this->attributes['object']->{$this->{$method}()} = $value;
268 268
 		} else {
269
-			$this->attributes['table'][ $name ] = $value;
269
+			$this->attributes['table'][$name] = $value;
270 270
 		}
271 271
 
272 272
 		return $this;
@@ -278,11 +278,11 @@  discard block
 block discarded – undo
278 278
 	 * @return array
279 279
 	 */
280 280
 	public function get_attribute_keys() {
281
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
282
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
281
+		if (isset(self::$memo[get_called_class()][__METHOD__])) {
282
+			return self::$memo[get_called_class()][__METHOD__];
283 283
 		}
284 284
 
285
-		return self::$memo[ get_called_class() ][ __METHOD__ ]
285
+		return self::$memo[get_called_class()][__METHOD__]
286 286
 			= array_merge(
287 287
 				$this->fillable,
288 288
 				$this->guarded,
@@ -297,22 +297,22 @@  discard block
 block discarded – undo
297 297
 	 * @return array
298 298
 	 */
299 299
 	public function get_table_keys() {
300
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
301
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
300
+		if (isset(self::$memo[get_called_class()][__METHOD__])) {
301
+			return self::$memo[get_called_class()][__METHOD__];
302 302
 		}
303 303
 
304 304
 		$keys = array();
305 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 )
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 310
 			) {
311 311
 				$keys[] = $key;
312 312
 			}
313 313
 		}
314 314
 
315
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
315
+		return self::$memo[get_called_class()][__METHOD__] = $keys;
316 316
 	}
317 317
 
318 318
 	/**
@@ -321,19 +321,19 @@  discard block
 block discarded – undo
321 321
 	 * @return array
322 322
 	 */
323 323
 	public function get_wp_object_keys() {
324
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
325
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
324
+		if (isset(self::$memo[get_called_class()][__METHOD__])) {
325
+			return self::$memo[get_called_class()][__METHOD__];
326 326
 		}
327 327
 
328 328
 		$keys = array();
329 329
 
330
-		foreach ( $this->get_attribute_keys() as $key ) {
331
-			if ( $this->has_map_method( $key ) ) {
330
+		foreach ($this->get_attribute_keys() as $key) {
331
+			if ($this->has_map_method($key)) {
332 332
 				$keys[] = $key;
333 333
 			}
334 334
 		}
335 335
 
336
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
336
+		return self::$memo[get_called_class()][__METHOD__] = $keys;
337 337
 	}
338 338
 
339 339
 	/**
@@ -342,19 +342,19 @@  discard block
 block discarded – undo
342 342
 	 * @return array
343 343
 	 */
344 344
 	public function get_computed_keys() {
345
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
346
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
345
+		if (isset(self::$memo[get_called_class()][__METHOD__])) {
346
+			return self::$memo[get_called_class()][__METHOD__];
347 347
 		}
348 348
 
349 349
 		$keys = array();
350 350
 
351
-		foreach ( $this->get_attribute_keys() as $key ) {
352
-			if ( $this->has_compute_method( $key ) ) {
351
+		foreach ($this->get_attribute_keys() as $key) {
352
+			if ($this->has_compute_method($key)) {
353 353
 				$keys[] = $key;
354 354
 			}
355 355
 		}
356 356
 
357
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
357
+		return self::$memo[get_called_class()][__METHOD__] = $keys;
358 358
 	}
359 359
 
360 360
 	/**
@@ -363,19 +363,19 @@  discard block
 block discarded – undo
363 363
 	 * @return array
364 364
 	 */
365 365
 	public function get_related_keys() {
366
-		if ( isset( self::$memo[ get_called_class() ][ __METHOD__ ] ) ) {
367
-			return self::$memo[ get_called_class() ][ __METHOD__ ];
366
+		if (isset(self::$memo[get_called_class()][__METHOD__])) {
367
+			return self::$memo[get_called_class()][__METHOD__];
368 368
 		}
369 369
 
370 370
 		$keys = array();
371 371
 
372
-		foreach ( $this->get_attribute_keys() as $key ) {
373
-			if ( $this->has_related_method( $key ) ) {
372
+		foreach ($this->get_attribute_keys() as $key) {
373
+			if ($this->has_related_method($key)) {
374 374
 				$keys[] = $key;
375 375
 			}
376 376
 		}
377 377
 
378
-		return self::$memo[ get_called_class() ][ __METHOD__ ] = $keys;
378
+		return self::$memo[get_called_class()][__METHOD__] = $keys;
379 379
 	}
380 380
 
381 381
 	/**
@@ -386,32 +386,32 @@  discard block
 block discarded – undo
386 386
 	public function serialize() {
387 387
 		$attributes = array();
388 388
 
389
-		if ( $this->visible ) {
389
+		if ($this->visible) {
390 390
 			// If visible attributes are set, we'll only reveal those.
391
-			foreach ( $this->visible as $key ) {
392
-				$attributes[ $key ] = $this->get_attribute( $key );
391
+			foreach ($this->visible as $key) {
392
+				$attributes[$key] = $this->get_attribute($key);
393 393
 			}
394
-		} elseif ( $this->hidden ) {
394
+		} elseif ($this->hidden) {
395 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 );
396
+			foreach ($this->get_attribute_keys() as $key) {
397
+				if (!in_array($key, $this->hidden)) {
398
+					$attributes[$key] = $this->get_attribute($key);
399 399
 				}
400 400
 			}
401 401
 		} else {
402 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 );
403
+			foreach ($this->get_attribute_keys() as $key) {
404
+				$attributes[$key] = $this->get_attribute($key);
405 405
 			}
406 406
 		}
407 407
 
408
-		return array_map( function ( $attribute ) {
409
-			if ( $attribute instanceof Serializes ) {
408
+		return array_map(function($attribute) {
409
+			if ($attribute instanceof Serializes) {
410 410
 				return $attribute->serialize();
411 411
 			}
412 412
 
413 413
 			return $attribute;
414
-		}, $attributes );
414
+		}, $attributes);
415 415
 	}
416 416
 
417 417
 	/**
@@ -422,7 +422,7 @@  discard block
 block discarded – undo
422 422
 	public function sync_original() {
423 423
 		$this->original = $this->attributes;
424 424
 
425
-		if ( $this->attributes['object'] ) {
425
+		if ($this->attributes['object']) {
426 426
 			$this->original['object'] = clone $this->attributes['object'];
427 427
 		}
428 428
 
@@ -438,24 +438,24 @@  discard block
 block discarded – undo
438 438
 	 *
439 439
 	 * @return bool
440 440
 	 */
441
-	private function is_fillable( $name ) {
441
+	private function is_fillable($name) {
442 442
 		// If this model isn't guarded, everything is fillable.
443
-		if ( ! $this->is_guarded ) {
443
+		if (!$this->is_guarded) {
444 444
 			return true;
445 445
 		}
446 446
 
447 447
 		// If it's in the fillable array, then it's fillable.
448
-		if ( in_array( $name, $this->fillable ) ) {
448
+		if (in_array($name, $this->fillable)) {
449 449
 			return true;
450 450
 		}
451 451
 
452 452
 		// If it's explicitly guarded, then it's not fillable.
453
-		if ( in_array( $name, $this->guarded ) ) {
453
+		if (in_array($name, $this->guarded)) {
454 454
 			return false;
455 455
 		}
456 456
 
457 457
 		// If fillable hasn't been defined, then everything else fillable.
458
-		return ! $this->fillable;
458
+		return !$this->fillable;
459 459
 	}
460 460
 
461 461
 	/**
@@ -467,8 +467,8 @@  discard block
 block discarded – undo
467 467
 	 *
468 468
 	 * @return $this
469 469
 	 */
470
-	private function override_wp_object( $value ) {
471
-		$this->attributes['object'] = $this->set_wp_object_constants( $value );
470
+	private function override_wp_object($value) {
471
+		$this->attributes['object'] = $this->set_wp_object_constants($value);
472 472
 
473 473
 		return $this;
474 474
 	}
@@ -482,19 +482,19 @@  discard block
 block discarded – undo
482 482
 	 * @throws LogicException
483 483
 	 */
484 484
 	private function create_wp_object() {
485
-		switch ( true ) {
485
+		switch (true) {
486 486
 			case $this instanceof UsesWordPressPost:
487
-				$object = new WP_Post( (object) array() );
487
+				$object = new WP_Post((object) array());
488 488
 				break;
489 489
 			case $this instanceof UsesWordPressTerm:
490
-				$object = new WP_Term( (object) array() );
490
+				$object = new WP_Term((object) array());
491 491
 				break;
492 492
 			default:
493 493
 				throw new LogicException;
494 494
 				break;
495 495
 		}
496 496
 
497
-		$this->attributes['object'] = $this->set_wp_object_constants( $object );
497
+		$this->attributes['object'] = $this->set_wp_object_constants($object);
498 498
 	}
499 499
 
500 500
 	/**
@@ -508,12 +508,12 @@  discard block
 block discarded – undo
508 508
 	 *
509 509
 	 * @return object
510 510
 	 */
511
-	protected function set_wp_object_constants( $object ) {
512
-		if ( $this instanceof UsesWordPressPost ) {
511
+	protected function set_wp_object_constants($object) {
512
+		if ($this instanceof UsesWordPressPost) {
513 513
 			$object->post_type = $this::get_post_type();
514 514
 		}
515 515
 
516
-		if ( $this instanceof UsesWordPressTerm ) {
516
+		if ($this instanceof UsesWordPressTerm) {
517 517
 			$object->taxonomy = $this::get_taxonomy();
518 518
 		}
519 519
 
@@ -529,8 +529,8 @@  discard block
 block discarded – undo
529 529
 	 *
530 530
 	 * @return mixed
531 531
 	 */
532
-	public function __get( $name ) {
533
-		return $this->get_attribute( $name );
532
+	public function __get($name) {
533
+		return $this->get_attribute($name);
534 534
 	}
535 535
 
536 536
 	/**
@@ -542,19 +542,19 @@  discard block
 block discarded – undo
542 542
 	 *
543 543
 	 * @throws PropertyDoesNotExistException If property isn't found.
544 544
 	 */
545
-	public function get_attribute( $name ) {
546
-		if ( $method = $this->has_map_method( $name ) ) {
545
+	public function get_attribute($name) {
546
+		if ($method = $this->has_map_method($name)) {
547 547
 			$value = $this->attributes['object']->{$this->{$method}()};
548
-		} elseif ( $method = $this->has_compute_method( $name ) ) {
548
+		} elseif ($method = $this->has_compute_method($name)) {
549 549
 			$value = $this->{$method}();
550
-		} else if ( $method = $this->has_related_method( $name ) ) {
551
-			$value = $this->get_related( $this->{$method}()->get_sha() );
550
+		} else if ($method = $this->has_related_method($name)) {
551
+			$value = $this->get_related($this->{$method}()->get_sha());
552 552
 		} else {
553
-			if ( ! isset( $this->attributes['table'][ $name ] ) ) {
553
+			if (!isset($this->attributes['table'][$name])) {
554 554
 				throw new PropertyDoesNotExistException;
555 555
 			}
556 556
 
557
-			$value = $this->attributes['table'][ $name ];
557
+			$value = $this->attributes['table'][$name];
558 558
 		}
559 559
 
560 560
 		return $value;
@@ -569,10 +569,10 @@  discard block
 block discarded – undo
569 569
 	 *
570 570
 	 * @throws PropertyDoesNotExistException If property isn't found.
571 571
 	 */
572
-	public function get_original_attribute( $name ) {
573
-		$original = new static( $this->original );
572
+	public function get_original_attribute($name) {
573
+		$original = new static($this->original);
574 574
 
575
-		return $original->get_attribute( $name );
575
+		return $original->get_attribute($name);
576 576
 	}
577 577
 
578 578
 	/**
@@ -584,11 +584,11 @@  discard block
 block discarded – undo
584 584
 	 * @throws LogicException
585 585
 	 */
586 586
 	public function get_primary_id() {
587
-		if ( $this instanceof UsesWordPressPost ) {
587
+		if ($this instanceof UsesWordPressPost) {
588 588
 			return $this->get_underlying_wp_object()->ID;
589 589
 		}
590 590
 
591
-		if ( $this instanceof UsesWordPressTerm ) {
591
+		if ($this instanceof UsesWordPressTerm) {
592 592
 			return $this->get_underlying_wp_object()->term_id;
593 593
 		}
594 594
 
@@ -605,7 +605,7 @@  discard block
 block discarded – undo
605 605
 	 * @throws LogicException
606 606
 	 */
607 607
 	public function get_foreign_key() {
608
-		if ( $this instanceof UsesWordPressPost ) {
608
+		if ($this instanceof UsesWordPressPost) {
609 609
 			return 'post_id';
610 610
 		}
611 611
 
@@ -620,8 +620,8 @@  discard block
 block discarded – undo
620 620
 	 *
621 621
 	 * @return Model|Collection
622 622
 	 */
623
-	public function get_related( $sha ) {
624
-		return $this->related[ $sha ];
623
+	public function get_related($sha) {
624
+		return $this->related[$sha];
625 625
 	}
626 626
 
627 627
 	/**
@@ -632,12 +632,12 @@  discard block
 block discarded – undo
632 632
 	 *
633 633
 	 * @throws RuntimeException
634 634
 	 */
635
-	public function set_related( $sha, $target ) {
636
-		if ( ! ( $target instanceof Model ) && ! ( $target instanceof Collection ) ) {
635
+	public function set_related($sha, $target) {
636
+		if (!($target instanceof Model) && !($target instanceof Collection)) {
637 637
 			throw new RuntimeException;
638 638
 		}
639 639
 
640
-		$this->related[ $sha ] = $target;
640
+		$this->related[$sha] = $target;
641 641
 	}
642 642
 
643 643
 	/**
@@ -651,8 +651,8 @@  discard block
 block discarded – undo
651 651
 	 *
652 652
 	 * @return false|string
653 653
 	 */
654
-	protected function has_map_method( $name ) {
655
-		if ( method_exists( $this, $method = "map_{$name}" ) ) {
654
+	protected function has_map_method($name) {
655
+		if (method_exists($this, $method = "map_{$name}")) {
656 656
 			return $method;
657 657
 		}
658 658
 
@@ -669,8 +669,8 @@  discard block
 block discarded – undo
669 669
 	 *
670 670
 	 * @return false|string
671 671
 	 */
672
-	protected function has_compute_method( $name ) {
673
-		if ( method_exists( $this, $method = "compute_{$name}" ) ) {
672
+	protected function has_compute_method($name) {
673
+		if (method_exists($this, $method = "compute_{$name}")) {
674 674
 			return $method;
675 675
 		}
676 676
 
@@ -687,8 +687,8 @@  discard block
 block discarded – undo
687 687
 	 *
688 688
 	 * @return false|string
689 689
 	 */
690
-	protected function has_related_method( $name ) {
691
-		if ( method_exists( $this, $method = "related_{$name}" ) ) {
690
+	protected function has_related_method($name) {
691
+		if (method_exists($this, $method = "related_{$name}")) {
692 692
 			return $method;
693 693
 		}
694 694
 
@@ -706,10 +706,10 @@  discard block
 block discarded – undo
706 706
 	public function clear() {
707 707
 		$keys = $this->get_attribute_keys();
708 708
 
709
-		foreach ( $keys as $key ) {
709
+		foreach ($keys as $key) {
710 710
 			try {
711
-				$this->set_attribute( $key, null );
712
-			} catch ( GuardedPropertyException $e ) {
711
+				$this->set_attribute($key, null);
712
+			} catch (GuardedPropertyException $e) {
713 713
 				// We won't clear out guarded attributes.
714 714
 			}
715 715
 		}
@@ -743,13 +743,13 @@  discard block
 block discarded – undo
743 743
 	 * @return array
744 744
 	 */
745 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;
746
+		$methods = get_class_methods(get_called_class());
747
+		$methods = array_filter($methods, function($method) {
748
+			return strrpos($method, 'compute_', - strlen($method)) !== false;
749 749
 		} );
750
-		$methods = array_map( function ( $method ) {
751
-			return substr( $method, strlen( 'compute_' ) );
752
-		}, $methods );
750
+		$methods = array_map(function($method) {
751
+			return substr($method, strlen('compute_'));
752
+		}, $methods);
753 753
 
754 754
 		return $methods;
755 755
 	}
@@ -760,13 +760,13 @@  discard block
 block discarded – undo
760 760
 	 * @return array
761 761
 	 */
762 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;
763
+		$methods = get_class_methods(get_called_class());
764
+		$methods = array_filter($methods, function($method) {
765
+			return strrpos($method, 'related_', - strlen($method)) !== false;
766 766
 		} );
767
-		$methods = array_map( function ( $method ) {
768
-			return substr( $method, strlen( 'related_' ) );
769
-		}, $methods );
767
+		$methods = array_map(function($method) {
768
+			return substr($method, strlen('related_'));
769
+		}, $methods);
770 770
 
771 771
 		return $methods;
772 772
 	}
@@ -778,12 +778,12 @@  discard block
 block discarded – undo
778 778
 	 *
779 779
 	 * @return bool
780 780
 	 */
781
-	public function relation_is_filled( $relation ) {
781
+	public function relation_is_filled($relation) {
782 782
 		$sha = $this
783
-			->{$this->has_related_method( $relation )}()
783
+			->{$this->has_related_method($relation)}()
784 784
 			->get_sha();
785 785
 
786
-		return isset( $this->related[ $sha ] );
786
+		return isset($this->related[$sha]);
787 787
 	}
788 788
 
789 789
 	/**
@@ -801,7 +801,7 @@  discard block
 block discarded – undo
801 801
 	 *
802 802
 	 * @param bool $is_filling
803 803
 	 */
804
-	public function set_filling( $is_filling ) {
804
+	public function set_filling($is_filling) {
805 805
 		$this->filling = $is_filling;
806 806
 	}
807 807
 
@@ -814,8 +814,8 @@  discard block
 block discarded – undo
814 814
 	 *
815 815
 	 * @return HasMany
816 816
 	 */
817
-	protected function has_many( $class, $type, $foreign_key ) {
818
-		return new HasMany( $this, $class, $type, $foreign_key );
817
+	protected function has_many($class, $type, $foreign_key) {
818
+		return new HasMany($this, $class, $type, $foreign_key);
819 819
 	}
820 820
 
821 821
 	/**
@@ -827,16 +827,16 @@  discard block
 block discarded – undo
827 827
 	 *
828 828
 	 * @return HasMany
829 829
 	 */
830
-	protected function belongs_to_one( $class, $type, $local_key = '' ) {
831
-		return new BelongsToOne( $this, $class, $type, $local_key );
830
+	protected function belongs_to_one($class, $type, $local_key = '') {
831
+		return new BelongsToOne($this, $class, $type, $local_key);
832 832
 	}
833 833
 
834 834
 	/**
835 835
 	 * Sets up the memo array for the creating model.
836 836
 	 */
837 837
 	private function maybe_boot() {
838
-		if ( ! isset( self::$memo[ get_called_class() ] ) ) {
839
-			self::$memo[ get_called_class() ] = array();
838
+		if (!isset(self::$memo[get_called_class()])) {
839
+			self::$memo[get_called_class()] = array();
840 840
 		}
841 841
 	}
842 842
 
Please login to merge, or discard this patch.