@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | protected $callback = null; |
| 37 | 37 | |
| 38 | 38 | |
| 39 | - public function __construct( string $method, string $route ) { |
|
| 39 | + public function __construct(string $method, string $route) { |
|
| 40 | 40 | $this->method = $method; |
| 41 | - $this->route = $this->format_route( $route ); |
|
| 41 | + $this->route = $this->format_route($route); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | * @param string $route |
| 48 | 48 | * @return string |
| 49 | 49 | */ |
| 50 | - protected function format_route( string $route ): string { |
|
| 51 | - $route = $this->translate_arguments( $route ); |
|
| 52 | - return '/' . ltrim( $route, '/\\' ); |
|
| 50 | + protected function format_route(string $route): string { |
|
| 51 | + $route = $this->translate_arguments($route); |
|
| 52 | + return '/'.ltrim($route, '/\\'); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -59,27 +59,27 @@ discard block |
||
| 59 | 59 | * @param string $route |
| 60 | 60 | * @return string |
| 61 | 61 | */ |
| 62 | - protected function translate_arguments( string $route ): string { |
|
| 63 | - if ( preg_match( '/[^-:\/_{}()a-zA-Z\d]/', $route ) ) { |
|
| 62 | + protected function translate_arguments(string $route): string { |
|
| 63 | + if (preg_match('/[^-:\/_{}()a-zA-Z\d]/', $route)) { |
|
| 64 | 64 | return $route; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | // Create capture group for ":parameter" |
| 68 | 68 | $allowed_chars = '[\@a-zA-Z0-9&.?:-_=#]*'; |
| 69 | 69 | $route = preg_replace( |
| 70 | - '/:(' . $allowed_chars . ')/', |
|
| 71 | - '(?<$1>' . $allowed_chars . ')', |
|
| 70 | + '/:('.$allowed_chars.')/', |
|
| 71 | + '(?<$1>'.$allowed_chars.')', |
|
| 72 | 72 | $route |
| 73 | 73 | ); |
| 74 | 74 | |
| 75 | 75 | // Create capture group for '{parameter}' |
| 76 | 76 | $route = preg_replace( |
| 77 | - '/{(' . $allowed_chars . ')}/', |
|
| 78 | - '(?<$1>' . $allowed_chars . ')', |
|
| 79 | - is_string( $route ) ? $route : '' |
|
| 77 | + '/{('.$allowed_chars.')}/', |
|
| 78 | + '(?<$1>'.$allowed_chars.')', |
|
| 79 | + is_string($route) ? $route : '' |
|
| 80 | 80 | ); |
| 81 | 81 | |
| 82 | - return is_string( $route ) ? $route : ''; |
|
| 82 | + return is_string($route) ? $route : ''; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return self |
| 100 | 100 | */ |
| 101 | - public function callback( callable $callback ): self { |
|
| 101 | + public function callback(callable $callback): self { |
|
| 102 | 102 | $this->callback = $callback; |
| 103 | 103 | return $this; |
| 104 | 104 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | * @param string $namespace |
| 128 | 128 | * @return static |
| 129 | 129 | */ |
| 130 | - public function namespace( string $namespace ) { |
|
| 130 | + public function namespace(string $namespace) { |
|
| 131 | 131 | $this->namespace = $namespace; |
| 132 | 132 | return $this; |
| 133 | 133 | } |
@@ -138,27 +138,27 @@ discard block |
||
| 138 | 138 | * @param string $method |
| 139 | 139 | * @return self |
| 140 | 140 | */ |
| 141 | - public function with_method( string $method ): self { |
|
| 142 | - $clone = new self( $method, $this->get_route() ); |
|
| 141 | + public function with_method(string $method): self { |
|
| 142 | + $clone = new self($method, $this->get_route()); |
|
| 143 | 143 | // Arguments |
| 144 | - if ( ! empty( $this->arguments ) ) { |
|
| 145 | - foreach ( $this->arguments as $argument ) { |
|
| 146 | - $clone->argument( $argument ); |
|
| 144 | + if ( ! empty($this->arguments)) { |
|
| 145 | + foreach ($this->arguments as $argument) { |
|
| 146 | + $clone->argument($argument); |
|
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | // Authentication |
| 150 | - if ( ! empty( $this->authentication ) ) { |
|
| 151 | - foreach ( $this->authentication as $authentication ) { |
|
| 152 | - $clone->authentication( $authentication ); |
|
| 150 | + if ( ! empty($this->authentication)) { |
|
| 151 | + foreach ($this->authentication as $authentication) { |
|
| 152 | + $clone->authentication($authentication); |
|
| 153 | 153 | } |
| 154 | 154 | } |
| 155 | 155 | // Callback |
| 156 | - if ( ! empty( $this->callback ) ) { |
|
| 157 | - $clone->callback( $this->callback ); |
|
| 156 | + if ( ! empty($this->callback)) { |
|
| 157 | + $clone->callback($this->callback); |
|
| 158 | 158 | } |
| 159 | 159 | // Namespace |
| 160 | - if ( ! empty( $this->namespace ) ) { |
|
| 161 | - $clone->namespace( $this->namespace ); |
|
| 160 | + if ( ! empty($this->namespace)) { |
|
| 161 | + $clone->namespace($this->namespace); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | return $clone; |
@@ -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 ) { |
|
| 127 | + if (count($callbacks) === 1) { |
|
| 128 | 128 | return $callbacks[0]; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - return Utils::compose_conditional_all_true( ...$callbacks ); |
|
| 131 | + return Utils::compose_conditional_all_true(...$callbacks); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | |
@@ -24,9 +24,9 @@ discard block |
||
| 24 | 24 | * @return self |
| 25 | 25 | * @code 101 |
| 26 | 26 | */ |
| 27 | - public static function namespace_not_defined( string $route ): self { |
|
| 27 | + public static function namespace_not_defined(string $route): self { |
|
| 28 | 28 | return new self( |
| 29 | - sprintf( 'Namespace not defined in %s', $route ), |
|
| 29 | + sprintf('Namespace not defined in %s', $route), |
|
| 30 | 30 | 101 |
| 31 | 31 | ); |
| 32 | 32 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @return self |
| 39 | 39 | * @code 102 |
| 40 | 40 | */ |
| 41 | - public static function callback_not_defined( Route $route ): self { |
|
| 41 | + public static function callback_not_defined(Route $route): self { |
|
| 42 | 42 | // Set the namespace if exists. |
| 43 | 43 | $namespace = '' !== $route->get_namespace() |
| 44 | 44 | ? $route->get_namespace() |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | return new self( |
| 48 | 48 | sprintf( |
| 49 | 49 | 'Callback not defined for [%s] %s%s', |
| 50 | - strtoupper( $route->get_method() ), |
|
| 51 | - strtoupper( $namespace ), |
|
| 52 | - strtoupper( $route->get_route() ) |
|
| 50 | + strtoupper($route->get_method()), |
|
| 51 | + strtoupper($namespace), |
|
| 52 | + strtoupper($route->get_route()) |
|
| 53 | 53 | ), |
| 54 | 54 | 102 |
| 55 | 55 | ); |
@@ -61,11 +61,11 @@ discard block |
||
| 61 | 61 | * @param Route $route |
| 62 | 62 | * @return self |
| 63 | 63 | */ |
| 64 | - public static function invalid_http_method( Route $route ): self { |
|
| 64 | + public static function invalid_http_method(Route $route): self { |
|
| 65 | 65 | return new self( |
| 66 | 66 | sprintf( |
| 67 | 67 | '%s is a none supported HTTP Mehtod.', |
| 68 | - strtoupper( $route->get_method() ) |
|
| 68 | + strtoupper($route->get_method()) |
|
| 69 | 69 | ), |
| 70 | 70 | 103 |
| 71 | 71 | ); |