|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cp\Parser; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class PlanParser |
|
7
|
|
|
*/ |
|
8
|
|
|
class PlanParser extends AbstractCpParser |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param string $url |
|
12
|
|
|
* |
|
13
|
|
|
* @return string |
|
14
|
|
|
* @throws \Exception |
|
15
|
|
|
*/ |
|
16
|
|
|
public function parseToJson($url) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->loadContent($url); |
|
19
|
|
|
$weeks = $this->parser->find('#plans table'); |
|
20
|
|
|
if (0 >= $weeks->count()) { |
|
21
|
|
|
throw new \Exception(sprintf('Plan not found for this url: %s', $url)); |
|
22
|
3 |
|
} |
|
23
|
|
|
|
|
24
|
3 |
|
$plan = $this->getPlan(); |
|
25
|
3 |
|
foreach ($weeks as $training) { |
|
|
|
|
|
|
26
|
|
|
$week = ['name' => $training->find('thead tr')->find('td')[1]->innerHtml]; |
|
27
|
|
|
foreach ($training->find('tbody tr') as $seance) { |
|
28
|
|
|
$training = [ |
|
29
|
|
|
'type' => strip_tags($seance->find('td')[0]->innerHtml), |
|
30
|
|
|
'content' => strip_tags($seance->find('td')[1]->innerHtml), |
|
31
|
|
|
]; |
|
32
|
3 |
|
$week['trainings'][] = $training; |
|
33
|
|
|
} |
|
34
|
3 |
|
$plan['weeks'][] = $week; |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
return json_encode($plan, true); |
|
38
|
|
|
} |
|
39
|
3 |
|
|
|
40
|
3 |
|
/** |
|
41
|
|
|
* @param string $url |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
* @throws \Exception |
|
45
|
|
|
*/ |
|
46
|
|
|
public function parseToHtml($url) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->loadContent($url); |
|
49
|
|
|
|
|
50
|
2 |
|
return sprintf('%s%s%s', '<table>', $this->parser->find('#plans table')->innerHtml, '</table>'); |
|
51
|
2 |
|
} |
|
52
|
2 |
|
|
|
53
|
1 |
|
/** |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
1 |
|
private function getPlan() |
|
57
|
1 |
|
{ |
|
58
|
1 |
|
$nameOfPlan = strip_tags($this->parser->find('.article-content-main h1')->innerHtml); |
|
59
|
1 |
|
$typeOfPlan = strip_tags($this->parser->find('.article-content-main h3')->innerHtml); |
|
60
|
|
|
|
|
61
|
1 |
|
return [ |
|
62
|
1 |
|
'name' => strip_tags($nameOfPlan), |
|
63
|
1 |
|
'type' => strip_tags($typeOfPlan), |
|
64
|
1 |
|
'weeks' => [], |
|
65
|
1 |
|
]; |
|
66
|
1 |
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.