Completed
Pull Request — master (#3)
by James
03:03
created
src/Axolotl/Repository/AbstractRepository.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -159,7 +159,7 @@
 block discarded – undo
159 159
 	/**
160 160
 	 * Fills the provided Model with attributes from its custom table.
161 161
 	 *
162
-	 * @param Model|UsesCustomTable $model
162
+	 * @param Model $model
163 163
 	 */
164 164
 	protected function fill_table_attrs_from_table( Model $model ) {
165 165
 		$sql[] = "SELECT * FROM {$this->make_table_name( $model )}";
Please login to merge, or discard this patch.
Indentation   +196 added lines, -196 removed lines patch added patch discarded remove patch
@@ -17,200 +17,200 @@
 block discarded – undo
17 17
  * @subpackage Axolotl\Repository
18 18
  */
19 19
 abstract class AbstractRepository {
20
-	/**
21
-	 * All models found by Repositories.
22
-	 *
23
-	 * @var array
24
-	 */
25
-	protected static $found = array();
26
-
27
-	/**
28
-	 * Reset the array of found model
29
-	 */
30
-	public static function free() {
31
-		self::$found = array();
32
-	}
33
-
34
-	/**
35
-	 * Retrieves the retrieved model by class and id.
36
-	 *
37
-	 * @param string $class
38
-	 * @param int    $id
39
-	 *
40
-	 * @return bool
41
-	 */
42
-	protected static function get_found( $class, $id ) {
43
-		if ( isset( self::$found[ $class ][ $id ] ) ) {
44
-			return self::$found[ $class ][ $id ];
45
-		}
46
-
47
-		return false;
48
-	}
49
-
50
-	/**
51
-	 * EntityManger service.
52
-	 *
53
-	 * @var EntityManager
54
-	 */
55
-	protected $database;
56
-
57
-	/**
58
-	 * Model class for this repository.
59
-	 *
60
-	 * @var string
61
-	 */
62
-	protected $class;
63
-
64
-	/**
65
-	 * EntityManager prefix.
66
-	 *
67
-	 * @var string
68
-	 */
69
-	protected $prefix;
70
-
71
-	/**
72
-	 * WP_Query instance.
73
-	 *
74
-	 * @var WP_Query
75
-	 */
76
-	protected $main;
77
-
78
-	/**
79
-	 * WordPress Database connection.
80
-	 *
81
-	 * @var wpdb
82
-	 */
83
-	protected $wpdb;
84
-
85
-	/**
86
-	 * AbstractRepository constructor.
87
-	 *
88
-	 * @param EntityManager $database
89
-	 * @param string        $class
90
-	 */
91
-	public function __construct( EntityManager $database, $class ) {
92
-		$this->database = $database;
93
-		$this->class    = $class;
94
-		$this->prefix   = $database->get_prefix();
95
-		$this->main     = $database->get_main_query();
96
-		$this->wpdb     = $database->get_wpdb();
97
-
98
-		if ( ! isset( self::$found[ $class ] ) ) {
99
-			self::$found[ $class ] = array();
100
-		}
101
-	}
102
-
103
-	/**
104
-	 * Get a single model of the repository class with the given ID.
105
-	 *
106
-	 * @param int $id ID of the model.
107
-	 *
108
-	 * @return Model|WP_Error
109
-	 */
110
-	abstract public function find( $id );
111
-
112
-	/**
113
-	 * Finds all the models of the repository class for the given params.
114
-	 *
115
-	 * @param array $params Params to constrain the find.
116
-	 *
117
-	 * @return Collection|WP_Error
118
-	 */
119
-	abstract public function find_by( array $params = array() );
120
-
121
-	/**
122
-	 * Create and saves a new model of the repository class
123
-	 * with the given data.
124
-	 *
125
-	 * @param array $data
126
-	 *
127
-	 * @return Model|WP_Error
128
-	 */
129
-	abstract public function create( array $data = array() );
130
-
131
-	/**
132
-	 * Updates a model with its latest data.
133
-	 *
134
-	 * @param Model $model
135
-	 *
136
-	 * @return Model|WP_Error
137
-	 */
138
-	abstract public function persist( Model $model );
139
-
140
-	/**
141
-	 * Delete the provided Model.
142
-	 *
143
-	 * @param Model $model
144
-	 * @param bool  $force
145
-	 *
146
-	 * @return Model|WP_Error
147
-	 */
148
-	abstract public function delete( Model $model, $force = false );
149
-
150
-	/**
151
-	 * Registers the found Model with the EntityManager.
152
-	 *
153
-	 * @param Model $model
154
-	 */
155
-	public function register_model( Model $model ) {
156
-		self::$found[ $this->class ][ $model->get_primary_id() ] = $model;
157
-	}
158
-
159
-	/**
160
-	 * Fills the provided Model with attributes from its custom table.
161
-	 *
162
-	 * @param Model|UsesCustomTable $model
163
-	 */
164
-	protected function fill_table_attrs_from_table( Model $model ) {
165
-		$sql[] = "SELECT * FROM {$this->make_table_name( $model )}";
166
-		$sql[] = "WHERE {$model->get_foreign_key()} = %d";
167
-
168
-		$sql = $this->wpdb->prepare(
169
-			implode( ' ', $sql ),
170
-			$model->get_primary_id()
171
-		);
172
-
173
-		$row = $this->wpdb->get_row( $sql, ARRAY_A );
174
-
175
-		$model->unguard();
176
-
177
-		foreach ( $row as $key => $value ) {
178
-			$model->set_attribute( $key, $value );
179
-		}
180
-
181
-		$model->reguard();
182
-	}
183
-
184
-	/**
185
-	 * Saves the Model's changed table attributes to its table.
186
-	 *
187
-	 * @param Model $model
188
-	 *
189
-	 * @throws LogicException
190
-	 */
191
-	protected function save_table_attributes_to_table( Model $model ) {
192
-		throw new LogicException;
193
-	}
194
-
195
-	/**
196
-	 * Deletes the Model's table attributes from its table.
197
-	 *
198
-	 * @param Model $model
199
-	 *
200
-	 * @throws LogicException
201
-	 */
202
-	protected function delete_table_attributes_from_table( Model $model ) {
203
-		throw new LogicException;
204
-	}
205
-
206
-	/**
207
-	 * Creates the table name string for the provided Model.
208
-	 *
209
-	 * @param UsesCustomTable $model
210
-	 *
211
-	 * @return string
212
-	 */
213
-	protected function make_table_name( UsesCustomTable $model ) {
214
-		return "{$this->wpdb->prefix}{$this->prefix}_{$model::get_table_name()}";
215
-	}
20
+    /**
21
+     * All models found by Repositories.
22
+     *
23
+     * @var array
24
+     */
25
+    protected static $found = array();
26
+
27
+    /**
28
+     * Reset the array of found model
29
+     */
30
+    public static function free() {
31
+        self::$found = array();
32
+    }
33
+
34
+    /**
35
+     * Retrieves the retrieved model by class and id.
36
+     *
37
+     * @param string $class
38
+     * @param int    $id
39
+     *
40
+     * @return bool
41
+     */
42
+    protected static function get_found( $class, $id ) {
43
+        if ( isset( self::$found[ $class ][ $id ] ) ) {
44
+            return self::$found[ $class ][ $id ];
45
+        }
46
+
47
+        return false;
48
+    }
49
+
50
+    /**
51
+     * EntityManger service.
52
+     *
53
+     * @var EntityManager
54
+     */
55
+    protected $database;
56
+
57
+    /**
58
+     * Model class for this repository.
59
+     *
60
+     * @var string
61
+     */
62
+    protected $class;
63
+
64
+    /**
65
+     * EntityManager prefix.
66
+     *
67
+     * @var string
68
+     */
69
+    protected $prefix;
70
+
71
+    /**
72
+     * WP_Query instance.
73
+     *
74
+     * @var WP_Query
75
+     */
76
+    protected $main;
77
+
78
+    /**
79
+     * WordPress Database connection.
80
+     *
81
+     * @var wpdb
82
+     */
83
+    protected $wpdb;
84
+
85
+    /**
86
+     * AbstractRepository constructor.
87
+     *
88
+     * @param EntityManager $database
89
+     * @param string        $class
90
+     */
91
+    public function __construct( EntityManager $database, $class ) {
92
+        $this->database = $database;
93
+        $this->class    = $class;
94
+        $this->prefix   = $database->get_prefix();
95
+        $this->main     = $database->get_main_query();
96
+        $this->wpdb     = $database->get_wpdb();
97
+
98
+        if ( ! isset( self::$found[ $class ] ) ) {
99
+            self::$found[ $class ] = array();
100
+        }
101
+    }
102
+
103
+    /**
104
+     * Get a single model of the repository class with the given ID.
105
+     *
106
+     * @param int $id ID of the model.
107
+     *
108
+     * @return Model|WP_Error
109
+     */
110
+    abstract public function find( $id );
111
+
112
+    /**
113
+     * Finds all the models of the repository class for the given params.
114
+     *
115
+     * @param array $params Params to constrain the find.
116
+     *
117
+     * @return Collection|WP_Error
118
+     */
119
+    abstract public function find_by( array $params = array() );
120
+
121
+    /**
122
+     * Create and saves a new model of the repository class
123
+     * with the given data.
124
+     *
125
+     * @param array $data
126
+     *
127
+     * @return Model|WP_Error
128
+     */
129
+    abstract public function create( array $data = array() );
130
+
131
+    /**
132
+     * Updates a model with its latest data.
133
+     *
134
+     * @param Model $model
135
+     *
136
+     * @return Model|WP_Error
137
+     */
138
+    abstract public function persist( Model $model );
139
+
140
+    /**
141
+     * Delete the provided Model.
142
+     *
143
+     * @param Model $model
144
+     * @param bool  $force
145
+     *
146
+     * @return Model|WP_Error
147
+     */
148
+    abstract public function delete( Model $model, $force = false );
149
+
150
+    /**
151
+     * Registers the found Model with the EntityManager.
152
+     *
153
+     * @param Model $model
154
+     */
155
+    public function register_model( Model $model ) {
156
+        self::$found[ $this->class ][ $model->get_primary_id() ] = $model;
157
+    }
158
+
159
+    /**
160
+     * Fills the provided Model with attributes from its custom table.
161
+     *
162
+     * @param Model|UsesCustomTable $model
163
+     */
164
+    protected function fill_table_attrs_from_table( Model $model ) {
165
+        $sql[] = "SELECT * FROM {$this->make_table_name( $model )}";
166
+        $sql[] = "WHERE {$model->get_foreign_key()} = %d";
167
+
168
+        $sql = $this->wpdb->prepare(
169
+            implode( ' ', $sql ),
170
+            $model->get_primary_id()
171
+        );
172
+
173
+        $row = $this->wpdb->get_row( $sql, ARRAY_A );
174
+
175
+        $model->unguard();
176
+
177
+        foreach ( $row as $key => $value ) {
178
+            $model->set_attribute( $key, $value );
179
+        }
180
+
181
+        $model->reguard();
182
+    }
183
+
184
+    /**
185
+     * Saves the Model's changed table attributes to its table.
186
+     *
187
+     * @param Model $model
188
+     *
189
+     * @throws LogicException
190
+     */
191
+    protected function save_table_attributes_to_table( Model $model ) {
192
+        throw new LogicException;
193
+    }
194
+
195
+    /**
196
+     * Deletes the Model's table attributes from its table.
197
+     *
198
+     * @param Model $model
199
+     *
200
+     * @throws LogicException
201
+     */
202
+    protected function delete_table_attributes_from_table( Model $model ) {
203
+        throw new LogicException;
204
+    }
205
+
206
+    /**
207
+     * Creates the table name string for the provided Model.
208
+     *
209
+     * @param UsesCustomTable $model
210
+     *
211
+     * @return string
212
+     */
213
+    protected function make_table_name( UsesCustomTable $model ) {
214
+        return "{$this->wpdb->prefix}{$this->prefix}_{$model::get_table_name()}";
215
+    }
216 216
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -39,9 +39,9 @@  discard block
 block discarded – undo
39 39
 	 *
40 40
 	 * @return bool
41 41
 	 */
42
-	protected static function get_found( $class, $id ) {
43
-		if ( isset( self::$found[ $class ][ $id ] ) ) {
44
-			return self::$found[ $class ][ $id ];
42
+	protected static function get_found($class, $id) {
43
+		if (isset(self::$found[$class][$id])) {
44
+			return self::$found[$class][$id];
45 45
 		}
46 46
 
47 47
 		return false;
@@ -88,15 +88,15 @@  discard block
 block discarded – undo
88 88
 	 * @param EntityManager $database
89 89
 	 * @param string        $class
90 90
 	 */
91
-	public function __construct( EntityManager $database, $class ) {
91
+	public function __construct(EntityManager $database, $class) {
92 92
 		$this->database = $database;
93 93
 		$this->class    = $class;
94 94
 		$this->prefix   = $database->get_prefix();
95 95
 		$this->main     = $database->get_main_query();
96 96
 		$this->wpdb     = $database->get_wpdb();
97 97
 
98
-		if ( ! isset( self::$found[ $class ] ) ) {
99
-			self::$found[ $class ] = array();
98
+		if (!isset(self::$found[$class])) {
99
+			self::$found[$class] = array();
100 100
 		}
101 101
 	}
102 102
 
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
 	 *
108 108
 	 * @return Model|WP_Error
109 109
 	 */
110
-	abstract public function find( $id );
110
+	abstract public function find($id);
111 111
 
112 112
 	/**
113 113
 	 * Finds all the models of the repository class for the given params.
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
 	 *
117 117
 	 * @return Collection|WP_Error
118 118
 	 */
119
-	abstract public function find_by( array $params = array() );
119
+	abstract public function find_by(array $params = array());
120 120
 
121 121
 	/**
122 122
 	 * Create and saves a new model of the repository class
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 	 *
127 127
 	 * @return Model|WP_Error
128 128
 	 */
129
-	abstract public function create( array $data = array() );
129
+	abstract public function create(array $data = array());
130 130
 
131 131
 	/**
132 132
 	 * Updates a model with its latest data.
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 	 *
136 136
 	 * @return Model|WP_Error
137 137
 	 */
138
-	abstract public function persist( Model $model );
138
+	abstract public function persist(Model $model);
139 139
 
140 140
 	/**
141 141
 	 * Delete the provided Model.
@@ -145,15 +145,15 @@  discard block
 block discarded – undo
145 145
 	 *
146 146
 	 * @return Model|WP_Error
147 147
 	 */
148
-	abstract public function delete( Model $model, $force = false );
148
+	abstract public function delete(Model $model, $force = false);
149 149
 
150 150
 	/**
151 151
 	 * Registers the found Model with the EntityManager.
152 152
 	 *
153 153
 	 * @param Model $model
154 154
 	 */
155
-	public function register_model( Model $model ) {
156
-		self::$found[ $this->class ][ $model->get_primary_id() ] = $model;
155
+	public function register_model(Model $model) {
156
+		self::$found[$this->class][$model->get_primary_id()] = $model;
157 157
 	}
158 158
 
159 159
 	/**
@@ -161,21 +161,21 @@  discard block
 block discarded – undo
161 161
 	 *
162 162
 	 * @param Model|UsesCustomTable $model
163 163
 	 */
164
-	protected function fill_table_attrs_from_table( Model $model ) {
165
-		$sql[] = "SELECT * FROM {$this->make_table_name( $model )}";
164
+	protected function fill_table_attrs_from_table(Model $model) {
165
+		$sql[] = "SELECT * FROM {$this->make_table_name($model)}";
166 166
 		$sql[] = "WHERE {$model->get_foreign_key()} = %d";
167 167
 
168 168
 		$sql = $this->wpdb->prepare(
169
-			implode( ' ', $sql ),
169
+			implode(' ', $sql),
170 170
 			$model->get_primary_id()
171 171
 		);
172 172
 
173
-		$row = $this->wpdb->get_row( $sql, ARRAY_A );
173
+		$row = $this->wpdb->get_row($sql, ARRAY_A);
174 174
 
175 175
 		$model->unguard();
176 176
 
177
-		foreach ( $row as $key => $value ) {
178
-			$model->set_attribute( $key, $value );
177
+		foreach ($row as $key => $value) {
178
+			$model->set_attribute($key, $value);
179 179
 		}
180 180
 
181 181
 		$model->reguard();
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
 	 *
189 189
 	 * @throws LogicException
190 190
 	 */
191
-	protected function save_table_attributes_to_table( Model $model ) {
191
+	protected function save_table_attributes_to_table(Model $model) {
192 192
 		throw new LogicException;
193 193
 	}
194 194
 
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
 	 *
200 200
 	 * @throws LogicException
201 201
 	 */
202
-	protected function delete_table_attributes_from_table( Model $model ) {
202
+	protected function delete_table_attributes_from_table(Model $model) {
203 203
 		throw new LogicException;
204 204
 	}
205 205
 
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
 	 *
211 211
 	 * @return string
212 212
 	 */
213
-	protected function make_table_name( UsesCustomTable $model ) {
213
+	protected function make_table_name(UsesCustomTable $model) {
214 214
 		return "{$this->wpdb->prefix}{$this->prefix}_{$model::get_table_name()}";
215 215
 	}
216 216
 }
Please login to merge, or discard this patch.
src/Axolotl/Repository/AbstractWordPress.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -239,7 +239,7 @@
 block discarded – undo
239 239
 	 *
240 240
 	 * @param Model $model
241 241
 	 *
242
-	 * @return int|WP_Error
242
+	 * @return integer
243 243
 	 */
244 244
 	abstract protected function save_wp_object( Model $model );
245 245
 
Please login to merge, or discard this patch.
Indentation   +276 added lines, -276 removed lines patch added patch discarded remove patch
@@ -16,280 +16,280 @@
 block discarded – undo
16 16
  * @subpackage Axolotl\Repository
17 17
  */
18 18
 abstract class AbstractWordPress extends AbstractRepository {
19
-	/**
20
-	 * {@inheritDoc}
21
-	 *
22
-	 * @param int $id
23
-	 */
24
-	public function find( $id ) {
25
-		if ( $model = self::get_found( $this->class, $id ) ) {
26
-			return $model;
27
-		}
28
-
29
-		$object = $this->get_wp_object_by_id( $id );
30
-
31
-		if ( ! $object ) {
32
-			return new WP_Error(
33
-				'not_found',
34
-				__( 'Entity not found', 'jaxion' )
35
-			);
36
-		}
37
-
38
-		return $this->make_model_from_wp_object( $object );
39
-	}
40
-
41
-	/**
42
-	 * {@inheritDoc}
43
-	 *
44
-	 * @param array $params
45
-	 *
46
-	 * @return Collection
47
-	 */
48
-	public function find_by( array $params = array() ) {
49
-		$collection = new Collection(
50
-			array(),
51
-			array( 'model' => $this->class )
52
-		);
53
-
54
-		foreach ( $this->get_wp_objects_by_params( $params ) as $object ) {
55
-			$collection->add(
56
-				$this->make_model_from_wp_object( $object )
57
-			);
58
-		}
59
-
60
-		return $collection;
61
-	}
62
-
63
-	/**
64
-	 * {@inheritDoc}
65
-	 *
66
-	 * @param array $data
67
-	 *
68
-	 * @return Model|WP_Error
69
-	 */
70
-	public function create( array $data = array() ) {
71
-		/**
72
-		 * Model object.
73
-		 *
74
-		 * @var Model $model
75
-		 */
76
-		$model = new $this->class( $data );
77
-
78
-		$id = $this->save_wp_object( $model );
79
-
80
-		if ( is_wp_error( $id ) ) {
81
-			return $id;
82
-		}
83
-
84
-		$model->set_attribute(
85
-			'object',
86
-			$this->get_wp_object_by_id( $id )
87
-		);
88
-
89
-		$this->save_table_attributes( $model );
90
-		$model->sync_original();
91
-
92
-		return $model;
93
-	}
94
-
95
-	/**
96
-	 * {@inheritDoc}
97
-	 *
98
-	 * @param Model $model
99
-	 *
100
-	 * @return Model|WP_Error
101
-	 */
102
-	public function persist( Model $model ) {
103
-		$id = $this->save_wp_object( $model );
104
-
105
-		if ( is_wp_error( $id ) ) {
106
-			return $id;
107
-		}
108
-
109
-		$model->set_attribute(
110
-			'object',
111
-			$this->get_wp_object_by_id( $id )
112
-		);
113
-
114
-		$this->save_table_attributes( $model );
115
-		$model->sync_original();
116
-
117
-		return $model;
118
-	}
119
-
120
-	/**
121
-	 * {@inheritDoc}
122
-	 *
123
-	 * @param Model $model
124
-	 * @param bool  $force
125
-	 *
126
-	 * @return Model|WP_Error
127
-	 */
128
-	public function delete( Model $model, $force = false ) {
129
-		$result = $this->delete_wp_object( $model, $force );
130
-
131
-		if ( is_wp_error( $result ) ) {
132
-			return $result;
133
-		}
134
-
135
-		if ( $force ) {
136
-			$this->delete_table_attributes( $model );
137
-		}
138
-
139
-		return $model;
140
-	}
141
-
142
-	/**
143
-	 * Builds the provided model class from the a wp object.
144
-	 *
145
-	 * @param WP_Post|WP_Term $object
146
-	 *
147
-	 * @return Model
148
-	 */
149
-	protected function make_model_from_wp_object( $object ) {
150
-		if ( $model = self::get_found( $this->class, $this->get_wp_object_id( $object ) ) ) {
151
-			return $model;
152
-		}
153
-
154
-		/**
155
-		 * Found Model.
156
-		 *
157
-		 * @var Model $model
158
-		 */
159
-		$model = new $this->class( array( 'object' => $object ) );
160
-
161
-		if ( $model instanceof UsesCustomTable ) {
162
-			$this->fill_table_attrs_from_table( $model );
163
-		} else {
164
-			$this->fill_table_attrs_from_meta( $model );
165
-		}
166
-
167
-		return $model;
168
-	}
169
-
170
-	/**
171
-	 * Save the table attributes for the provided model
172
-	 * to either a custom table or the Model's object's
173
-	 * metadata.
174
-	 *
175
-	 * @param Model $model
176
-	 */
177
-	protected function save_table_attributes( Model $model ) {
178
-		if ( $model instanceof UsesCustomTable ) {
179
-			$this->save_table_attributes_to_table( $model );
180
-		} else {
181
-			$this->save_table_attributes_to_meta( $model );
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * Delete the table attributes for the provided model
187
-	 * from either a custom table or the Model's object's
188
-	 * metadata.
189
-	 *
190
-	 * @param Model $model
191
-	 */
192
-	protected function delete_table_attributes( Model $model ) {
193
-		if ( $model instanceof UsesCustomTable ) {
194
-			$this->delete_table_attributes_from_table( $model );
195
-		} else {
196
-			$this->delete_table_attributes_from_meta( $model );
197
-		}
198
-	}
199
-
200
-	/**
201
-	 * Generates the unique meta key for
202
-	 * a WordPress model's data.
203
-	 *
204
-	 * @param string $key
205
-	 *
206
-	 * @return string
207
-	 */
208
-	protected function make_meta_key( $key ) {
209
-		return "_{$this->prefix}_{$key}";
210
-	}
211
-
212
-	/**
213
-	 * Retrieves an WordPress object by the provided id.
214
-	 *
215
-	 * @param int $id
216
-	 *
217
-	 * @return object|false
218
-	 */
219
-	abstract protected function get_wp_object_by_id( $id );
220
-
221
-	/**
222
-	 * Retrieves an array of WordPress objects by the provided params.
223
-	 *
224
-	 * @param array $params
225
-	 *
226
-	 * @return array
227
-	 */
228
-	abstract protected function get_wp_objects_by_params( $params );
229
-
230
-	/**
231
-	 * Fills the provided Model with its meta attributes.
232
-	 *
233
-	 * @param Model $model
234
-	 */
235
-	abstract protected function fill_table_attrs_from_meta( Model $model );
236
-
237
-	/**
238
-	 * Save the provided WordPress object to the WordPress database.
239
-	 *
240
-	 * @param Model $model
241
-	 *
242
-	 * @return int|WP_Error
243
-	 */
244
-	abstract protected function save_wp_object( Model $model );
245
-
246
-	/**
247
-	 * Saves the provided Model's changed table attributes
248
-	 * to the appropriate meta table.
249
-	 *
250
-	 * @param Model $model
251
-	 */
252
-	abstract protected function save_table_attributes_to_meta( Model $model );
253
-
254
-	/**
255
-	 * Delete the WordPress object associated with the
256
-	 * provided model. If `$force` is true, the object
257
-	 * will be deleted directly rather than
258
-	 *
259
-	 * @param Model $model
260
-	 * @param bool  $force
261
-	 */
262
-	abstract protected function delete_wp_object( Model $model, $force = false );
263
-
264
-	/**
265
-	 * Delete all of the metadata associated with the object on
266
-	 * the provided model.
267
-	 *
268
-	 * @param Model $model
269
-	 */
270
-	abstract protected function delete_table_attributes_from_meta( Model $model );
271
-
272
-	/**
273
-	 * Gets the primary ID of the provided WordPress object.
274
-	 *
275
-	 * @param WP_Post|WP_Term $object
276
-	 *
277
-	 * @return int|null
278
-	 *
279
-	 * @throws LogicException
280
-	 */
281
-	protected function get_wp_object_id( $object ) {
282
-		if ( $object instanceof WP_Post ) {
283
-			return $object->ID;
284
-		}
285
-
286
-		if ( $object instanceof WP_Term ||
287
-		     // This is required for WP4.3- compatibility, when they returned stdClass.
288
-		     ( $object instanceof \stdClass && property_exists( $object, 'term_id' ) )
289
-		) {
290
-			return $object->term_id;
291
-		}
292
-
293
-		throw new LogicException;
294
-	}
19
+    /**
20
+     * {@inheritDoc}
21
+     *
22
+     * @param int $id
23
+     */
24
+    public function find( $id ) {
25
+        if ( $model = self::get_found( $this->class, $id ) ) {
26
+            return $model;
27
+        }
28
+
29
+        $object = $this->get_wp_object_by_id( $id );
30
+
31
+        if ( ! $object ) {
32
+            return new WP_Error(
33
+                'not_found',
34
+                __( 'Entity not found', 'jaxion' )
35
+            );
36
+        }
37
+
38
+        return $this->make_model_from_wp_object( $object );
39
+    }
40
+
41
+    /**
42
+     * {@inheritDoc}
43
+     *
44
+     * @param array $params
45
+     *
46
+     * @return Collection
47
+     */
48
+    public function find_by( array $params = array() ) {
49
+        $collection = new Collection(
50
+            array(),
51
+            array( 'model' => $this->class )
52
+        );
53
+
54
+        foreach ( $this->get_wp_objects_by_params( $params ) as $object ) {
55
+            $collection->add(
56
+                $this->make_model_from_wp_object( $object )
57
+            );
58
+        }
59
+
60
+        return $collection;
61
+    }
62
+
63
+    /**
64
+     * {@inheritDoc}
65
+     *
66
+     * @param array $data
67
+     *
68
+     * @return Model|WP_Error
69
+     */
70
+    public function create( array $data = array() ) {
71
+        /**
72
+         * Model object.
73
+         *
74
+         * @var Model $model
75
+         */
76
+        $model = new $this->class( $data );
77
+
78
+        $id = $this->save_wp_object( $model );
79
+
80
+        if ( is_wp_error( $id ) ) {
81
+            return $id;
82
+        }
83
+
84
+        $model->set_attribute(
85
+            'object',
86
+            $this->get_wp_object_by_id( $id )
87
+        );
88
+
89
+        $this->save_table_attributes( $model );
90
+        $model->sync_original();
91
+
92
+        return $model;
93
+    }
94
+
95
+    /**
96
+     * {@inheritDoc}
97
+     *
98
+     * @param Model $model
99
+     *
100
+     * @return Model|WP_Error
101
+     */
102
+    public function persist( Model $model ) {
103
+        $id = $this->save_wp_object( $model );
104
+
105
+        if ( is_wp_error( $id ) ) {
106
+            return $id;
107
+        }
108
+
109
+        $model->set_attribute(
110
+            'object',
111
+            $this->get_wp_object_by_id( $id )
112
+        );
113
+
114
+        $this->save_table_attributes( $model );
115
+        $model->sync_original();
116
+
117
+        return $model;
118
+    }
119
+
120
+    /**
121
+     * {@inheritDoc}
122
+     *
123
+     * @param Model $model
124
+     * @param bool  $force
125
+     *
126
+     * @return Model|WP_Error
127
+     */
128
+    public function delete( Model $model, $force = false ) {
129
+        $result = $this->delete_wp_object( $model, $force );
130
+
131
+        if ( is_wp_error( $result ) ) {
132
+            return $result;
133
+        }
134
+
135
+        if ( $force ) {
136
+            $this->delete_table_attributes( $model );
137
+        }
138
+
139
+        return $model;
140
+    }
141
+
142
+    /**
143
+     * Builds the provided model class from the a wp object.
144
+     *
145
+     * @param WP_Post|WP_Term $object
146
+     *
147
+     * @return Model
148
+     */
149
+    protected function make_model_from_wp_object( $object ) {
150
+        if ( $model = self::get_found( $this->class, $this->get_wp_object_id( $object ) ) ) {
151
+            return $model;
152
+        }
153
+
154
+        /**
155
+         * Found Model.
156
+         *
157
+         * @var Model $model
158
+         */
159
+        $model = new $this->class( array( 'object' => $object ) );
160
+
161
+        if ( $model instanceof UsesCustomTable ) {
162
+            $this->fill_table_attrs_from_table( $model );
163
+        } else {
164
+            $this->fill_table_attrs_from_meta( $model );
165
+        }
166
+
167
+        return $model;
168
+    }
169
+
170
+    /**
171
+     * Save the table attributes for the provided model
172
+     * to either a custom table or the Model's object's
173
+     * metadata.
174
+     *
175
+     * @param Model $model
176
+     */
177
+    protected function save_table_attributes( Model $model ) {
178
+        if ( $model instanceof UsesCustomTable ) {
179
+            $this->save_table_attributes_to_table( $model );
180
+        } else {
181
+            $this->save_table_attributes_to_meta( $model );
182
+        }
183
+    }
184
+
185
+    /**
186
+     * Delete the table attributes for the provided model
187
+     * from either a custom table or the Model's object's
188
+     * metadata.
189
+     *
190
+     * @param Model $model
191
+     */
192
+    protected function delete_table_attributes( Model $model ) {
193
+        if ( $model instanceof UsesCustomTable ) {
194
+            $this->delete_table_attributes_from_table( $model );
195
+        } else {
196
+            $this->delete_table_attributes_from_meta( $model );
197
+        }
198
+    }
199
+
200
+    /**
201
+     * Generates the unique meta key for
202
+     * a WordPress model's data.
203
+     *
204
+     * @param string $key
205
+     *
206
+     * @return string
207
+     */
208
+    protected function make_meta_key( $key ) {
209
+        return "_{$this->prefix}_{$key}";
210
+    }
211
+
212
+    /**
213
+     * Retrieves an WordPress object by the provided id.
214
+     *
215
+     * @param int $id
216
+     *
217
+     * @return object|false
218
+     */
219
+    abstract protected function get_wp_object_by_id( $id );
220
+
221
+    /**
222
+     * Retrieves an array of WordPress objects by the provided params.
223
+     *
224
+     * @param array $params
225
+     *
226
+     * @return array
227
+     */
228
+    abstract protected function get_wp_objects_by_params( $params );
229
+
230
+    /**
231
+     * Fills the provided Model with its meta attributes.
232
+     *
233
+     * @param Model $model
234
+     */
235
+    abstract protected function fill_table_attrs_from_meta( Model $model );
236
+
237
+    /**
238
+     * Save the provided WordPress object to the WordPress database.
239
+     *
240
+     * @param Model $model
241
+     *
242
+     * @return int|WP_Error
243
+     */
244
+    abstract protected function save_wp_object( Model $model );
245
+
246
+    /**
247
+     * Saves the provided Model's changed table attributes
248
+     * to the appropriate meta table.
249
+     *
250
+     * @param Model $model
251
+     */
252
+    abstract protected function save_table_attributes_to_meta( Model $model );
253
+
254
+    /**
255
+     * Delete the WordPress object associated with the
256
+     * provided model. If `$force` is true, the object
257
+     * will be deleted directly rather than
258
+     *
259
+     * @param Model $model
260
+     * @param bool  $force
261
+     */
262
+    abstract protected function delete_wp_object( Model $model, $force = false );
263
+
264
+    /**
265
+     * Delete all of the metadata associated with the object on
266
+     * the provided model.
267
+     *
268
+     * @param Model $model
269
+     */
270
+    abstract protected function delete_table_attributes_from_meta( Model $model );
271
+
272
+    /**
273
+     * Gets the primary ID of the provided WordPress object.
274
+     *
275
+     * @param WP_Post|WP_Term $object
276
+     *
277
+     * @return int|null
278
+     *
279
+     * @throws LogicException
280
+     */
281
+    protected function get_wp_object_id( $object ) {
282
+        if ( $object instanceof WP_Post ) {
283
+            return $object->ID;
284
+        }
285
+
286
+        if ( $object instanceof WP_Term ||
287
+             // This is required for WP4.3- compatibility, when they returned stdClass.
288
+             ( $object instanceof \stdClass && property_exists( $object, 'term_id' ) )
289
+        ) {
290
+            return $object->term_id;
291
+        }
292
+
293
+        throw new LogicException;
294
+    }
295 295
 }
Please login to merge, or discard this patch.
Spacing   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -21,21 +21,21 @@  discard block
 block discarded – undo
21 21
 	 *
22 22
 	 * @param int $id
23 23
 	 */
24
-	public function find( $id ) {
25
-		if ( $model = self::get_found( $this->class, $id ) ) {
24
+	public function find($id) {
25
+		if ($model = self::get_found($this->class, $id)) {
26 26
 			return $model;
27 27
 		}
28 28
 
29
-		$object = $this->get_wp_object_by_id( $id );
29
+		$object = $this->get_wp_object_by_id($id);
30 30
 
31
-		if ( ! $object ) {
31
+		if (!$object) {
32 32
 			return new WP_Error(
33 33
 				'not_found',
34
-				__( 'Entity not found', 'jaxion' )
34
+				__('Entity not found', 'jaxion')
35 35
 			);
36 36
 		}
37 37
 
38
-		return $this->make_model_from_wp_object( $object );
38
+		return $this->make_model_from_wp_object($object);
39 39
 	}
40 40
 
41 41
 	/**
@@ -45,15 +45,15 @@  discard block
 block discarded – undo
45 45
 	 *
46 46
 	 * @return Collection
47 47
 	 */
48
-	public function find_by( array $params = array() ) {
48
+	public function find_by(array $params = array()) {
49 49
 		$collection = new Collection(
50 50
 			array(),
51
-			array( 'model' => $this->class )
51
+			array('model' => $this->class)
52 52
 		);
53 53
 
54
-		foreach ( $this->get_wp_objects_by_params( $params ) as $object ) {
54
+		foreach ($this->get_wp_objects_by_params($params) as $object) {
55 55
 			$collection->add(
56
-				$this->make_model_from_wp_object( $object )
56
+				$this->make_model_from_wp_object($object)
57 57
 			);
58 58
 		}
59 59
 
@@ -67,26 +67,26 @@  discard block
 block discarded – undo
67 67
 	 *
68 68
 	 * @return Model|WP_Error
69 69
 	 */
70
-	public function create( array $data = array() ) {
70
+	public function create(array $data = array()) {
71 71
 		/**
72 72
 		 * Model object.
73 73
 		 *
74 74
 		 * @var Model $model
75 75
 		 */
76
-		$model = new $this->class( $data );
76
+		$model = new $this->class($data);
77 77
 
78
-		$id = $this->save_wp_object( $model );
78
+		$id = $this->save_wp_object($model);
79 79
 
80
-		if ( is_wp_error( $id ) ) {
80
+		if (is_wp_error($id)) {
81 81
 			return $id;
82 82
 		}
83 83
 
84 84
 		$model->set_attribute(
85 85
 			'object',
86
-			$this->get_wp_object_by_id( $id )
86
+			$this->get_wp_object_by_id($id)
87 87
 		);
88 88
 
89
-		$this->save_table_attributes( $model );
89
+		$this->save_table_attributes($model);
90 90
 		$model->sync_original();
91 91
 
92 92
 		return $model;
@@ -99,19 +99,19 @@  discard block
 block discarded – undo
99 99
 	 *
100 100
 	 * @return Model|WP_Error
101 101
 	 */
102
-	public function persist( Model $model ) {
103
-		$id = $this->save_wp_object( $model );
102
+	public function persist(Model $model) {
103
+		$id = $this->save_wp_object($model);
104 104
 
105
-		if ( is_wp_error( $id ) ) {
105
+		if (is_wp_error($id)) {
106 106
 			return $id;
107 107
 		}
108 108
 
109 109
 		$model->set_attribute(
110 110
 			'object',
111
-			$this->get_wp_object_by_id( $id )
111
+			$this->get_wp_object_by_id($id)
112 112
 		);
113 113
 
114
-		$this->save_table_attributes( $model );
114
+		$this->save_table_attributes($model);
115 115
 		$model->sync_original();
116 116
 
117 117
 		return $model;
@@ -125,15 +125,15 @@  discard block
 block discarded – undo
125 125
 	 *
126 126
 	 * @return Model|WP_Error
127 127
 	 */
128
-	public function delete( Model $model, $force = false ) {
129
-		$result = $this->delete_wp_object( $model, $force );
128
+	public function delete(Model $model, $force = false) {
129
+		$result = $this->delete_wp_object($model, $force);
130 130
 
131
-		if ( is_wp_error( $result ) ) {
131
+		if (is_wp_error($result)) {
132 132
 			return $result;
133 133
 		}
134 134
 
135
-		if ( $force ) {
136
-			$this->delete_table_attributes( $model );
135
+		if ($force) {
136
+			$this->delete_table_attributes($model);
137 137
 		}
138 138
 
139 139
 		return $model;
@@ -146,8 +146,8 @@  discard block
 block discarded – undo
146 146
 	 *
147 147
 	 * @return Model
148 148
 	 */
149
-	protected function make_model_from_wp_object( $object ) {
150
-		if ( $model = self::get_found( $this->class, $this->get_wp_object_id( $object ) ) ) {
149
+	protected function make_model_from_wp_object($object) {
150
+		if ($model = self::get_found($this->class, $this->get_wp_object_id($object))) {
151 151
 			return $model;
152 152
 		}
153 153
 
@@ -156,12 +156,12 @@  discard block
 block discarded – undo
156 156
 		 *
157 157
 		 * @var Model $model
158 158
 		 */
159
-		$model = new $this->class( array( 'object' => $object ) );
159
+		$model = new $this->class(array('object' => $object));
160 160
 
161
-		if ( $model instanceof UsesCustomTable ) {
162
-			$this->fill_table_attrs_from_table( $model );
161
+		if ($model instanceof UsesCustomTable) {
162
+			$this->fill_table_attrs_from_table($model);
163 163
 		} else {
164
-			$this->fill_table_attrs_from_meta( $model );
164
+			$this->fill_table_attrs_from_meta($model);
165 165
 		}
166 166
 
167 167
 		return $model;
@@ -174,11 +174,11 @@  discard block
 block discarded – undo
174 174
 	 *
175 175
 	 * @param Model $model
176 176
 	 */
177
-	protected function save_table_attributes( Model $model ) {
178
-		if ( $model instanceof UsesCustomTable ) {
179
-			$this->save_table_attributes_to_table( $model );
177
+	protected function save_table_attributes(Model $model) {
178
+		if ($model instanceof UsesCustomTable) {
179
+			$this->save_table_attributes_to_table($model);
180 180
 		} else {
181
-			$this->save_table_attributes_to_meta( $model );
181
+			$this->save_table_attributes_to_meta($model);
182 182
 		}
183 183
 	}
184 184
 
@@ -189,11 +189,11 @@  discard block
 block discarded – undo
189 189
 	 *
190 190
 	 * @param Model $model
191 191
 	 */
192
-	protected function delete_table_attributes( Model $model ) {
193
-		if ( $model instanceof UsesCustomTable ) {
194
-			$this->delete_table_attributes_from_table( $model );
192
+	protected function delete_table_attributes(Model $model) {
193
+		if ($model instanceof UsesCustomTable) {
194
+			$this->delete_table_attributes_from_table($model);
195 195
 		} else {
196
-			$this->delete_table_attributes_from_meta( $model );
196
+			$this->delete_table_attributes_from_meta($model);
197 197
 		}
198 198
 	}
199 199
 
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
 	 *
206 206
 	 * @return string
207 207
 	 */
208
-	protected function make_meta_key( $key ) {
208
+	protected function make_meta_key($key) {
209 209
 		return "_{$this->prefix}_{$key}";
210 210
 	}
211 211
 
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
 	 *
217 217
 	 * @return object|false
218 218
 	 */
219
-	abstract protected function get_wp_object_by_id( $id );
219
+	abstract protected function get_wp_object_by_id($id);
220 220
 
221 221
 	/**
222 222
 	 * Retrieves an array of WordPress objects by the provided params.
@@ -225,14 +225,14 @@  discard block
 block discarded – undo
225 225
 	 *
226 226
 	 * @return array
227 227
 	 */
228
-	abstract protected function get_wp_objects_by_params( $params );
228
+	abstract protected function get_wp_objects_by_params($params);
229 229
 
230 230
 	/**
231 231
 	 * Fills the provided Model with its meta attributes.
232 232
 	 *
233 233
 	 * @param Model $model
234 234
 	 */
235
-	abstract protected function fill_table_attrs_from_meta( Model $model );
235
+	abstract protected function fill_table_attrs_from_meta(Model $model);
236 236
 
237 237
 	/**
238 238
 	 * Save the provided WordPress object to the WordPress database.
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
 	 *
242 242
 	 * @return int|WP_Error
243 243
 	 */
244
-	abstract protected function save_wp_object( Model $model );
244
+	abstract protected function save_wp_object(Model $model);
245 245
 
246 246
 	/**
247 247
 	 * Saves the provided Model's changed table attributes
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 	 *
250 250
 	 * @param Model $model
251 251
 	 */
252
-	abstract protected function save_table_attributes_to_meta( Model $model );
252
+	abstract protected function save_table_attributes_to_meta(Model $model);
253 253
 
254 254
 	/**
255 255
 	 * Delete the WordPress object associated with the
@@ -259,7 +259,7 @@  discard block
 block discarded – undo
259 259
 	 * @param Model $model
260 260
 	 * @param bool  $force
261 261
 	 */
262
-	abstract protected function delete_wp_object( Model $model, $force = false );
262
+	abstract protected function delete_wp_object(Model $model, $force = false);
263 263
 
264 264
 	/**
265 265
 	 * Delete all of the metadata associated with the object on
@@ -267,7 +267,7 @@  discard block
 block discarded – undo
267 267
 	 *
268 268
 	 * @param Model $model
269 269
 	 */
270
-	abstract protected function delete_table_attributes_from_meta( Model $model );
270
+	abstract protected function delete_table_attributes_from_meta(Model $model);
271 271
 
272 272
 	/**
273 273
 	 * Gets the primary ID of the provided WordPress object.
@@ -278,14 +278,14 @@  discard block
 block discarded – undo
278 278
 	 *
279 279
 	 * @throws LogicException
280 280
 	 */
281
-	protected function get_wp_object_id( $object ) {
282
-		if ( $object instanceof WP_Post ) {
281
+	protected function get_wp_object_id($object) {
282
+		if ($object instanceof WP_Post) {
283 283
 			return $object->ID;
284 284
 		}
285 285
 
286
-		if ( $object instanceof WP_Term ||
286
+		if ($object instanceof WP_Term ||
287 287
 		     // This is required for WP4.3- compatibility, when they returned stdClass.
288
-		     ( $object instanceof \stdClass && property_exists( $object, 'term_id' ) )
288
+		     ($object instanceof \stdClass && property_exists($object, 'term_id'))
289 289
 		) {
290 290
 			return $object->term_id;
291 291
 		}
Please login to merge, or discard this patch.
src/Http/Filter.php 2 patches
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -12,134 +12,134 @@
 block discarded – undo
12 12
  * @subpackage Http
13 13
  */
14 14
 class Filter implements FilterContract {
15
-	/**
16
-	 * Filter rules.
17
-	 *
18
-	 * @var array
19
-	 */
20
-	protected $rules;
21
-
22
-	/**
23
-	 * Instantiates a new filter with the provided rules array.
24
-	 *
25
-	 * @param array $rules
26
-	 */
27
-	public function __construct( $rules = array() ) {
28
-		$this->rules = $rules;
29
-	}
30
-
31
-	/**
32
-	 * Generates argument rules.
33
-	 *
34
-	 * Returns an array matching the WP-API format for argument rules,
35
-	 * including sanitization, validation, required, or defaults.
36
-	 *
37
-	 * @return array
38
-	 */
39
-	public function rules() {
40
-		$args = array();
41
-
42
-		foreach ( $this->rules as $arg => $validation ) {
43
-			if ( ! $validation || ! is_string( $validation ) ) {
44
-				continue;
45
-			}
46
-
47
-			$args[ $arg ] = $this->parse_validation( $validation );
48
-		}
49
-
50
-		return $args;
51
-	}
52
-
53
-	/**
54
-	 * Parses a validation string into a WP-API compatible rule.
55
-	 *
56
-	 * @param string $validation
57
-	 *
58
-	 * @return array
59
-	 *
60
-	 * @todo The next rule added needs to refactor this process.
61
-	 */
62
-	protected function parse_validation( $validation ) {
63
-		$validation = explode( '|', $validation );
64
-
65
-		$rules = array();
66
-
67
-		foreach ( $validation as $rule ) {
68
-			if ( 0 === strpos( $rule, 'default' ) ) {
69
-				$rule_arr = explode( ':', $rule );
70
-
71
-				$rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
72
-			}
73
-
74
-			if ( 0 === strpos( $rule, 'oneof' ) ) {
75
-				list( $rule, $values ) = explode( ':', $rule );
76
-
77
-				$values   = explode( ',', $values );
78
-				$callback = function ( $value ) use ( $values ) {
79
-					if ( in_array( $value, $values, true ) ) {
80
-						return true;
81
-					}
82
-
83
-					return false;
84
-				};
85
-
86
-				$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
87
-			}
88
-
89
-			switch ( $rule ) {
90
-				case 'required':
91
-					$rules['required'] = true;
92
-					break;
93
-				case 'integer':
94
-					$callback                   = array( $this, 'validate_integer' );
95
-					$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
96
-
97
-					$callback                   = array( $this, 'make_integer' );
98
-					$rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
99
-					break;
100
-			}
101
-		}
102
-
103
-		return $rules;
104
-	}
105
-
106
-	/**
107
-	 * Validate that provided value is an integer.
108
-	 *
109
-	 * @param mixed $value
110
-	 *
111
-	 * @return bool
112
-	 */
113
-	public function validate_integer( $value ) {
114
-		return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
115
-	}
116
-
117
-	/**
118
-	 * Casts a provided value to an integer.
119
-	 *
120
-	 * @param mixed $value
121
-	 *
122
-	 * @return int
123
-	 */
124
-	public function make_integer( $value ) {
125
-		return (int) $value;
126
-	}
127
-
128
-	/**
129
-	 * Creates a new callback that connects the previous and next callback.
130
-	 *
131
-	 * @param callable $previous
132
-	 * @param callable $next
133
-	 *
134
-	 * @return \Closure;
135
-	 */
136
-	private function add_callback( $previous, $next ) {
137
-		return function ( $value ) use ( $previous, $next ) {
138
-			if ( call_user_func( $previous, $value ) ) {
139
-				return call_user_func( $next, $value );
140
-			}
141
-
142
-			return false;
143
-		};
144
-	}
15
+    /**
16
+     * Filter rules.
17
+     *
18
+     * @var array
19
+     */
20
+    protected $rules;
21
+
22
+    /**
23
+     * Instantiates a new filter with the provided rules array.
24
+     *
25
+     * @param array $rules
26
+     */
27
+    public function __construct( $rules = array() ) {
28
+        $this->rules = $rules;
29
+    }
30
+
31
+    /**
32
+     * Generates argument rules.
33
+     *
34
+     * Returns an array matching the WP-API format for argument rules,
35
+     * including sanitization, validation, required, or defaults.
36
+     *
37
+     * @return array
38
+     */
39
+    public function rules() {
40
+        $args = array();
41
+
42
+        foreach ( $this->rules as $arg => $validation ) {
43
+            if ( ! $validation || ! is_string( $validation ) ) {
44
+                continue;
45
+            }
46
+
47
+            $args[ $arg ] = $this->parse_validation( $validation );
48
+        }
49
+
50
+        return $args;
51
+    }
52
+
53
+    /**
54
+     * Parses a validation string into a WP-API compatible rule.
55
+     *
56
+     * @param string $validation
57
+     *
58
+     * @return array
59
+     *
60
+     * @todo The next rule added needs to refactor this process.
61
+     */
62
+    protected function parse_validation( $validation ) {
63
+        $validation = explode( '|', $validation );
64
+
65
+        $rules = array();
66
+
67
+        foreach ( $validation as $rule ) {
68
+            if ( 0 === strpos( $rule, 'default' ) ) {
69
+                $rule_arr = explode( ':', $rule );
70
+
71
+                $rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
72
+            }
73
+
74
+            if ( 0 === strpos( $rule, 'oneof' ) ) {
75
+                list( $rule, $values ) = explode( ':', $rule );
76
+
77
+                $values   = explode( ',', $values );
78
+                $callback = function ( $value ) use ( $values ) {
79
+                    if ( in_array( $value, $values, true ) ) {
80
+                        return true;
81
+                    }
82
+
83
+                    return false;
84
+                };
85
+
86
+                $rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
87
+            }
88
+
89
+            switch ( $rule ) {
90
+                case 'required':
91
+                    $rules['required'] = true;
92
+                    break;
93
+                case 'integer':
94
+                    $callback                   = array( $this, 'validate_integer' );
95
+                    $rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
96
+
97
+                    $callback                   = array( $this, 'make_integer' );
98
+                    $rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
99
+                    break;
100
+            }
101
+        }
102
+
103
+        return $rules;
104
+    }
105
+
106
+    /**
107
+     * Validate that provided value is an integer.
108
+     *
109
+     * @param mixed $value
110
+     *
111
+     * @return bool
112
+     */
113
+    public function validate_integer( $value ) {
114
+        return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
115
+    }
116
+
117
+    /**
118
+     * Casts a provided value to an integer.
119
+     *
120
+     * @param mixed $value
121
+     *
122
+     * @return int
123
+     */
124
+    public function make_integer( $value ) {
125
+        return (int) $value;
126
+    }
127
+
128
+    /**
129
+     * Creates a new callback that connects the previous and next callback.
130
+     *
131
+     * @param callable $previous
132
+     * @param callable $next
133
+     *
134
+     * @return \Closure;
135
+     */
136
+    private function add_callback( $previous, $next ) {
137
+        return function ( $value ) use ( $previous, $next ) {
138
+            if ( call_user_func( $previous, $value ) ) {
139
+                return call_user_func( $next, $value );
140
+            }
141
+
142
+            return false;
143
+        };
144
+    }
145 145
 }
Please login to merge, or discard this patch.
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
 	 *
25 25
 	 * @param array $rules
26 26
 	 */
27
-	public function __construct( $rules = array() ) {
27
+	public function __construct($rules = array()) {
28 28
 		$this->rules = $rules;
29 29
 	}
30 30
 
@@ -39,12 +39,12 @@  discard block
 block discarded – undo
39 39
 	public function rules() {
40 40
 		$args = array();
41 41
 
42
-		foreach ( $this->rules as $arg => $validation ) {
43
-			if ( ! $validation || ! is_string( $validation ) ) {
42
+		foreach ($this->rules as $arg => $validation) {
43
+			if (!$validation || !is_string($validation)) {
44 44
 				continue;
45 45
 			}
46 46
 
47
-			$args[ $arg ] = $this->parse_validation( $validation );
47
+			$args[$arg] = $this->parse_validation($validation);
48 48
 		}
49 49
 
50 50
 		return $args;
@@ -59,43 +59,43 @@  discard block
 block discarded – undo
59 59
 	 *
60 60
 	 * @todo The next rule added needs to refactor this process.
61 61
 	 */
62
-	protected function parse_validation( $validation ) {
63
-		$validation = explode( '|', $validation );
62
+	protected function parse_validation($validation) {
63
+		$validation = explode('|', $validation);
64 64
 
65 65
 		$rules = array();
66 66
 
67
-		foreach ( $validation as $rule ) {
68
-			if ( 0 === strpos( $rule, 'default' ) ) {
69
-				$rule_arr = explode( ':', $rule );
67
+		foreach ($validation as $rule) {
68
+			if (0 === strpos($rule, 'default')) {
69
+				$rule_arr = explode(':', $rule);
70 70
 
71
-				$rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
71
+				$rules['default'] = count($rule_arr) === 2 ? array_pop($rule_arr) : '';
72 72
 			}
73 73
 
74
-			if ( 0 === strpos( $rule, 'oneof' ) ) {
75
-				list( $rule, $values ) = explode( ':', $rule );
74
+			if (0 === strpos($rule, 'oneof')) {
75
+				list($rule, $values) = explode(':', $rule);
76 76
 
77
-				$values   = explode( ',', $values );
78
-				$callback = function ( $value ) use ( $values ) {
79
-					if ( in_array( $value, $values, true ) ) {
77
+				$values   = explode(',', $values);
78
+				$callback = function($value) use ($values) {
79
+					if (in_array($value, $values, true)) {
80 80
 						return true;
81 81
 					}
82 82
 
83 83
 					return false;
84 84
 				};
85 85
 
86
-				$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
86
+				$rules['validate_callback'] = isset($rules['validate_callback']) ? $this->add_callback($rules['validate_callback'], $callback) : $callback;
87 87
 			}
88 88
 
89
-			switch ( $rule ) {
89
+			switch ($rule) {
90 90
 				case 'required':
91 91
 					$rules['required'] = true;
92 92
 					break;
93 93
 				case 'integer':
94
-					$callback                   = array( $this, 'validate_integer' );
95
-					$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
94
+					$callback                   = array($this, 'validate_integer');
95
+					$rules['validate_callback'] = isset($rules['validate_callback']) ? $this->add_callback($rules['validate_callback'], $callback) : $callback;
96 96
 
97
-					$callback                   = array( $this, 'make_integer' );
98
-					$rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
97
+					$callback                   = array($this, 'make_integer');
98
+					$rules['sanitize_callback'] = isset($rules['sanitize_callback']) ? $this->add_callback($rules['sanitize_callback'], $callback) : $callback;
99 99
 					break;
100 100
 			}
101 101
 		}
@@ -110,8 +110,8 @@  discard block
 block discarded – undo
110 110
 	 *
111 111
 	 * @return bool
112 112
 	 */
113
-	public function validate_integer( $value ) {
114
-		return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
113
+	public function validate_integer($value) {
114
+		return filter_var($value, FILTER_VALIDATE_INT) !== false;
115 115
 	}
116 116
 
117 117
 	/**
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
 	 *
122 122
 	 * @return int
123 123
 	 */
124
-	public function make_integer( $value ) {
124
+	public function make_integer($value) {
125 125
 		return (int) $value;
126 126
 	}
127 127
 
@@ -133,10 +133,10 @@  discard block
 block discarded – undo
133 133
 	 *
134 134
 	 * @return \Closure;
135 135
 	 */
136
-	private function add_callback( $previous, $next ) {
137
-		return function ( $value ) use ( $previous, $next ) {
138
-			if ( call_user_func( $previous, $value ) ) {
139
-				return call_user_func( $next, $value );
136
+	private function add_callback($previous, $next) {
137
+		return function($value) use ($previous, $next) {
138
+			if (call_user_func($previous, $value)) {
139
+				return call_user_func($next, $value);
140 140
 			}
141 141
 
142 142
 			return false;
Please login to merge, or discard this patch.
src/Http/ServiceProvider.php 2 patches
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -11,37 +11,37 @@
 block discarded – undo
11 11
  * @subpackage Http
12 12
  */
13 13
 class ServiceProvider implements ServiceProviderContract {
14
-	/**
15
-	 * Container to register on.
16
-	 *
17
-	 * @var Container
18
-	 */
19
-	protected $container;
14
+    /**
15
+     * Container to register on.
16
+     *
17
+     * @var Container
18
+     */
19
+    protected $container;
20 20
 
21
-	/**
22
-	 * {@inheritDoc}
23
-	 *
24
-	 * @param Container $container
25
-	 */
26
-	public function register( Container $container ) {
27
-		$this->container = $container;
21
+    /**
22
+     * {@inheritDoc}
23
+     *
24
+     * @param Container $container
25
+     */
26
+    public function register( Container $container ) {
27
+        $this->container = $container;
28 28
 
29
-		$this->container->define( array( 'router' => 'Intraxia\Jaxion\Http\Router' ), $router = new Router );
29
+        $this->container->define( array( 'router' => 'Intraxia\Jaxion\Http\Router' ), $router = new Router );
30 30
 
31
-		$this->add_routes( $router );
32
-	}
31
+        $this->add_routes( $router );
32
+    }
33 33
 
34
-	/**
35
-	 * Registers the routes on the generated Router.
36
-	 *
37
-	 * This is a no-op by default by can be overwritten by the implementing developer
38
-	 * to provide a single, clean location to register their routes.
39
-	 *
40
-	 * @param Router $router
41
-	 *
42
-	 * @codeCoverageIgnore
43
-	 */
44
-	protected function add_routes( Router $router ) {
45
-		// no-op
46
-	}
34
+    /**
35
+     * Registers the routes on the generated Router.
36
+     *
37
+     * This is a no-op by default by can be overwritten by the implementing developer
38
+     * to provide a single, clean location to register their routes.
39
+     *
40
+     * @param Router $router
41
+     *
42
+     * @codeCoverageIgnore
43
+     */
44
+    protected function add_routes( Router $router ) {
45
+        // no-op
46
+    }
47 47
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -23,12 +23,12 @@  discard block
 block discarded – undo
23 23
 	 *
24 24
 	 * @param Container $container
25 25
 	 */
26
-	public function register( Container $container ) {
26
+	public function register(Container $container) {
27 27
 		$this->container = $container;
28 28
 
29
-		$this->container->define( array( 'router' => 'Intraxia\Jaxion\Http\Router' ), $router = new Router );
29
+		$this->container->define(array('router' => 'Intraxia\Jaxion\Http\Router'), $router = new Router);
30 30
 
31
-		$this->add_routes( $router );
31
+		$this->add_routes($router);
32 32
 	}
33 33
 
34 34
 	/**
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
 	 *
42 42
 	 * @codeCoverageIgnore
43 43
 	 */
44
-	protected function add_routes( Router $router ) {
44
+	protected function add_routes(Router $router) {
45 45
 		// no-op
46 46
 	}
47 47
 }
Please login to merge, or discard this patch.
src/Contract/Core/HasActions.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -2,13 +2,13 @@
 block discarded – undo
2 2
 namespace Intraxia\Jaxion\Contract\Core;
3 3
 
4 4
 interface HasActions {
5
-	/**
6
-	 * Provides the array of actions the class wants to register with WordPress.
7
-	 *
8
-	 * These actions are retrieved by the Loader class and used to register the
9
-	 * correct service methods with WordPress.
10
-	 *
11
-	 * @return array[]
12
-	 */
13
-	public function action_hooks();
5
+    /**
6
+     * Provides the array of actions the class wants to register with WordPress.
7
+     *
8
+     * These actions are retrieved by the Loader class and used to register the
9
+     * correct service methods with WordPress.
10
+     *
11
+     * @return array[]
12
+     */
13
+    public function action_hooks();
14 14
 }
Please login to merge, or discard this patch.
src/Contract/Core/HasShortcode.php 2 patches
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -2,18 +2,18 @@
 block discarded – undo
2 2
 namespace Intraxia\Jaxion\Contract\Core;
3 3
 
4 4
 interface HasShortcode {
5
-	/**
6
-	 * Get the shortcode string to interpret.
7
-	 *
8
-	 * @return string
9
-	 */
10
-	public function shortcode_name();
5
+    /**
6
+     * Get the shortcode string to interpret.
7
+     *
8
+     * @return string
9
+     */
10
+    public function shortcode_name();
11 11
 
12
-	/**
13
-	 * WordPress callback to fire on the shortcode.
14
-	 *
15
-	 * @param array  $atts
16
-	 * @param string $content
17
-	 */
18
-	public function do_shortcode( array $atts, $content = '' );
12
+    /**
13
+     * WordPress callback to fire on the shortcode.
14
+     *
15
+     * @param array  $atts
16
+     * @param string $content
17
+     */
18
+    public function do_shortcode( array $atts, $content = '' );
19 19
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -15,5 +15,5 @@
 block discarded – undo
15 15
 	 * @param array  $atts
16 16
 	 * @param string $content
17 17
 	 */
18
-	public function do_shortcode( array $atts, $content = '' );
18
+	public function do_shortcode(array $atts, $content = '');
19 19
 }
Please login to merge, or discard this patch.
src/Contract/Core/Loader.php 2 patches
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -2,41 +2,41 @@
 block discarded – undo
2 2
 namespace Intraxia\Jaxion\Contract\Core;
3 3
 
4 4
 interface Loader {
5
-	/**
6
-	 * Register all the actions and filters with WordPress.
7
-	 *
8
-	 * Loops through all the registered actions/filters and fires add_* for each of
9
-	 * them respectively.
10
-	 */
11
-	public function run();
5
+    /**
6
+     * Register all the actions and filters with WordPress.
7
+     *
8
+     * Loops through all the registered actions/filters and fires add_* for each of
9
+     * them respectively.
10
+     */
11
+    public function run();
12 12
 
13
-	/**
14
-	 * Registers the service's actions with the loader.
15
-	 *
16
-	 * Actions retrieved from the service are registered with the Loader.
17
-	 * When the loader runs, this actions are registered with WordPress.
18
-	 *
19
-	 * @param HasActions $service
20
-	 */
21
-	public function register_actions( HasActions $service );
13
+    /**
14
+     * Registers the service's actions with the loader.
15
+     *
16
+     * Actions retrieved from the service are registered with the Loader.
17
+     * When the loader runs, this actions are registered with WordPress.
18
+     *
19
+     * @param HasActions $service
20
+     */
21
+    public function register_actions( HasActions $service );
22 22
 
23
-	/**
24
-	 * Registers the service's filters with the loader.
25
-	 *
26
-	 * Filters retrieved from the service are registered with the Loader.
27
-	 * When the loader runs, these filters are registered with WordPress.
28
-	 *
29
-	 * @param HasFilters $service
30
-	 */
31
-	public function register_filters( HasFilters $service );
23
+    /**
24
+     * Registers the service's filters with the loader.
25
+     *
26
+     * Filters retrieved from the service are registered with the Loader.
27
+     * When the loader runs, these filters are registered with WordPress.
28
+     *
29
+     * @param HasFilters $service
30
+     */
31
+    public function register_filters( HasFilters $service );
32 32
 
33
-	/**
34
-	 * Registers the service's shortcode with the loader.
35
-	 *
36
-	 * Service's method definitions are passed into the add_shortcode hook
37
-	 * in WordPress to register the shortcode.
38
-	 *
39
-	 * @param HasShortcode $service
40
-	 */
41
-	public function register_shortcode( HasShortcode $service );
33
+    /**
34
+     * Registers the service's shortcode with the loader.
35
+     *
36
+     * Service's method definitions are passed into the add_shortcode hook
37
+     * in WordPress to register the shortcode.
38
+     *
39
+     * @param HasShortcode $service
40
+     */
41
+    public function register_shortcode( HasShortcode $service );
42 42
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
 	 *
19 19
 	 * @param HasActions $service
20 20
 	 */
21
-	public function register_actions( HasActions $service );
21
+	public function register_actions(HasActions $service);
22 22
 
23 23
 	/**
24 24
 	 * Registers the service's filters with the loader.
@@ -28,7 +28,7 @@  discard block
 block discarded – undo
28 28
 	 *
29 29
 	 * @param HasFilters $service
30 30
 	 */
31
-	public function register_filters( HasFilters $service );
31
+	public function register_filters(HasFilters $service);
32 32
 
33 33
 	/**
34 34
 	 * Registers the service's shortcode with the loader.
@@ -38,5 +38,5 @@  discard block
 block discarded – undo
38 38
 	 *
39 39
 	 * @param HasShortcode $service
40 40
 	 */
41
-	public function register_shortcode( HasShortcode $service );
41
+	public function register_shortcode(HasShortcode $service);
42 42
 }
Please login to merge, or discard this patch.
src/Contract/Core/HasFilters.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -2,13 +2,13 @@
 block discarded – undo
2 2
 namespace Intraxia\Jaxion\Contract\Core;
3 3
 
4 4
 interface HasFilters {
5
-	/**
6
-	 * Provides the array of filters the class wants to register with WordPress.
7
-	 *
8
-	 * These filters are retrieved by the Loader class and used to register the
9
-	 * correct service methods with WordPress.
10
-	 *
11
-	 * @return array[]
12
-	 */
13
-	public function filter_hooks();
5
+    /**
6
+     * Provides the array of filters the class wants to register with WordPress.
7
+     *
8
+     * These filters are retrieved by the Loader class and used to register the
9
+     * correct service methods with WordPress.
10
+     *
11
+     * @return array[]
12
+     */
13
+    public function filter_hooks();
14 14
 }
Please login to merge, or discard this patch.
src/Contract/Core/ServiceProvider.php 2 patches
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -2,13 +2,13 @@
 block discarded – undo
2 2
 namespace Intraxia\Jaxion\Contract\Core;
3 3
 
4 4
 interface ServiceProvider {
5
-	/**
6
-	 * Register the provider's services on the container.
7
-	 *
8
-	 * This method is passed the container to register on, giving the service provider
9
-	 * an opportunity to register its services on the container in an encapsulated way.
10
-	 *
11
-	 * @param Container $container
12
-	 */
13
-	public function register( Container $container );
5
+    /**
6
+     * Register the provider's services on the container.
7
+     *
8
+     * This method is passed the container to register on, giving the service provider
9
+     * an opportunity to register its services on the container in an encapsulated way.
10
+     *
11
+     * @param Container $container
12
+     */
13
+    public function register( Container $container );
14 14
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -10,5 +10,5 @@
 block discarded – undo
10 10
 	 *
11 11
 	 * @param Container $container
12 12
 	 */
13
-	public function register( Container $container );
13
+	public function register(Container $container);
14 14
 }
Please login to merge, or discard this patch.