| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Components\Rest_Api; |
| 4 |
|
| 5 |
class Route { |
| 6 |
|
| 7 |
/** |
| 8 |
* @var Route|null |
| 9 |
*/ |
| 10 |
private $parent_route; |
| 11 |
private $rest_namespace = ''; |
| 12 |
private $route = ''; |
| 13 |
private $override = false; |
| 14 |
/** |
| 15 |
* @var Interfaces\Endpoint_Interface[] |
| 16 |
*/ |
| 17 |
private $endpoints = array(); |
| 18 |
|
| 19 |
public function register() { |
| 20 |
register_rest_route( |
| 21 |
$this->get_namespace(), |
| 22 |
"/{$this->get_route()}", |
| 23 |
iterator_to_array( $this->resolve_endpoints() ), |
| 24 |
$this->is_override() |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
private function resolve_endpoints(): \Generator { |
| 29 |
foreach ( $this->endpoints as $endpoint ) { |
| 30 |
yield array( |
| 31 |
'methods' => $endpoint->get_method(), |
| 32 |
'callback' => array( $endpoint, 'process' ), |
| 33 |
'permission_callback' => array( $endpoint, 'has_permission' ), |
| 34 |
'args' => $endpoint->get_args(), |
| 35 |
); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @param Route|null $parent_route |
| 41 |
*/ |
| 42 |
public function set_parent_route( Route $parent_route ) { |
| 43 |
$this->parent_route = $parent_route; |
| 44 |
|
| 45 |
if ( ! $this->get_namespace() ) { |
| 46 |
$this->set_namespace( $parent_route->get_namespace() ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param string $rest_namespace |
| 52 |
*/ |
| 53 |
public function set_namespace( string $rest_namespace ) { |
| 54 |
$this->rest_namespace = $rest_namespace; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function get_namespace(): string { |
| 61 |
return $this->rest_namespace; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param string $route |
| 66 |
*/ |
| 67 |
public function set_route( string $route ) { |
| 68 |
$this->route = trim( $route, '/' ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function get_route(): string { |
| 75 |
if ( ! $this->parent_route ) { |
| 76 |
return $this->route; |
| 77 |
} |
| 78 |
|
| 79 |
return ( $this->parent_route->get_route() . '/' . $this->route ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param bool $override |
| 84 |
*/ |
| 85 |
public function set_override( bool $override ) { |
| 86 |
$this->override = $override; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @return bool |
| 91 |
*/ |
| 92 |
public function is_override(): bool { |
| 93 |
return $this->override; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param Interfaces\Endpoint_Interface[] $endpoints |
| 98 |
*/ |
| 99 |
public function set_endpoints( array $endpoints ) { |
| 100 |
$this->endpoints = array(); |
| 101 |
|
| 102 |
foreach ( $endpoints as $endpoint ) { |
| 103 |
$this->add_endpoint( $endpoint ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function add_endpoint( Interfaces\Endpoint_Interface $endpoint ) { |
| 108 |
$this->endpoints[] = $endpoint; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return Interfaces\Endpoint_Interface[] |
| 113 |
*/ |
| 114 |
public function get_endpoints(): array { |
| 115 |
return $this->endpoints; |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|