Completed
Pull Request — master (#3)
by James
03:03
created
src/Contract/Core/Container.php 1 patch
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -2,75 +2,75 @@
 block discarded – undo
2 2
 namespace Intraxia\Jaxion\Contract\Core;
3 3
 
4 4
 interface Container extends \ArrayAccess, \Iterator {
5
-	/**
6
-	 * Define a new service or value on the Container.
7
-	 *
8
-	 * The alias is the name that the value will be referenced by. This can be used by both
9
-	 * the `get` method to retrieve the value or through ArrayAccess (`$container['alias']`).
10
-	 * It should be a short name used to reference the defined value. The definition can be
11
-	 * any scalar value to assign to the alias, or it can define a service object to return.
12
-	 * This can be accomplished by passing in a closure, which takes the container and returns
13
-	 * a fully constructed object. This closure will be executed every time the class is fetched.
14
-	 * If an already-instantiated object is passed in, it will be returned when fetched. A
15
-	 * Definition object will be returned for additional manipulation. Scalar values will be
16
-	 * locked automatically and can't be overridden.
17
-	 *
18
-	 * @param string|array $alias
19
-	 * @param mixed        $definition
20
-	 *
21
-	 * @return $this
22
-	 */
23
-	public function define( $alias, $definition );
5
+    /**
6
+     * Define a new service or value on the Container.
7
+     *
8
+     * The alias is the name that the value will be referenced by. This can be used by both
9
+     * the `get` method to retrieve the value or through ArrayAccess (`$container['alias']`).
10
+     * It should be a short name used to reference the defined value. The definition can be
11
+     * any scalar value to assign to the alias, or it can define a service object to return.
12
+     * This can be accomplished by passing in a closure, which takes the container and returns
13
+     * a fully constructed object. This closure will be executed every time the class is fetched.
14
+     * If an already-instantiated object is passed in, it will be returned when fetched. A
15
+     * Definition object will be returned for additional manipulation. Scalar values will be
16
+     * locked automatically and can't be overridden.
17
+     *
18
+     * @param string|array $alias
19
+     * @param mixed        $definition
20
+     *
21
+     * @return $this
22
+     */
23
+    public function define( $alias, $definition );
24 24
 
25
-	/**
26
-	 * Defines a new singleton on the Container.
27
-	 *
28
-	 * Functions identically to Container::define, except closures passed in are only executed
29
-	 * once, and the return value is reused across multiple fetches.
30
-	 *
31
-	 * @param string|array $alias
32
-	 * @param mixed        $definition
33
-	 *
34
-	 * @return $this
35
-	 */
36
-	public function share( $alias, $definition );
25
+    /**
26
+     * Defines a new singleton on the Container.
27
+     *
28
+     * Functions identically to Container::define, except closures passed in are only executed
29
+     * once, and the return value is reused across multiple fetches.
30
+     *
31
+     * @param string|array $alias
32
+     * @param mixed        $definition
33
+     *
34
+     * @return $this
35
+     */
36
+    public function share( $alias, $definition );
37 37
 
38
-	/**
39
-	 * Fetches the value for the provided alias.
40
-	 *
41
-	 * @param string $alias
42
-	 *
43
-	 * @return mixed
44
-	 */
45
-	public function fetch( $alias );
38
+    /**
39
+     * Fetches the value for the provided alias.
40
+     *
41
+     * @param string $alias
42
+     *
43
+     * @return mixed
44
+     */
45
+    public function fetch( $alias );
46 46
 
47
-	/**
48
-	 * Checks whether the provided alias exists on the container.
49
-	 *
50
-	 * @param string $alias
51
-	 *
52
-	 * @return bool
53
-	 */
54
-	public function has( $alias );
47
+    /**
48
+     * Checks whether the provided alias exists on the container.
49
+     *
50
+     * @param string $alias
51
+     *
52
+     * @return bool
53
+     */
54
+    public function has( $alias );
55 55
 
56
-	/**
57
-	 * Removes the provided alias from the container.
58
-	 *
59
-	 * @param string $alias
60
-	 *
61
-	 * @return bool
62
-	 */
63
-	public function remove( $alias );
56
+    /**
57
+     * Removes the provided alias from the container.
58
+     *
59
+     * @param string $alias
60
+     *
61
+     * @return bool
62
+     */
63
+    public function remove( $alias );
64 64
 
65
-	/**
66
-	 * Registers a service provider with the container.
67
-	 *
68
-	 * A service provider is responsible for defining and generating services that will be bound
69
-	 * into the container. This keeps the container and Application responsible solely for maintaining
70
-	 * the generated services and the API for registering them and allows for a clean interface for
71
-	 * adding new services to the container.
72
-	 *
73
-	 * @param ServiceProvider $provider
74
-	 */
75
-	public function register( ServiceProvider $provider );
65
+    /**
66
+     * Registers a service provider with the container.
67
+     *
68
+     * A service provider is responsible for defining and generating services that will be bound
69
+     * into the container. This keeps the container and Application responsible solely for maintaining
70
+     * the generated services and the API for registering them and allows for a clean interface for
71
+     * adding new services to the container.
72
+     *
73
+     * @param ServiceProvider $provider
74
+     */
75
+    public function register( ServiceProvider $provider );
76 76
 }
Please login to merge, or discard this patch.
src/Contract/Http/Filter.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -10,13 +10,13 @@
 block discarded – undo
10 10
  * @subpackage Contract\Http
11 11
  */
12 12
 interface Filter {
13
-	/**
14
-	 * Generates argument rules.
15
-	 *
16
-	 * Returns an array matching the WP-API format for argument rules,
17
-	 * including sanitization, validation, required, or defaults.
18
-	 *
19
-	 * @return array
20
-	 */
21
-	public function rules();
13
+    /**
14
+     * Generates argument rules.
15
+     *
16
+     * Returns an array matching the WP-API format for argument rules,
17
+     * including sanitization, validation, required, or defaults.
18
+     *
19
+     * @return array
20
+     */
21
+    public function rules();
22 22
 }
Please login to merge, or discard this patch.
src/Contract/Http/Guard.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -10,13 +10,13 @@
 block discarded – undo
10 10
  * @subpackage Contract\Http
11 11
  */
12 12
 interface Guard {
13
-	/**
14
-	 * Validates when the user is authorized for the route.
15
-	 *
16
-	 * Returns a boolean based on whether the current user is authorized
17
-	 * to interact with the given route.
18
-	 *
19
-	 * @return bool
20
-	 */
21
-	public function authorized();
13
+    /**
14
+     * Validates when the user is authorized for the route.
15
+     *
16
+     * Returns a boolean based on whether the current user is authorized
17
+     * to interact with the given route.
18
+     *
19
+     * @return bool
20
+     */
21
+    public function authorized();
22 22
 }
Please login to merge, or discard this patch.
src/Http/Endpoint.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -14,141 +14,141 @@
 block discarded – undo
14 14
  * @subpackage Http
15 15
  */
16 16
 class Endpoint {
17
-	/**
18
-	 * Endpoint's route.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	protected $route;
23
-
24
-	/**
25
-	 * Endpoint's HTTP verb(s).
26
-	 *
27
-	 * @var string
28
-	 */
29
-	protected $method;
30
-
31
-	/**
32
-	 * Endpoint's callback.
33
-	 *
34
-	 * @var callable
35
-	 */
36
-	protected $callback;
37
-
38
-	/**
39
-	 * Endpoint's permission guard.
40
-	 *
41
-	 * @var GuardContract
42
-	 */
43
-	protected $guard;
44
-
45
-	/**
46
-	 * Endpoint's arguments filter.
47
-	 *
48
-	 * @var FilterContract
49
-	 */
50
-	protected $filter;
51
-
52
-	/**
53
-	 * Endpoint's route prefix.
54
-	 *
55
-	 * @var string
56
-	 */
57
-	protected $prefix;
58
-
59
-	/**
60
-	 * Instantiate a new endpoint with a provided route, method, and callback.
61
-	 *
62
-	 * @param string   $route
63
-	 * @param string   $method
64
-	 * @param callable $callback
65
-	 *
66
-	 * @throws MalformedRouteException
67
-	 */
68
-	public function __construct( $route, $method, $callback ) {
69
-		if ( ! Str::starts_with( $route, '/' ) || Str::ends_with( $route, '/' ) ) {
70
-			throw new MalformedRouteException;
71
-		}
72
-
73
-		$this->route    = $route;
74
-		$this->method   = $method;
75
-		$this->callback = $callback;
76
-	}
77
-
78
-	/**
79
-	 * Generates the endpoint's route.
80
-	 *
81
-	 * Combines the prefix with the route to generate the full route string.
82
-	 *
83
-	 * @return string
84
-	 */
85
-	public function get_route() {
86
-		return ( $this->prefix ?: '' ) . $this->route;
87
-	}
88
-
89
-	/**
90
-	 * Generates the endpoint's WP-API options array.
91
-	 *
92
-	 * @return array
93
-	 */
94
-	public function get_options() {
95
-		$options = array(
96
-			'methods'  => $this->method,
97
-			'callback' => $this->callback,
98
-		);
99
-
100
-		if ( $this->guard ) {
101
-			$options['permission_callback'] = array( $this->guard, 'authorized' );
102
-		}
103
-
104
-		if ( $this->filter ) {
105
-			$options['args'] = $this->filter->rules();
106
-		}
107
-
108
-		return $options;
109
-	}
110
-
111
-	/**
112
-	 * Sets the endpoint's permission guard.
113
-	 *
114
-	 * @param GuardContract $guard
115
-	 *
116
-	 * @return $this
117
-	 */
118
-	public function set_guard( GuardContract $guard ) {
119
-		$this->guard = $guard;
120
-
121
-		return $this;
122
-	}
123
-
124
-	/**
125
-	 * Sets the endpoint's arguments filter.
126
-	 *
127
-	 * @param FilterContract $filter
128
-	 *
129
-	 * @return $this
130
-	 */
131
-	public function set_filter( FilterContract $filter ) {
132
-		$this->filter = $filter;
133
-
134
-		return $this;
135
-	}
136
-
137
-	/**
138
-	 * Sets the endpoint's prefix.
139
-	 *
140
-	 * @param string $prefix
141
-	 *
142
-	 * @return $this
143
-	 * @throws MalformedRouteException
144
-	 */
145
-	public function set_prefix( $prefix ) {
146
-		if ( ! Str::starts_with( $prefix, '/' ) || Str::ends_with( $prefix, '/' ) ) {
147
-			throw new MalformedRouteException;
148
-		}
149
-
150
-		$this->prefix = $prefix;
151
-
152
-		return $this;
153
-	}
17
+    /**
18
+     * Endpoint's route.
19
+     *
20
+     * @var string
21
+     */
22
+    protected $route;
23
+
24
+    /**
25
+     * Endpoint's HTTP verb(s).
26
+     *
27
+     * @var string
28
+     */
29
+    protected $method;
30
+
31
+    /**
32
+     * Endpoint's callback.
33
+     *
34
+     * @var callable
35
+     */
36
+    protected $callback;
37
+
38
+    /**
39
+     * Endpoint's permission guard.
40
+     *
41
+     * @var GuardContract
42
+     */
43
+    protected $guard;
44
+
45
+    /**
46
+     * Endpoint's arguments filter.
47
+     *
48
+     * @var FilterContract
49
+     */
50
+    protected $filter;
51
+
52
+    /**
53
+     * Endpoint's route prefix.
54
+     *
55
+     * @var string
56
+     */
57
+    protected $prefix;
58
+
59
+    /**
60
+     * Instantiate a new endpoint with a provided route, method, and callback.
61
+     *
62
+     * @param string   $route
63
+     * @param string   $method
64
+     * @param callable $callback
65
+     *
66
+     * @throws MalformedRouteException
67
+     */
68
+    public function __construct( $route, $method, $callback ) {
69
+        if ( ! Str::starts_with( $route, '/' ) || Str::ends_with( $route, '/' ) ) {
70
+            throw new MalformedRouteException;
71
+        }
72
+
73
+        $this->route    = $route;
74
+        $this->method   = $method;
75
+        $this->callback = $callback;
76
+    }
77
+
78
+    /**
79
+     * Generates the endpoint's route.
80
+     *
81
+     * Combines the prefix with the route to generate the full route string.
82
+     *
83
+     * @return string
84
+     */
85
+    public function get_route() {
86
+        return ( $this->prefix ?: '' ) . $this->route;
87
+    }
88
+
89
+    /**
90
+     * Generates the endpoint's WP-API options array.
91
+     *
92
+     * @return array
93
+     */
94
+    public function get_options() {
95
+        $options = array(
96
+            'methods'  => $this->method,
97
+            'callback' => $this->callback,
98
+        );
99
+
100
+        if ( $this->guard ) {
101
+            $options['permission_callback'] = array( $this->guard, 'authorized' );
102
+        }
103
+
104
+        if ( $this->filter ) {
105
+            $options['args'] = $this->filter->rules();
106
+        }
107
+
108
+        return $options;
109
+    }
110
+
111
+    /**
112
+     * Sets the endpoint's permission guard.
113
+     *
114
+     * @param GuardContract $guard
115
+     *
116
+     * @return $this
117
+     */
118
+    public function set_guard( GuardContract $guard ) {
119
+        $this->guard = $guard;
120
+
121
+        return $this;
122
+    }
123
+
124
+    /**
125
+     * Sets the endpoint's arguments filter.
126
+     *
127
+     * @param FilterContract $filter
128
+     *
129
+     * @return $this
130
+     */
131
+    public function set_filter( FilterContract $filter ) {
132
+        $this->filter = $filter;
133
+
134
+        return $this;
135
+    }
136
+
137
+    /**
138
+     * Sets the endpoint's prefix.
139
+     *
140
+     * @param string $prefix
141
+     *
142
+     * @return $this
143
+     * @throws MalformedRouteException
144
+     */
145
+    public function set_prefix( $prefix ) {
146
+        if ( ! Str::starts_with( $prefix, '/' ) || Str::ends_with( $prefix, '/' ) ) {
147
+            throw new MalformedRouteException;
148
+        }
149
+
150
+        $this->prefix = $prefix;
151
+
152
+        return $this;
153
+    }
154 154
 }
Please login to merge, or discard this patch.
src/Http/Guard.php 2 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.
src/Axolotl/EntityManager.php 2 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.
src/Axolotl/Relationship/BelongsToOne.php 2 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\EntityManager;
5 5
 use Intraxia\Jaxion\Axolotl\Model;
6
-use Intraxia\Jaxion\Contract\Axolotl\UsesWordPressPost;
7 6
 use LogicException;
8 7
 
9 8
 /**
Please login to merge, or discard this patch.
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -13,110 +13,110 @@
 block discarded – undo
13 13
  * @subpackage Axolotl\Relationship
14 14
  */
15 15
 class BelongsToOne extends Root {
16
-	/**
17
-	 * Relationship's primary key name
18
-	 * on related model.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	protected $local_key = '';
23
-
24
-	/**
25
-	 * HasMany constructor.
26
-	 *
27
-	 * @param Model  $model
28
-	 * @param string $class
29
-	 * @param string $type
30
-	 * @param string $local_key
31
-	 */
32
-	public function __construct( Model $model, $class, $type, $local_key ) {
33
-		$this->local_key = $local_key;
34
-
35
-		parent::__construct( $model, $class, $type );
36
-	}
37
-
38
-	/**
39
-	 * {@inheritDoc}
40
-	 *
41
-	 * @param EntityManager $database
42
-	 *
43
-	 * @throws LogicException
44
-	 */
45
-	public function attach_relation( EntityManager $database ) {
46
-		if ( $this->get_model()->is_filling() ) {
47
-			return;
48
-		}
49
-
50
-		$this->get_model()->set_filling( true );
51
-
52
-		$id = $this->make_target_id();
53
-
54
-		if ( $id ) {
55
-			$target = $database->find(
56
-				$this->get_class(),
57
-				$id
58
-			);
59
-
60
-			$this->set_target( $target );
61
-		}
62
-
63
-		$this->get_model()->set_filling( false );
64
-	}
65
-
66
-	/**
67
-	 * {@inheritDoc}
68
-	 *
69
-	 * @return string
70
-	 */
71
-	public function get_sha() {
72
-		return sha1(
73
-			__CLASS__ .
74
-			get_class( $this->model ) .
75
-			$this->class .
76
-			$this->type .
77
-			$this->local_key
78
-		);
79
-	}
80
-
81
-	/**
82
-	 * Get the relationship's local key.
83
-	 *
84
-	 * @return string
85
-	 */
86
-	public function get_local_key() {
87
-		return $this->local_key;
88
-	}
89
-
90
-	/**
91
-	 * Gets the ID value for the target model to search by.
92
-	 *
93
-	 * @return int|false
94
-	 *
95
-	 * @throws LogicException
96
-	 */
97
-	protected function make_target_id() {
98
-		$class = $this->get_class();
99
-
100
-		switch ( $this->get_relationship_type() ) {
101
-			case 'post_post':
102
-				return $this->get_model()
103
-					->get_underlying_wp_object()
104
-					->{$this->local_key};
105
-			case 'post_term':
106
-				$terms = wp_get_post_terms(
107
-					$this->get_model()
108
-						->get_underlying_wp_object()
109
-						->ID,
110
-					$class::get_taxonomy()
111
-				);
112
-
113
-				if ( ! $terms || is_wp_error( $terms ) ) {
114
-					return false;
115
-				}
116
-
117
-				return $terms[0]->term_id;
118
-			default:
119
-				throw new LogicException;
120
-		}
121
-	}
16
+    /**
17
+     * Relationship's primary key name
18
+     * on related model.
19
+     *
20
+     * @var string
21
+     */
22
+    protected $local_key = '';
23
+
24
+    /**
25
+     * HasMany constructor.
26
+     *
27
+     * @param Model  $model
28
+     * @param string $class
29
+     * @param string $type
30
+     * @param string $local_key
31
+     */
32
+    public function __construct( Model $model, $class, $type, $local_key ) {
33
+        $this->local_key = $local_key;
34
+
35
+        parent::__construct( $model, $class, $type );
36
+    }
37
+
38
+    /**
39
+     * {@inheritDoc}
40
+     *
41
+     * @param EntityManager $database
42
+     *
43
+     * @throws LogicException
44
+     */
45
+    public function attach_relation( EntityManager $database ) {
46
+        if ( $this->get_model()->is_filling() ) {
47
+            return;
48
+        }
49
+
50
+        $this->get_model()->set_filling( true );
51
+
52
+        $id = $this->make_target_id();
53
+
54
+        if ( $id ) {
55
+            $target = $database->find(
56
+                $this->get_class(),
57
+                $id
58
+            );
59
+
60
+            $this->set_target( $target );
61
+        }
62
+
63
+        $this->get_model()->set_filling( false );
64
+    }
65
+
66
+    /**
67
+     * {@inheritDoc}
68
+     *
69
+     * @return string
70
+     */
71
+    public function get_sha() {
72
+        return sha1(
73
+            __CLASS__ .
74
+            get_class( $this->model ) .
75
+            $this->class .
76
+            $this->type .
77
+            $this->local_key
78
+        );
79
+    }
80
+
81
+    /**
82
+     * Get the relationship's local key.
83
+     *
84
+     * @return string
85
+     */
86
+    public function get_local_key() {
87
+        return $this->local_key;
88
+    }
89
+
90
+    /**
91
+     * Gets the ID value for the target model to search by.
92
+     *
93
+     * @return int|false
94
+     *
95
+     * @throws LogicException
96
+     */
97
+    protected function make_target_id() {
98
+        $class = $this->get_class();
99
+
100
+        switch ( $this->get_relationship_type() ) {
101
+            case 'post_post':
102
+                return $this->get_model()
103
+                    ->get_underlying_wp_object()
104
+                    ->{$this->local_key};
105
+            case 'post_term':
106
+                $terms = wp_get_post_terms(
107
+                    $this->get_model()
108
+                        ->get_underlying_wp_object()
109
+                        ->ID,
110
+                    $class::get_taxonomy()
111
+                );
112
+
113
+                if ( ! $terms || is_wp_error( $terms ) ) {
114
+                    return false;
115
+                }
116
+
117
+                return $terms[0]->term_id;
118
+            default:
119
+                throw new LogicException;
120
+        }
121
+    }
122 122
 }
Please login to merge, or discard this patch.
src/Http/Filter.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -12,134 +12,134 @@
 block discarded – undo
12 12
  * @subpackage Http
13 13
  */
14 14
 class Filter implements FilterContract {
15
-	/**
16
-	 * Filter rules.
17
-	 *
18
-	 * @var array
19
-	 */
20
-	protected $rules;
21
-
22
-	/**
23
-	 * Instantiates a new filter with the provided rules array.
24
-	 *
25
-	 * @param array $rules
26
-	 */
27
-	public function __construct( $rules = array() ) {
28
-		$this->rules = $rules;
29
-	}
30
-
31
-	/**
32
-	 * Generates argument rules.
33
-	 *
34
-	 * Returns an array matching the WP-API format for argument rules,
35
-	 * including sanitization, validation, required, or defaults.
36
-	 *
37
-	 * @return array
38
-	 */
39
-	public function rules() {
40
-		$args = array();
41
-
42
-		foreach ( $this->rules as $arg => $validation ) {
43
-			if ( ! $validation || ! is_string( $validation ) ) {
44
-				continue;
45
-			}
46
-
47
-			$args[ $arg ] = $this->parse_validation( $validation );
48
-		}
49
-
50
-		return $args;
51
-	}
52
-
53
-	/**
54
-	 * Parses a validation string into a WP-API compatible rule.
55
-	 *
56
-	 * @param string $validation
57
-	 *
58
-	 * @return array
59
-	 *
60
-	 * @todo The next rule added needs to refactor this process.
61
-	 */
62
-	protected function parse_validation( $validation ) {
63
-		$validation = explode( '|', $validation );
64
-
65
-		$rules = array();
66
-
67
-		foreach ( $validation as $rule ) {
68
-			if ( 0 === strpos( $rule, 'default' ) ) {
69
-				$rule_arr = explode( ':', $rule );
70
-
71
-				$rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
72
-			}
73
-
74
-			if ( 0 === strpos( $rule, 'oneof' ) ) {
75
-				list( $rule, $values ) = explode( ':', $rule );
76
-
77
-				$values   = explode( ',', $values );
78
-				$callback = function ( $value ) use ( $values ) {
79
-					if ( in_array( $value, $values, true ) ) {
80
-						return true;
81
-					}
82
-
83
-					return false;
84
-				};
85
-
86
-				$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
87
-			}
88
-
89
-			switch ( $rule ) {
90
-				case 'required':
91
-					$rules['required'] = true;
92
-					break;
93
-				case 'integer':
94
-					$callback                   = array( $this, 'validate_integer' );
95
-					$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
96
-
97
-					$callback                   = array( $this, 'make_integer' );
98
-					$rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
99
-					break;
100
-			}
101
-		}
102
-
103
-		return $rules;
104
-	}
105
-
106
-	/**
107
-	 * Validate that provided value is an integer.
108
-	 *
109
-	 * @param mixed $value
110
-	 *
111
-	 * @return bool
112
-	 */
113
-	public function validate_integer( $value ) {
114
-		return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
115
-	}
116
-
117
-	/**
118
-	 * Casts a provided value to an integer.
119
-	 *
120
-	 * @param mixed $value
121
-	 *
122
-	 * @return int
123
-	 */
124
-	public function make_integer( $value ) {
125
-		return (int) $value;
126
-	}
127
-
128
-	/**
129
-	 * Creates a new callback that connects the previous and next callback.
130
-	 *
131
-	 * @param callable $previous
132
-	 * @param callable $next
133
-	 *
134
-	 * @return \Closure;
135
-	 */
136
-	private function add_callback( $previous, $next ) {
137
-		return function ( $value ) use ( $previous, $next ) {
138
-			if ( call_user_func( $previous, $value ) ) {
139
-				return call_user_func( $next, $value );
140
-			}
141
-
142
-			return false;
143
-		};
144
-	}
15
+    /**
16
+     * Filter rules.
17
+     *
18
+     * @var array
19
+     */
20
+    protected $rules;
21
+
22
+    /**
23
+     * Instantiates a new filter with the provided rules array.
24
+     *
25
+     * @param array $rules
26
+     */
27
+    public function __construct( $rules = array() ) {
28
+        $this->rules = $rules;
29
+    }
30
+
31
+    /**
32
+     * Generates argument rules.
33
+     *
34
+     * Returns an array matching the WP-API format for argument rules,
35
+     * including sanitization, validation, required, or defaults.
36
+     *
37
+     * @return array
38
+     */
39
+    public function rules() {
40
+        $args = array();
41
+
42
+        foreach ( $this->rules as $arg => $validation ) {
43
+            if ( ! $validation || ! is_string( $validation ) ) {
44
+                continue;
45
+            }
46
+
47
+            $args[ $arg ] = $this->parse_validation( $validation );
48
+        }
49
+
50
+        return $args;
51
+    }
52
+
53
+    /**
54
+     * Parses a validation string into a WP-API compatible rule.
55
+     *
56
+     * @param string $validation
57
+     *
58
+     * @return array
59
+     *
60
+     * @todo The next rule added needs to refactor this process.
61
+     */
62
+    protected function parse_validation( $validation ) {
63
+        $validation = explode( '|', $validation );
64
+
65
+        $rules = array();
66
+
67
+        foreach ( $validation as $rule ) {
68
+            if ( 0 === strpos( $rule, 'default' ) ) {
69
+                $rule_arr = explode( ':', $rule );
70
+
71
+                $rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
72
+            }
73
+
74
+            if ( 0 === strpos( $rule, 'oneof' ) ) {
75
+                list( $rule, $values ) = explode( ':', $rule );
76
+
77
+                $values   = explode( ',', $values );
78
+                $callback = function ( $value ) use ( $values ) {
79
+                    if ( in_array( $value, $values, true ) ) {
80
+                        return true;
81
+                    }
82
+
83
+                    return false;
84
+                };
85
+
86
+                $rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
87
+            }
88
+
89
+            switch ( $rule ) {
90
+                case 'required':
91
+                    $rules['required'] = true;
92
+                    break;
93
+                case 'integer':
94
+                    $callback                   = array( $this, 'validate_integer' );
95
+                    $rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
96
+
97
+                    $callback                   = array( $this, 'make_integer' );
98
+                    $rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
99
+                    break;
100
+            }
101
+        }
102
+
103
+        return $rules;
104
+    }
105
+
106
+    /**
107
+     * Validate that provided value is an integer.
108
+     *
109
+     * @param mixed $value
110
+     *
111
+     * @return bool
112
+     */
113
+    public function validate_integer( $value ) {
114
+        return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
115
+    }
116
+
117
+    /**
118
+     * Casts a provided value to an integer.
119
+     *
120
+     * @param mixed $value
121
+     *
122
+     * @return int
123
+     */
124
+    public function make_integer( $value ) {
125
+        return (int) $value;
126
+    }
127
+
128
+    /**
129
+     * Creates a new callback that connects the previous and next callback.
130
+     *
131
+     * @param callable $previous
132
+     * @param callable $next
133
+     *
134
+     * @return \Closure;
135
+     */
136
+    private function add_callback( $previous, $next ) {
137
+        return function ( $value ) use ( $previous, $next ) {
138
+            if ( call_user_func( $previous, $value ) ) {
139
+                return call_user_func( $next, $value );
140
+            }
141
+
142
+            return false;
143
+        };
144
+    }
145 145
 }
Please login to merge, or discard this patch.
src/Http/Router.php 1 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.