@@ -35,8 +35,8 @@ discard block |
||
| 35 | 35 | * @throws Route_Exception (code 101) |
| 36 | 36 | */ |
| 37 | 37 | private function get_namespace(): string { |
| 38 | - if ( ! is_string( $this->namespace ) || mb_strlen( $this->namespace ) === 0 ) { |
|
| 39 | - throw Route_Exception::namespace_not_defined( get_class( $this ) ); |
|
| 38 | + if ( ! is_string($this->namespace) || mb_strlen($this->namespace) === 0) { |
|
| 39 | + throw Route_Exception::namespace_not_defined(get_class($this)); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | return $this->namespace; |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * @throws Route_Exception (code 101) |
| 50 | 50 | */ |
| 51 | 51 | private function get_factory(): Route_Factory { |
| 52 | - return Route_Factory::for( $this->get_namespace() ); |
|
| 52 | + return Route_Factory::for ($this->get_namespace()); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -58,10 +58,10 @@ discard block |
||
| 58 | 58 | * @param Route_Collection $collection |
| 59 | 59 | * @return Route_Collection |
| 60 | 60 | */ |
| 61 | - final public function get_routes( Route_Collection $collection ): Route_Collection { |
|
| 62 | - $routes = $this->define_routes( $this->get_factory() ); |
|
| 63 | - foreach ( $routes as $route ) { |
|
| 64 | - $collection->add_route( $route ); |
|
| 61 | + final public function get_routes(Route_Collection $collection): Route_Collection { |
|
| 62 | + $routes = $this->define_routes($this->get_factory()); |
|
| 63 | + foreach ($routes as $route) { |
|
| 64 | + $collection->add_route($route); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | return $collection; |
@@ -73,6 +73,6 @@ discard block |
||
| 73 | 73 | * @param Route_Factory $factory |
| 74 | 74 | * @return array<Route|Route_Group> |
| 75 | 75 | */ |
| 76 | - abstract protected function define_routes( Route_Factory $factory): array; |
|
| 76 | + abstract protected function define_routes(Route_Factory $factory): array; |
|
| 77 | 77 | |
| 78 | 78 | } |
@@ -26,10 +26,10 @@ discard block |
||
| 26 | 26 | * @param \PinkCrab\Route\Route\Route $route |
| 27 | 27 | * @return callable |
| 28 | 28 | */ |
| 29 | - public function create_callback( Route $route ): callable { |
|
| 30 | - return function() use ( $route ): void { |
|
| 31 | - $model = $this->map_to_wp_rest( $route ); |
|
| 32 | - register_rest_route( $model->namespace, $model->route, $model->args ); |
|
| 29 | + public function create_callback(Route $route): callable { |
|
| 30 | + return function() use ($route): void { |
|
| 31 | + $model = $this->map_to_wp_rest($route); |
|
| 32 | + register_rest_route($model->namespace, $model->route, $model->args); |
|
| 33 | 33 | }; |
| 34 | 34 | } |
| 35 | 35 | |
@@ -39,11 +39,11 @@ discard block |
||
| 39 | 39 | * @param \PinkCrab\Route\Route\Route $route |
| 40 | 40 | * @return WP_Rest_Route |
| 41 | 41 | */ |
| 42 | - public function map_to_wp_rest( Route $route ): WP_Rest_Route { |
|
| 42 | + public function map_to_wp_rest(Route $route): WP_Rest_Route { |
|
| 43 | 43 | $wp_rest = new WP_Rest_Route(); |
| 44 | 44 | $wp_rest->namespace = $route->get_namespace(); |
| 45 | 45 | $wp_rest->route = $route->get_route(); |
| 46 | - $wp_rest->args = $this->parse_options( $route ); |
|
| 46 | + $wp_rest->args = $this->parse_options($route); |
|
| 47 | 47 | return $wp_rest; |
| 48 | 48 | } |
| 49 | 49 | |
@@ -54,23 +54,23 @@ discard block |
||
| 54 | 54 | * @return array<mixed> |
| 55 | 55 | * @throws Route_Exception |
| 56 | 56 | */ |
| 57 | - protected function parse_options( Route $route ): array { |
|
| 57 | + protected function parse_options(Route $route): array { |
|
| 58 | 58 | |
| 59 | 59 | // If we have no callback defined for route, throw. |
| 60 | - if ( is_null( $route->get_callback() ) ) { |
|
| 61 | - throw Route_Exception::callback_not_defined( $route ); |
|
| 60 | + if (is_null($route->get_callback())) { |
|
| 61 | + throw Route_Exception::callback_not_defined($route); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | // If we have an invlaid method, throw |
| 65 | - if ( ! $this->is_valid_method( $route->get_method() ) ) { |
|
| 66 | - throw Route_Exception::invalid_http_method( $route ); |
|
| 65 | + if ( ! $this->is_valid_method($route->get_method())) { |
|
| 66 | + throw Route_Exception::invalid_http_method($route); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | $options = array(); |
| 70 | 70 | $options['methods'] = $route->get_method(); |
| 71 | 71 | $options['callback'] = $route->get_callback(); |
| 72 | - $options['permission_callback'] = $this->compose_permission_callback( $route ); |
|
| 73 | - $options['args'] = $this->parse_args( $route ); |
|
| 72 | + $options['permission_callback'] = $this->compose_permission_callback($route); |
|
| 73 | + $options['args'] = $this->parse_args($route); |
|
| 74 | 74 | |
| 75 | 75 | return $options; |
| 76 | 76 | } |
@@ -81,11 +81,11 @@ discard block |
||
| 81 | 81 | * @param Route $route |
| 82 | 82 | * @return array<mixed> |
| 83 | 83 | */ |
| 84 | - protected function parse_args( Route $route ): array { |
|
| 84 | + protected function parse_args(Route $route): array { |
|
| 85 | 85 | return array_reduce( |
| 86 | 86 | $route->get_arguments(), |
| 87 | - function( array $args, Argument $argument ) { |
|
| 88 | - $args[ $argument->get_key() ] = Argument_Parser::as_single( $argument ); |
|
| 87 | + function(array $args, Argument $argument) { |
|
| 88 | + $args[$argument->get_key()] = Argument_Parser::as_single($argument); |
|
| 89 | 89 | return $args; |
| 90 | 90 | }, |
| 91 | 91 | array() |
@@ -98,12 +98,12 @@ discard block |
||
| 98 | 98 | * @param string $method |
| 99 | 99 | * @return boolean |
| 100 | 100 | */ |
| 101 | - protected function is_valid_method( string $method ): bool { |
|
| 101 | + protected function is_valid_method(string $method): bool { |
|
| 102 | 102 | return in_array( |
| 103 | 103 | $method, |
| 104 | 104 | apply_filters( |
| 105 | 105 | 'pinkcrab/route/accepted_http_methods', // phpcs:ignore WordPress.NamingConventions.ValidHookName |
| 106 | - array( Route::DELETE, Route::POST, Route::PUT, Route::PATCH, Route::GET ) |
|
| 106 | + array(Route::DELETE, Route::POST, Route::PUT, Route::PATCH, Route::GET) |
|
| 107 | 107 | ), |
| 108 | 108 | true |
| 109 | 109 | ); |
@@ -115,20 +115,20 @@ discard block |
||
| 115 | 115 | * @param Route $route |
| 116 | 116 | * @return callable |
| 117 | 117 | */ |
| 118 | - protected function compose_permission_callback( Route $route ): callable { |
|
| 118 | + protected function compose_permission_callback(Route $route): callable { |
|
| 119 | 119 | $callbacks = $route->get_authentication(); |
| 120 | 120 | |
| 121 | 121 | // If we have no callback defined, use return true. |
| 122 | - if ( count( $callbacks ) === 0 ) { |
|
| 122 | + if (count($callbacks) === 0) { |
|
| 123 | 123 | return '__return_true'; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | // If we only have 1, return as is. |
| 127 | - if ( count( $callbacks ) === 1 ) { |
|
| 128 | - return reset( $callbacks ); |
|
| 127 | + if (count($callbacks) === 1) { |
|
| 128 | + return reset($callbacks); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - return all( ...$callbacks ); |
|
| 131 | + return all(...$callbacks); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | |