|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Christian Gripp <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Core23\LastFm\Crawler; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
15
|
|
|
|
|
16
|
|
|
final class UserEventCrawler extends AbstractCrawler implements UserEventCrawlerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getUserYears(string $username): ?array |
|
22
|
|
|
{ |
|
23
|
|
|
$node = $this->crawlUrl($username); |
|
24
|
|
|
|
|
25
|
|
|
if (null === $node) { |
|
26
|
|
|
return null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$years = $node->filter('.content-top .secondary-nav-item-link') |
|
30
|
|
|
->each(static function (Crawler $node) { |
|
31
|
|
|
return (int) trim($node->text()); |
|
32
|
|
|
}) |
|
33
|
|
|
; |
|
34
|
|
|
|
|
35
|
|
|
sort($years); |
|
36
|
|
|
array_shift($years); |
|
37
|
|
|
|
|
38
|
|
|
return $years; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getEvents(string $username, ?int $year, int $page = 1): ?array |
|
45
|
|
|
{ |
|
46
|
|
|
$node = $this->crawlUrl($username, $year, $page); |
|
47
|
|
|
|
|
48
|
|
|
if (null === $node) { |
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->crawlEventList($node); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getYearPages(string $username, ?int $year): ?int |
|
59
|
|
|
{ |
|
60
|
|
|
$node = $this->crawlUrl($username, $year); |
|
61
|
|
|
|
|
62
|
|
|
if (null === $node) { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->countListPages($node); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getYearCount(string $username, ?int $year, int $page = 1): ?int |
|
73
|
|
|
{ |
|
74
|
|
|
$node = $this->crawlUrl($username, $year, $page); |
|
75
|
|
|
|
|
76
|
|
|
if (null === $node) { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$perPage = $this->countListEvents($node); |
|
81
|
|
|
$pages = $this->countListPages($node); |
|
82
|
|
|
|
|
83
|
|
|
if ($pages) { |
|
84
|
|
|
$node = $this->crawlUrl($username, $year, $pages); |
|
85
|
|
|
|
|
86
|
|
|
if (null === $node) { |
|
87
|
|
|
return $perPage; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$count = $this->countListEvents($node); |
|
91
|
|
|
|
|
92
|
|
|
return ($pages - 1) * $perPage + $count; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $perPage; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Crawler $node |
|
100
|
|
|
* |
|
101
|
|
|
* @return int |
|
102
|
|
|
*/ |
|
103
|
|
|
private function countListPages(Crawler $node): int |
|
104
|
|
|
{ |
|
105
|
|
|
$pagination = $this->parseString($node->filter('.pagination .pages')); |
|
106
|
|
|
|
|
107
|
|
|
return $pagination ? (int) preg_replace('/.* of /', '', $pagination) : 1; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param Crawler $node |
|
112
|
|
|
* |
|
113
|
|
|
* @return int |
|
114
|
|
|
*/ |
|
115
|
|
|
private function countListEvents(Crawler $node): int |
|
116
|
|
|
{ |
|
117
|
|
|
return $node->filter('.events-list-item')->count(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $username |
|
122
|
|
|
* @param int|null $year |
|
123
|
|
|
* @param int $page |
|
124
|
|
|
* |
|
125
|
|
|
* @return Crawler|null |
|
126
|
|
|
*/ |
|
127
|
|
|
private function crawlUrl(string $username, ?int $year = null, int $page = 1): ?Crawler |
|
128
|
|
|
{ |
|
129
|
|
|
$url = 'http://www.last.fm/user/'.$username.'/events/'.($year ?: '').'?page='.$page; |
|
130
|
|
|
|
|
131
|
|
|
return $this->crawl($url); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.