1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
use TypedArrays\AbstractTypedArray; |
6
|
|
|
|
7
|
|
|
require getcwd() . '/vendor/autoload.php'; |
8
|
|
|
|
9
|
|
|
interface InterfacePublication |
10
|
|
|
{ |
11
|
|
|
public function getPublicationName(): string; |
12
|
|
|
|
13
|
|
|
public function getPublicationUrl(): string; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
final class Article implements InterfacePublication |
17
|
|
|
{ |
18
|
|
|
private int $id; |
19
|
|
|
private string $name; |
20
|
|
|
private string $slug; |
21
|
|
|
|
22
|
|
|
public function __construct(int $id, string $name, string $slug) |
23
|
|
|
{ |
24
|
|
|
$this->id = $id; |
25
|
|
|
$this->name = $name; |
26
|
|
|
$this->slug = $slug; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getPublicationName(): string |
30
|
|
|
{ |
31
|
|
|
return $this->name; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getPublicationUrl(): string |
35
|
|
|
{ |
36
|
|
|
return "https://example.org/articles/{$this->id}-{$this->slug}"; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
final class YouTubeVideo implements InterfacePublication |
41
|
|
|
{ |
42
|
|
|
private string $id; |
43
|
|
|
private string $name; |
44
|
|
|
|
45
|
|
|
public function __construct(string $id, string $name) |
46
|
|
|
{ |
47
|
|
|
$this->id = $id; |
48
|
|
|
$this->name = $name; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getPublicationName(): string |
52
|
|
|
{ |
53
|
|
|
return $this->name; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getPublicationUrl(): string |
57
|
|
|
{ |
58
|
|
|
return "https://www.youtube.com/watch?v={$this->id}"; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
final class PublicationList extends AbstractTypedArray |
63
|
|
|
{ |
64
|
|
|
protected function typeToEnforce(): string |
65
|
|
|
{ |
66
|
|
|
return InterfacePublication::class; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function isMutable(): bool |
70
|
|
|
{ |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function collectionType(): string |
75
|
|
|
{ |
76
|
|
|
return self::COLLECTION_TYPE_LIST; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$publications = new PublicationList([ |
81
|
|
|
new Article(1, 'TypedArrays is awesome', 'typed-arrays-is-awesome'), |
|
|
|
|
82
|
|
|
new YouTubeVideo('dQw4w9WgXcQ', 'Lil cute kittens'), |
83
|
|
|
new Article(2, 'Indeterminate graviton harmonics chain reaction', 'indeterminate-graviton-harmonics-chain-reaction'), |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
renderPublicationList($publications); |
87
|
|
|
|
88
|
|
|
function renderPublicationList(PublicationList $publications): void |
89
|
|
|
{ |
90
|
|
|
print '<ul>' . PHP_EOL; |
91
|
|
|
foreach ($publications as $publication) { |
92
|
|
|
print sprintf( |
93
|
|
|
' <li><a href="%s">%s</a></li>' . PHP_EOL, |
94
|
|
|
$publication->getPublicationUrl(), |
95
|
|
|
$publication->getPublicationName() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
print '</ul>' . PHP_EOL; |
99
|
|
|
} |
100
|
|
|
|