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