-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathItem.php
More file actions
103 lines (92 loc) · 2.32 KB
/
Item.php
File metadata and controls
103 lines (92 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace Brick\StructuredData;
/**
* An item, such as a Thing in schema.org's vocabulary.
*/
final class Item
{
/**
* The global identifier of the item, if any.
*/
private readonly ?string $id;
/**
* The types this Item implements, as URLs.
*
* @var string[]
*/
private readonly array $types;
/**
* The properties, as a map of property name to list of values.
*
* @var array<string, array<Item|string>>
*/
private array $properties = [];
/**
* Item constructor.
*
* @param string|null $id An optional global identifier for the item.
* @param string ...$types The types this Item implements, as URLs, e.g. http://schema.org/Product .
*/
public function __construct(?string $id, string ...$types)
{
$this->id = $id;
$this->types = $types;
}
/**
* Returns the global identifier of the item, if any.
*
* @return string|null
*/
public function getId() : ?string
{
return $this->id;
}
/**
* Returns the list of types this Item implements.
*
* Each type is represented as a URL, e.g. http://schema.org/Product .
*
* @return string[]
*/
public function getTypes() : array
{
return $this->types;
}
/**
* Returns a map of property name to list of values.
*
* Property names are represented as URLs, e.g. http://schema.org/price .
* Values are a list of Item instances or plain strings.
*
* @return array<string, array<Item|string>>
*/
public function getProperties() : array
{
return $this->properties;
}
/**
* Returns a list of values for the given property.
*
* The result is a list of Item instances or plain strings.
* If the property does not exist, an empty array is returned.
*
* @param string $name
*
* @return array<Item|string>
*/
public function getProperty(string $name) : array
{
return $this->properties[$name] ?? [];
}
/**
* @param string $name
* @param Item|string $value
*
* @return void
*/
public function addProperty(string $name, Item|string $value) : void
{
$this->properties[$name][] = $value;
}
}