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
|
2 |
|
public function parseToJson($url) |
17
|
|
|
{ |
18
|
2 |
|
$this->loadContent($url); |
19
|
2 |
|
$weeks = $this->parser->find('#plans table'); |
20
|
2 |
|
if (0 >= $weeks->count()) { |
21
|
1 |
|
throw new \Exception(sprintf('Plan not found for this url: %s', $url)); |
22
|
|
|
} |
23
|
|
|
|
24
|
1 |
|
$plan = $this->getPlan(); |
25
|
1 |
|
foreach ($weeks as $training) { |
|
|
|
|
26
|
1 |
|
$week = ['name' => $training->find('thead tr')->find('td')[1]->innerHtml]; |
27
|
1 |
|
foreach ($training->find('tbody tr') as $seance) { |
28
|
|
|
$training = [ |
29
|
1 |
|
'type' => strip_tags($seance->find('td')[0]->innerHtml), |
30
|
1 |
|
'content' => strip_tags($seance->find('td')[1]->innerHtml), |
31
|
1 |
|
]; |
32
|
1 |
|
$week['trainings'][] = $training; |
33
|
1 |
|
} |
34
|
1 |
|
$plan['weeks'][] = $week; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
1 |
|
return json_encode($plan, true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $url |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
1 |
|
public function parseToHtml($url) |
47
|
|
|
{ |
48
|
1 |
|
$this->loadContent($url); |
49
|
|
|
|
50
|
1 |
|
return sprintf('%s%s%s', '<table>', $this->parser->find('#plans table')->innerHtml, '</table>'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
1 |
|
private function getPlan() |
57
|
|
|
{ |
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
|
|
|
return [ |
62
|
1 |
|
'name' => strip_tags($nameOfPlan), |
63
|
1 |
|
'type' => strip_tags($typeOfPlan), |
64
|
1 |
|
'weeks' => [], |
65
|
1 |
|
]; |
66
|
|
|
} |
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.