| @@ 18-52 (lines=35) @@ | ||
| 15 | use DOMXPath; |
|
| 16 | use InvalidArgumentException; |
|
| 17 | ||
| 18 | class EventEndDate |
|
| 19 | { |
|
| 20 | protected $timezone; |
|
| 21 | ||
| 22 | public function __construct(DateTimeZone $timezone) |
|
| 23 | { |
|
| 24 | $this->timezone = $timezone; |
|
| 25 | } |
|
| 26 | ||
| 27 | public function parse(DOMDocument $dom, DOMXPath $xpath) : DateTimeImmutable |
|
| 28 | { |
|
| 29 | // This expression does not work. It looks like the reason is the array-notation... |
|
| 30 | //$endDate = $xpath->query('//div[contains(text()[2], "event ends")]/following-sibling::h2'); |
|
| 31 | $startDate = $xpath->query('//div[contains(text(), "event ends")]'); |
|
| 32 | ||
| 33 | if (! $startDate || $startDate->length == 0) { |
|
| 34 | // This expression does not work. It looks like the reason is the array-notation... |
|
| 35 | //$endDate = $xpath->query('//div[contains(text()[2], "event date")]/following-sibling::h2'); |
|
| 36 | $startDate = $xpath->query('//div[contains(text(), "event date")]'); |
|
| 37 | } |
|
| 38 | if (! $startDate || $startDate->length == 0) { |
|
| 39 | throw new \InvalidArgumentException('The Event does not seem to have an end date'); |
|
| 40 | } |
|
| 41 | ||
| 42 | $endDate = $xpath->query('h2', $startDate->item(0)->parentNode); |
|
| 43 | ||
| 44 | if (! $startDate || $startDate->length == 0) { |
|
| 45 | throw new \InvalidArgumentException('The Event does not seem to have an end date'); |
|
| 46 | } |
|
| 47 | ||
| 48 | $endDate = $endDate->item(0)->textContent; |
|
| 49 | ||
| 50 | return new DateTimeImmutable($endDate, $this->timezone); |
|
| 51 | } |
|
| 52 | } |
|
| 53 | ||
| @@ 17-51 (lines=35) @@ | ||
| 14 | use DOMDocument; |
|
| 15 | use DOMXPath; |
|
| 16 | ||
| 17 | class EventStartDate |
|
| 18 | { |
|
| 19 | protected $timezone; |
|
| 20 | ||
| 21 | public function __construct(DateTimeZone $timezone) |
|
| 22 | { |
|
| 23 | $this->timezone = $timezone; |
|
| 24 | } |
|
| 25 | ||
| 26 | public function parse(DOMDocument $dom, DOMXPath $xpath) : DateTimeImmutable |
|
| 27 | { |
|
| 28 | // This expression does not work. It looks like the reason is the array-notation... |
|
| 29 | //$startDate = $xpath->query('//div[contains(text()[2], "event starts")]/following-sibling::h2'); |
|
| 30 | $startDate = $xpath->query('//div[contains(., "event starts")]'); |
|
| 31 | ||
| 32 | if (! $startDate || $startDate->length == 0) { |
|
| 33 | // This expression does not work. It looks like the reason is the array-notation... |
|
| 34 | //$startDate = $xpath->query('//div[contains(text()[2], "event date")]/following-sibling::h2'); |
|
| 35 | $startDate = $xpath->query('//div[contains(., "event date")]'); |
|
| 36 | } |
|
| 37 | if (! $startDate || $startDate->length == 0) { |
|
| 38 | throw new \InvalidArgumentException('The Event does not seem to have a start date-identifier'); |
|
| 39 | } |
|
| 40 | ||
| 41 | $startDate = $xpath->query('h2', $startDate->item($startDate->length-1)->parentNode); |
|
| 42 | ||
| 43 | if (! $startDate || $startDate->length == 0) { |
|
| 44 | throw new \InvalidArgumentException('The Event does not seem to have a start date'); |
|
| 45 | } |
|
| 46 | ||
| 47 | $startDate = $startDate->item(0)->textContent; |
|
| 48 | ||
| 49 | return new DateTimeImmutable($startDate, $this->timezone); |
|
| 50 | } |
|
| 51 | } |
|
| 52 | ||