| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Schema_Aggregator\Domain; |
| 5 |
|
| 6 |
/** |
| 7 |
* Represents a piece of schema.org data. |
| 8 |
*/ |
| 9 |
class Schema_Piece { |
| 10 |
|
| 11 |
/** |
| 12 |
* The type(s) of the schema piece. |
| 13 |
* |
| 14 |
* @var string|array<string> |
| 15 |
*/ |
| 16 |
private $type; |
| 17 |
|
| 18 |
/** |
| 19 |
* The data of the schema piece. |
| 20 |
* |
| 21 |
* @var array<string, string|int|bool> |
| 22 |
*/ |
| 23 |
private $data; |
| 24 |
|
| 25 |
/** |
| 26 |
* Class constructor. |
| 27 |
* |
| 28 |
* @param array<string, string|int|bool> $data The data of the schema piece. |
| 29 |
* @param string|array<string> $type The type of the schema piece. |
| 30 |
*/ |
| 31 |
public function __construct( array $data, $type ) { |
| 32 |
$this->data = $data; |
| 33 |
$this->type = $type; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Gets the type of the schema piece. |
| 38 |
* |
| 39 |
* @return string|array<string> The type(s) of the schema piece. |
| 40 |
*/ |
| 41 |
public function get_type() { |
| 42 |
return $this->type; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Gets the data of the schema piece. |
| 47 |
* |
| 48 |
* @return array<string, string|int|bool> The data of the schema piece. |
| 49 |
*/ |
| 50 |
public function get_data(): array { |
| 51 |
return $this->data; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Gets the ID of the schema piece. |
| 56 |
* |
| 57 |
* @return string|null The ID of the schema piece, or null if not set. |
| 58 |
*/ |
| 59 |
public function get_id(): ?string { |
| 60 |
return ( $this->data['@id'] ?? null ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Converts multiple schema pieces to a JSON-LD-encoded graph. |
| 65 |
* |
| 66 |
* @return array<string, string|int|bool> The JSON-LD graph representation. |
| 67 |
*/ |
| 68 |
public function to_json_ld_graph(): array { |
| 69 |
return [ |
| 70 |
'@graph' => $this->data, |
| 71 |
]; |
| 72 |
} |
| 73 |
} |
| 74 |
|