| 1 |
<?php |
| 2 |
/** |
| 3 |
* The meta value object. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Page_Cache\Meta; |
| 11 |
|
| 12 |
use InvalidArgumentException; |
| 13 |
use SolidWP\Performance\Http\Header; |
| 14 |
|
| 15 |
/** |
| 16 |
* The meta value object. |
| 17 |
* |
| 18 |
* @package SolidWP\Performance |
| 19 |
*/ |
| 20 |
final class Meta { |
| 21 |
|
| 22 |
/** |
| 23 |
* The URL this meta is associated with. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public string $url; |
| 28 |
|
| 29 |
/** |
| 30 |
* The header object which contains the response headers. |
| 31 |
* |
| 32 |
* @var Header |
| 33 |
*/ |
| 34 |
public Header $headers; |
| 35 |
|
| 36 |
/** |
| 37 |
* @param string $url The URL this meta is associated with. |
| 38 |
* @param Header $headers The header object which contains the response headers. |
| 39 |
* |
| 40 |
* @throws InvalidArgumentException If the $url argument is empty. |
| 41 |
*/ |
| 42 |
private function __construct( string $url, Header $headers ) { |
| 43 |
if ( ! $url ) { |
| 44 |
throw new InvalidArgumentException( 'The $url argument cannot be empty' ); |
| 45 |
} |
| 46 |
|
| 47 |
$this->url = $url; |
| 48 |
$this->headers = $headers; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates the value object. |
| 53 |
* |
| 54 |
* @param array{url: string, headers: Header|array<string, string[]>} $data The meta data. |
| 55 |
* |
| 56 |
* @throws InvalidArgumentException If the $url argument is empty. |
| 57 |
* |
| 58 |
* @return self |
| 59 |
*/ |
| 60 |
public static function from( array $data ): self { |
| 61 |
$headers = $data['headers']; |
| 62 |
|
| 63 |
if ( ! $headers instanceof Header ) { |
| 64 |
$headers = ( new Header() )->replace( $headers ); |
| 65 |
} |
| 66 |
|
| 67 |
return new self( |
| 68 |
$data['url'], |
| 69 |
$headers |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Convert the value object to an array. |
| 75 |
* |
| 76 |
* @return array{url: string, headers: array<string, string[]>} |
| 77 |
*/ |
| 78 |
public function to_array(): array { |
| 79 |
return [ |
| 80 |
'url' => $this->url, |
| 81 |
'headers' => $this->headers->all(), |
| 82 |
]; |
| 83 |
} |
| 84 |
} |
| 85 |
|