PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.8.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.8.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Page_Cache / Meta / Meta.php

Meta.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.8.0, at src/Performance/Page_Cache/Meta/Meta.php

85 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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