| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 4 |
namespace Yoast\WP\SEO\Routes\Endpoint; |
| 5 |
|
| 6 |
/** |
| 7 |
* List of endpoints. |
| 8 |
*/ |
| 9 |
class Endpoint_List { |
| 10 |
|
| 11 |
/** |
| 12 |
* Holds the endpoints. |
| 13 |
* |
| 14 |
* @var array<Endpoint_Interface> |
| 15 |
*/ |
| 16 |
private $endpoints = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Adds an endpoint to the list. |
| 20 |
* |
| 21 |
* @param Endpoint_Interface $endpoint An endpoint. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function add_endpoint( Endpoint_Interface $endpoint ): void { |
| 26 |
$this->endpoints[] = $endpoint; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Converts the list to an array. |
| 31 |
* |
| 32 |
* @return array<string, string> The array of endpoints. |
| 33 |
*/ |
| 34 |
public function to_array(): array { |
| 35 |
$result = []; |
| 36 |
foreach ( $this->endpoints as $endpoint ) { |
| 37 |
$result[ $endpoint->get_name() ] = $endpoint->get_url(); |
| 38 |
} |
| 39 |
|
| 40 |
return $result; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Converts the list to an array of paths (namespace + route, without host). |
| 45 |
* |
| 46 |
* @return array<string, string> The array of endpoint paths. |
| 47 |
*/ |
| 48 |
public function to_paths_array(): array { |
| 49 |
$result = []; |
| 50 |
foreach ( $this->endpoints as $endpoint ) { |
| 51 |
$result[ $endpoint->get_name() ] = $endpoint->get_namespace() . $endpoint->get_route(); |
| 52 |
} |
| 53 |
|
| 54 |
return $result; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Merges two Endpoint_List objects together. |
| 59 |
* Returns the current instance for method chaining. |
| 60 |
* |
| 61 |
* @param Endpoint_List $other_list The other Endpoint_List to merge with. |
| 62 |
* |
| 63 |
* @return $this |
| 64 |
*/ |
| 65 |
public function merge_with( Endpoint_List $other_list ): Endpoint_List { |
| 66 |
foreach ( $other_list->endpoints as $endpoint ) { |
| 67 |
$this->add_endpoint( $endpoint ); |
| 68 |
} |
| 69 |
|
| 70 |
return $this; |
| 71 |
} |
| 72 |
} |
| 73 |
|