* @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; } }