diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Entities/CategoryEntity.php | 110 | ||||
-rw-r--r-- | src/Entities/ChannelEntity.php | 189 | ||||
-rw-r--r-- | src/Entities/CountryEntity.php | 122 | ||||
-rw-r--r-- | src/Entities/GenreEntity.php | 104 | ||||
-rw-r--r-- | src/Entities/Program/CreditEntity.php | 89 | ||||
-rw-r--r-- | src/Entities/ProgramEntity.php | 223 | ||||
-rw-r--r-- | src/Environments/ServiceEnvironment.php | 60 | ||||
-rw-r--r-- | src/Parsers/BaseParser.php | 34 | ||||
-rw-r--r-- | src/Parsers/IntegerParser.php | 36 | ||||
-rw-r--r-- | src/Parsers/StringParser.php | 36 | ||||
-rw-r--r-- | src/Presenter/ServicePresenter.php | 101 | ||||
-rw-r--r-- | src/Repositories/BaseRepository.php | 32 | ||||
-rw-r--r-- | src/Repositories/CategoryRepository.php | 104 | ||||
-rw-r--r-- | src/Repositories/ChannelRepository.php | 108 | ||||
-rw-r--r-- | src/Repositories/CountryRepository.php | 92 | ||||
-rw-r--r-- | src/Repositories/GenreRepository.php | 103 | ||||
-rw-r--r-- | src/Repositories/ProgramRepository.php | 190 |
17 files changed, 1733 insertions, 0 deletions
diff --git a/src/Entities/CategoryEntity.php b/src/Entities/CategoryEntity.php new file mode 100644 index 0000000..2d113a4 --- /dev/null +++ b/src/Entities/CategoryEntity.php @@ -0,0 +1,110 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Entities; + +use RuntimeException; +use function is_string; + +/** + * @property-read string $id + * @property-read string $lang + * @property-read string $name + * @property-read string $version + * + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Entities + */ +final class CategoryEntity { + /** + * @var string + */ + private string $id; + + /** + * @var string + */ + private string $lang; + + /** + * @var string + */ + private string $name; + + /** + * @var string + */ + private string $version; + + /** + * @param array $payload + */ + private function __construct(array $payload) { + $this->id = $payload['id']; + $this->lang = $payload['lang']; + $this->name = $payload['name']; + $this->version = $payload['version']; + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\CategoryEntity + * @throws \RuntimeException + */ + public static function create(array $payload): CategoryEntity { + if (!is_string($payload['id'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['lang'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['name'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['version'])) { + throw new RuntimeException('blah-blah-blah'); + } + + return new CategoryEntity($payload); + } + + /** + * @param string $name + * + * @return mixed + */ + public function __get(string $name) { + return $this->$name; + } + + /** + * @param string $name + * @param mixed $value + * + * @throws \RuntimeException + */ + public function __set(string $name, $value) { + throw new RuntimeException('blah-blah-blah'); + } +} diff --git a/src/Entities/ChannelEntity.php b/src/Entities/ChannelEntity.php new file mode 100644 index 0000000..4881722 --- /dev/null +++ b/src/Entities/ChannelEntity.php @@ -0,0 +1,189 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Entities; + +use DateTime; +use RuntimeException; +use function is_string; + +/** + * @property-read string $base_id + * @property-read string $base_name + * @property-read string $epg_id + * @property-read string $geo_data + * @property-read string $href + * @property-read string $icon + * @property-read string $id + * @property-read string $lang + * @property-read string $name + * @property-read \DateTime $update_at + * @property-read string $week + * + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Entities + */ +final class ChannelEntity { + /** + * @var string + */ + private string $base_id; + + /** + * @var string + */ + private string $base_name; + + /** + * @var string + */ + private string $epg_id; + + /** + * @var string + */ + private string $geo_data; + + /** + * @var string + */ + private string $href; + + /** + * @var string + */ + private string $icon; + + /** + * @var string + */ + private string $id; + + /** + * @var string + */ + private string $lang; + + /** + * @var string + */ + private string $name; + + /** + * @var \DateTime + */ + private DateTime $update_at; + + /** + * @var string + */ + private string $week; + + /** + * @param array $payload + */ + private function __construct(array $payload) { + $this->base_id = $payload['base_id']; + $this->base_name = $payload['base_name']; + $this->epg_id = $payload['epg_id']; + $this->geo_data = $payload['geo_data']; + $this->href = $payload['href']; + $this->icon = $payload['icon']; + $this->id = $payload['id']; + $this->lang = $payload['lang']; + $this->name = $payload['name']; + $this->update_at = $payload['update_at']; + $this->week = $payload['week']; + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\ChannelEntity + * + * @throws \RuntimeException + */ + public static function create(array $payload): ChannelEntity { + if (!is_string($payload['base_id'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['base_name'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['epg_id'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['geo_data'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['href'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['icon'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['id'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['lang'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['name'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!$payload['update_at'] instanceof DateTime) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['week'])) { + throw new RuntimeException('blah-blah-blah'); + } + + return new ChannelEntity($payload); + } + + /** + * @param string $name + * + * @return mixed + */ + public function __get(string $name) { + return $this->$name; + } + + /** + * @param string $name + * @param mixed $value + * + * @throws \RuntimeException + */ + public function __set(string $name, $value) { + throw new RuntimeException('blah-blah-blah'); + } +} diff --git a/src/Entities/CountryEntity.php b/src/Entities/CountryEntity.php new file mode 100644 index 0000000..bf8b6f7 --- /dev/null +++ b/src/Entities/CountryEntity.php @@ -0,0 +1,122 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Entities; + +use RuntimeException; +use function is_string; + +/** + * @property-read string $id + * @property-read string $iso + * @property-read string $lang + * @property-read string $name + * @property-read string $version + * + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Entities + */ +final class CountryEntity { + /** + * @var string + */ + private string $id; + + /** + * @var string + */ + private string $iso; + + /** + * @var string + */ + private string $lang; + + /** + * @var string + */ + private string $name; + + /** + * @var string + */ + private string $version; + + /** + * @param array $payload + */ + private function __construct(array $payload) { + $this->id = $payload['id']; + $this->iso = $payload['iso']; + $this->lang = $payload['lang']; + $this->name = $payload['name']; + $this->version = $payload['version']; + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\CountryEntity + * + * @throws \RuntimeException + */ + public static function create(array $payload): CountryEntity { + if (!is_string($payload['id'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['iso'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['lang'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['name'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['version'])) { + throw new RuntimeException('blah-blah-blah'); + } + + return new CountryEntity($payload); + } + + /** + * @param string $name + * + * @return mixed + */ + public function __get(string $name) { + return $this->$name; + } + + /** + * @param string $name + * @param mixed $value + * + * @throws \RuntimeException + */ + public function __set(string $name, $value) { + throw new RuntimeException('blah-blah-blah'); + } +} diff --git a/src/Entities/GenreEntity.php b/src/Entities/GenreEntity.php new file mode 100644 index 0000000..ee81041 --- /dev/null +++ b/src/Entities/GenreEntity.php @@ -0,0 +1,104 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Entities; + +use RuntimeException; +use function is_string; + +/** + * @property-read string $id + * @property-read string $lang + * @property-read string $name + * @property-read string $version + * + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Entities + */ +final class GenreEntity { + /** + * @var string + */ + private string $id; + + /** + * @var string + */ + private string $lang; + + /** + * @var string + */ + private string $name; + + /** + * @var string + */ + private string $version; + + /** + * @param array $payload + */ + private function __construct(array $payload) { + $this->id = $payload['id']; + $this->lang = $payload['lang']; + $this->name = $payload['name']; + $this->version = $payload['version']; + } + + public static function create(array $payload): GenreEntity { + if (!is_string($payload['id'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['lang'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['name'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['version'])) { + throw new RuntimeException('blah-blah-blah'); + } + + return new GenreEntity($payload); + } + + /** + * @param string $name + * + * @return mixed + */ + public function __get(string $name) { + return $this->$name; + } + + /** + * @param string $name + * @param mixed $value + * + * @throws \RuntimeException + */ + public function __set(string $name, $value) { + throw new RuntimeException('blah-blah-blah'); + } +} diff --git a/src/Entities/Program/CreditEntity.php b/src/Entities/Program/CreditEntity.php new file mode 100644 index 0000000..f7088ea --- /dev/null +++ b/src/Entities/Program/CreditEntity.php @@ -0,0 +1,89 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Entities\Program; + +use RuntimeException; +use function is_string; + +/** + * @property-read string $name + * @property-read string $type + * + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Entities\Program + */ +final class CreditEntity { + /** + * @var string + */ + private string $name; + + /** + * @var string + */ + private string $type; + + /** + * @param array $payload + */ + private function __construct(array $payload) { + $this->name = $payload['name']; + $this->type = $payload['type']; + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\Program\CreditEntity + * + * @throws \RuntimeException + */ + public static function create(array $payload): CreditEntity { + if (!is_string($payload['name'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['type'])) { + throw new RuntimeException('blah-blah-blah'); + } + + return new CreditEntity($payload); + } + + /** + * @param string $name + * + * @return mixed + */ + public function __get(string $name) { + return $this->$name; + } + + /** + * @param string $name + * @param mixed $value + * + * @throws \RuntimeException + */ + public function __set(string $name, $value) { + throw new RuntimeException('blah-blah-blah'); + } +} diff --git a/src/Entities/ProgramEntity.php b/src/Entities/ProgramEntity.php new file mode 100644 index 0000000..04017c6 --- /dev/null +++ b/src/Entities/ProgramEntity.php @@ -0,0 +1,223 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Entities; + +use DateTime; +use RuntimeException; +use function is_array; +use function is_string; + +/** + * @property-read \EPGService\Entities\Program\CreditEntity[]|null $credits + * @property-read \DateTime $date + * @property-read \DateTime $start + * @property-read \DateTime $stop + * @property-read string $channel_id + * @property-read string|null $description + * @property-read string|null $parents_guide + * @property-read string|null $sub_title + * @property-read string $title + * @property-read string|null $year + * @property-read string[]|null $categories + * @property-read string[]|null $countries + * @property-read string[]|null $icons + * @property-read string[]|null $productions + * + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Entities + */ +final class ProgramEntity { + /** + * @var string[]|null + */ + private ?array $categories; + + /** + * @var string + */ + private string $channel_id; + + /** + * @var string[]|null + */ + private ?array $countries; + + /** + * @var \EPGService\Entities\Program\CreditEntity[]|null + */ + private ?array $credits; + + /** + * @var \DateTime + */ + private DateTime $date; + + /** + * @var string|null + */ + private ?string $description; + + /** + * @var string[]|null + */ + private ?array $icons; + + /** + * @var string|null + */ + private ?string $parents_guide; + + /** + * @var string[]|null + */ + private ?array $productions; + + /** + * @var \DateTime + */ + private DateTime $start; + + /** + * @var \DateTime + */ + private DateTime $stop; + + /** + * @var string|null + */ + private ?string $sub_title; + + /** + * @var string + */ + private string $title; + + /** + * @var string|null + */ + private ?string $year; + + /** + * @param array $payload + */ + private function __construct(array $payload) { + $this->categories = $payload['categories'] ?? null; + $this->channel_id = $payload['channel_id']; + $this->countries = $payload['countries'] ?? null; + $this->credits = $payload['credits'] ?? null; + $this->date = $payload['date']; + $this->description = $payload['description'] ?? null; + $this->icons = $payload['icons'] ?? null; + $this->parents_guide = $payload['parents_guide'] ?? null; + $this->productions = $payload['productions'] ?? null; + $this->start = $payload['start']; + $this->stop = $payload['stop']; + $this->sub_title = $payload['sub_title'] ?? null; + $this->title = $payload['title']; + $this->year = $payload['year'] ?? null; + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\ProgramEntity + * + * @throws \RuntimeException + */ + public static function create(array $payload): ProgramEntity { + if (isset($payload['categories']) && !is_array($payload['categories'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['channel_id'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['countries']) && !is_array($payload['countries'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['credits']) && !is_array($payload['credits'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!$payload['date'] instanceof DateTime) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['description']) && !is_string($payload['description'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['icons']) && !is_array($payload['icons'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['parents_guide']) && !is_string($payload['parents_guide'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['productions']) && !is_array($payload['productions'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!$payload['start'] instanceof DateTime) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!$payload['stop'] instanceof DateTime) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['sub_title']) && !is_string($payload['sub_title'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (!is_string($payload['title'])) { + throw new RuntimeException('blah-blah-blah'); + } + + if (isset($payload['year']) && !is_string($payload['year'])) { + throw new RuntimeException('blah-blah-blah'); + } + + return new ProgramEntity($payload); + } + + /** + * @param string $name + * + * @return mixed + */ + public function __get(string $name) { + return $this->$name; + } + + /** + * @param string $name + * @param mixed $value + * + * @throws \RuntimeException + */ + public function __set(string $name, $value) { + throw new RuntimeException('blah-blah-blah'); + } +} diff --git a/src/Environments/ServiceEnvironment.php b/src/Environments/ServiceEnvironment.php new file mode 100644 index 0000000..53ab732 --- /dev/null +++ b/src/Environments/ServiceEnvironment.php @@ -0,0 +1,60 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Environments; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Environments + */ +class ServiceEnvironment { + /** + * @var string + */ + protected const BASE_URL = 'http://xmldata.epgservice.ru/EPGService/hs/xmldata/%s/'; + + /** + * @var string + */ + private string $key; + + /** + * @param string $key + */ + protected function __construct(string $key) { + $this->key = $key; + } + + /** + * @param string $key + * + * @return \EPGService\Environments\ServiceEnvironment + */ + public static function create(string $key): ServiceEnvironment { + return new ServiceEnvironment($key); + } + + /** + * @return string + */ + public function getUrl(): string { + return sprintf(self::BASE_URL, $this->key); + } +} diff --git a/src/Parsers/BaseParser.php b/src/Parsers/BaseParser.php new file mode 100644 index 0000000..d36508d --- /dev/null +++ b/src/Parsers/BaseParser.php @@ -0,0 +1,34 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Parsers; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Parsers + */ +interface BaseParser { + /** + * @param mixed $value + * + * @return mixed + */ + public static function get($value); +} diff --git a/src/Parsers/IntegerParser.php b/src/Parsers/IntegerParser.php new file mode 100644 index 0000000..ba89315 --- /dev/null +++ b/src/Parsers/IntegerParser.php @@ -0,0 +1,36 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Parsers; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Parsers + */ +final class IntegerParser implements BaseParser { + /** + * @param mixed $value + * + * @return int + */ + public static function get($value): int { + return filter_var($value, FILTER_VALIDATE_INT); + } +} diff --git a/src/Parsers/StringParser.php b/src/Parsers/StringParser.php new file mode 100644 index 0000000..8473106 --- /dev/null +++ b/src/Parsers/StringParser.php @@ -0,0 +1,36 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Parsers; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Parsers + */ +final class StringParser implements BaseParser { + /** + * @param $value + * + * @return string + */ + public static function get($value): string { + return filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); + } +} diff --git a/src/Presenter/ServicePresenter.php b/src/Presenter/ServicePresenter.php new file mode 100644 index 0000000..e3117ed --- /dev/null +++ b/src/Presenter/ServicePresenter.php @@ -0,0 +1,101 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Presenter; + +use EPGService\Environments\ServiceEnvironment; +use EPGService\Repositories\CategoryRepository; +use EPGService\Repositories\ChannelRepository; +use EPGService\Repositories\GenreRepository; +use EPGService\Repositories\ProgramRepository; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Presenter + */ +final class ServicePresenter { + /** + * @var \EPGService\Environments\ServiceEnvironment + */ + private ServiceEnvironment$environment; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + */ + private function __construct(ServiceEnvironment $environment) { + $this->environment = $environment; + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * + * @return \EPGService\Presenter\ServicePresenter + */ + public static function create(ServiceEnvironment $environment): ServicePresenter { + return new ServicePresenter($environment); + } + + /** + * @return \EPGService\Entities\CategoryEntity[] + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + * @throws \Exception + */ + public function getCategories(): array { + return CategoryRepository::create($this->environment)->get(); + } + + /** + * @return \EPGService\Entities\ChannelEntity[] + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + * @throws \Exception + */ + public function getChannels(): array { + return ChannelRepository::create($this->environment)->get(); + } + + /** + * @return \EPGService\Entities\GenreEntity[] + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + * @throws \Exception + */ + public function getGenres(): array { + return GenreRepository::create($this->environment)->get(); + } + + /** + * @param string $channel_id + * @param string $week + * + * @return \EPGService\Entities\ProgramEntity[] + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + * @throws \Exception + */ + public function getPrograms(string $channel_id, string $week): array { + return ProgramRepository::create($this->environment, $channel_id, $week)->get(); + } +} diff --git a/src/Repositories/BaseRepository.php b/src/Repositories/BaseRepository.php new file mode 100644 index 0000000..14c9061 --- /dev/null +++ b/src/Repositories/BaseRepository.php @@ -0,0 +1,32 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Repositories; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +interface BaseRepository { + /** + * @return mixed + */ + public function get(); +} diff --git a/src/Repositories/CategoryRepository.php b/src/Repositories/CategoryRepository.php new file mode 100644 index 0000000..6139168 --- /dev/null +++ b/src/Repositories/CategoryRepository.php @@ -0,0 +1,104 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Repositories; + +use EPGService\Entities\CategoryEntity; +use EPGService\Environments\ServiceEnvironment; +use EPGService\Parsers\IntegerParser; +use EPGService\Parsers\StringParser; +use GuzzleHttp\Client; +use RuntimeException; +use SimpleXMLElement; +use function is_bool; +use function simplexml_load_string; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class CategoryRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'category_list'; + + /** + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + */ + private function __construct(ServiceEnvironment $environment) { + $this->client = new Client([ + 'base_uri' => $environment->getUrl(), + ]); + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * + * @return \EPGService\Repositories\CategoryRepository + */ + public static function create(ServiceEnvironment $environment): CategoryRepository { + return new CategoryRepository($environment); + } + + /** + * @return array + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + */ + public function get(): array { + $response = $this->client->get(self::METHOD); + $content = $response->getBody()->getContents(); + $xml = simplexml_load_string($content); + + if (is_bool($xml) || !$xml->element instanceof SimpleXMLElement) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($xml->element as $element) { + if (!$element instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + if (!$element->name instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + $result[] = CategoryEntity::create([ + 'id' => IntegerParser::get($element['id']), + 'lang' => StringParser::get($element->name['lang']), + 'name' => StringParser::get($element->name), + 'version' => StringParser::get($element['version']), + ]); + } + + return $result; + } +} diff --git a/src/Repositories/ChannelRepository.php b/src/Repositories/ChannelRepository.php new file mode 100644 index 0000000..772e4f9 --- /dev/null +++ b/src/Repositories/ChannelRepository.php @@ -0,0 +1,108 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Repositories; + +use DateTime; +use EPGService\Entities\ChannelEntity; +use EPGService\Environments\ServiceEnvironment; +use EPGService\Parsers\StringParser; +use GuzzleHttp\Client; +use RuntimeException; +use SimpleXMLElement; +use function is_bool; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class ChannelRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'index'; + + /** + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + */ + private function __construct(ServiceEnvironment $environment) { + $this->client = new Client([ + 'base_uri' => $environment->getUrl(), + ]); + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * + * @return \EPGService\Repositories\ChannelRepository + */ + public static function create(ServiceEnvironment $environment): ChannelRepository { + return new ChannelRepository($environment); + } + + /** + * @return \EPGService\Entities\ChannelEntity[] + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + * @throws \Exception + */ + public function get(): array { + $response = $this->client->get(self::METHOD); + $content = $response->getBody()->getContents(); + $xml = simplexml_load_string($content); + + if (is_bool($xml) || !$xml->channel instanceof SimpleXMLElement) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($xml->channel as $element) { + if (!$element instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + $update = StringParser::get($element->update); + + $result[] = ChannelEntity::create([ + 'base_id' => StringParser::get($element->{'base-channel'}), + 'base_name' => StringParser::get($element->{'base-channel'}['id']), + 'epg_id' => StringParser::get($element['epgsrvc_id']), + 'geo_data' => StringParser::get($element->{'geo-data'}), + 'href' => StringParser::get($element->href), + 'icon' => StringParser::get($element->icon['src']), + 'id' => StringParser::get($element['id']), + 'lang' => StringParser::get($element->{'display-name'}['lang']), + 'name' => StringParser::get($element->{'base-channel'}), + 'update_at' => new DateTime($update), + 'week' => StringParser::get($element->week), + ]); + } + + return $result; + } +} diff --git a/src/Repositories/CountryRepository.php b/src/Repositories/CountryRepository.php new file mode 100644 index 0000000..cb7e3ae --- /dev/null +++ b/src/Repositories/CountryRepository.php @@ -0,0 +1,92 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Repositories; + +use EPGService\Entities\CountryEntity; +use EPGService\Environments\ServiceEnvironment; +use EPGService\Parsers\StringParser; +use GuzzleHttp\Client; +use RuntimeException; +use SimpleXMLElement; +use function is_bool; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class CountryRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'country_list'; + + /** + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + */ + private function __construct(ServiceEnvironment $environment) { + $this->client = new Client([ + 'base_uri' => $environment->getUrl(), + ]); + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * + * @return \EPGService\Repositories\CountryRepository + */ + public static function create(ServiceEnvironment $environment): CountryRepository { + return new CountryRepository($environment); + } + + public function get(): array { + $response = $this->client->get(self::METHOD); + $content = $response->getBody()->getContents(); + $xml = simplexml_load_string($content); + + if (is_bool($xml) || !$xml->element instanceof SimpleXMLElement) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($xml->element as $element) { + if (!$element instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + $result[] = CountryEntity::create([ + 'id' => StringParser::get($element['id']), + 'iso' => StringParser::get($element['ISO']), + 'lang' => StringParser::get($element->name['lang']), + 'name' => StringParser::get($element->name), + 'version' => StringParser::get($element['version']), + ]); + } + + return $result; + } +} diff --git a/src/Repositories/GenreRepository.php b/src/Repositories/GenreRepository.php new file mode 100644 index 0000000..67c9a4e --- /dev/null +++ b/src/Repositories/GenreRepository.php @@ -0,0 +1,103 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Repositories; + +use EPGService\Entities\GenreEntity; +use EPGService\Environments\ServiceEnvironment; +use EPGService\Parsers\IntegerParser; +use EPGService\Parsers\StringParser; +use GuzzleHttp\Client; +use RuntimeException; +use SimpleXMLElement; +use function is_bool; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class GenreRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'genre_list'; + + /** + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + */ + private function __construct(ServiceEnvironment $environment) { + $this->client = new Client([ + 'base_uri' => $environment->getUrl(), + ]); + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $env + * + * @return \EPGService\Repositories\GenreRepository + */ + public static function create(ServiceEnvironment $env): GenreRepository { + return new GenreRepository($env); + } + + /** + * @return \EPGService\Entities\GenreEntity[] + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + */ + public function get(): array { + $response = $this->client->get(self::METHOD); + $content = $response->getBody()->getContents(); + $xml = simplexml_load_string($content); + + if (is_bool($xml) || !$xml->element instanceof SimpleXMLElement) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($xml->element as $element) { + if (!$element instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + if (!$element->name instanceof SimpleXMLElement) { + trigger_error('blah-blah-blah', E_USER_WARNING); + continue; + } + + $result[] = GenreEntity::create([ + 'id' => IntegerParser::get($element['id']), + 'lang' => StringParser::get($element->name['lang']), + 'name' => StringParser::get($element->name), + 'version' => StringParser::get($element['version']), + ]); + } + + return $result; + } +} diff --git a/src/Repositories/ProgramRepository.php b/src/Repositories/ProgramRepository.php new file mode 100644 index 0000000..56884da --- /dev/null +++ b/src/Repositories/ProgramRepository.php @@ -0,0 +1,190 @@ +<?php +/** + * Copyright 2020 “EPGService” + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types = 1); + +namespace EPGService\Repositories; + +use DateTime; +use EPGService\Entities\Program\CreditEntity; +use EPGService\Entities\ProgramEntity; +use EPGService\Environments\ServiceEnvironment; +use GuzzleHttp\Client; +use GuzzleHttp\RequestOptions; +use RuntimeException; +use function is_array; + +/** + * @copyright Copyright © 2020 “Valentin Popov” <info@valentineus.link> + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @package EPGService\Repositories + */ +final class ProgramRepository implements BaseRepository { + /** + * @var string + */ + private const METHOD = 'file/%s'; + + /** + * @var string + */ + private string $channel_id; + + /** + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * @var string + */ + private string $week; + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * @param string $channel_id + * @param string $week + */ + private function __construct(ServiceEnvironment $environment, string $channel_id, string $week) { + $this->channel_id = $channel_id; + $this->week = $week; + + $this->client = new Client([ + 'base_uri' => $environment->getUrl(), + ]); + } + + /** + * @param \EPGService\Environments\ServiceEnvironment $environment + * @param string $channel_id + * @param string $week + * + * @return \EPGService\Repositories\ProgramRepository + */ + public static function create(ServiceEnvironment $environment, string $channel_id, string $week): ProgramRepository { + return new ProgramRepository($environment, $channel_id, $week); + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\Program\CreditEntity[] + * + * @throws \RuntimeException + */ + private static function getCreditEntities(array $payload): array { + return array_map(static function (array $value): CreditEntity { + return CreditEntity::create($value); + }, $payload); + } + + /** + * @param array $payload + * + * @return \EPGService\Entities\ProgramEntity + * + * @throws \RuntimeException + * @throws \Exception + */ + private static function getProgramEntity(array $payload): ProgramEntity { + $data = []; + + foreach ($payload as $name => $value) { + switch ($name) { + case 'category': + $data['categories'] = $value; + break; + case 'channel': + $data['channel_id'] = $value; + break; + case 'country': + $data['countries'] = $value; + break; + case 'credits': + $data['credits'] = self::getCreditEntities($value); + break; + case 'date': + $data['date'] = new DateTime($value); + break; + case 'desc': + $data['description'] = $value; + break; + case 'icon': + $data['icons'] = $value; + break; + case 'pg': + $data['parents_guide'] = $value; + break; + case 'production': + $data['productions'] = $value; + break; + case 'start': + $data['start'] = new DateTime($value); + break; + case 'stop': + $data['stop'] = new DateTime($value); + break; + case 'sub_title': + $data['sub_title'] = $value; + break; + case 'title': + $data['title'] = $value; + break; + case 'year': + $data['year'] = $value; + break; + default: + trigger_error('blah-blah-blah', E_USER_WARNING); + break; + } + } + + return ProgramEntity::create($data); + } + + /** + * @return array + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \RuntimeException + * @throws \Exception + */ + public function get(): array { + $uri = sprintf(self::METHOD, $this->channel_id); + + $response = $this->client->get($uri, [ + RequestOptions::QUERY => [ + 'week' => $this->week, + ], + ]); + + $content = $response->getBody()->getContents(); + $json = json_decode($content, true, 512, JSON_THROW_ON_ERROR); + + if (!is_array($json) || !is_array($json['programms'])) { + throw new RuntimeException('blah-blah-blah'); + } + + $result = []; + + foreach ($json['programms'] as $program) { + $result[] = self::getProgramEntity($program); + } + + return $result; + } +} |