diff --git a/src/Prismic/ApiData.php b/src/Prismic/ApiData.php index 32fcbfd6..fa836fa3 100644 --- a/src/Prismic/ApiData.php +++ b/src/Prismic/ApiData.php @@ -56,22 +56,30 @@ class ApiData */ private $experiments; + /** + * List of configured languages + * @var array + */ + private $languages; + /** * A constructor to build the object when you've retrieved all the data you need. * - * @param array $refs - * @param array $bookmarks - * @param array $types - * @param array $tags - * @param array $forms + * @param array $refs + * @param array $bookmarks + * @param array $types + * @param array $languages + * @param array $tags + * @param array $forms * @param Experiments $experiments - * @param string $oauth_initiate - * @param string $oauth_token + * @param string $oauth_initiate + * @param string $oauth_token */ private function __construct( array $refs, array $bookmarks, array $types, + array $languages, array $tags, array $forms, Experiments $experiments, @@ -81,6 +89,7 @@ private function __construct( $this->refs = $refs; $this->bookmarks = $bookmarks; $this->types = $types; + $this->languages = $languages; $this->tags = $tags; $this->forms = $forms; $this->experiments = $experiments; @@ -124,6 +133,12 @@ function ($ref) { ), (array)$json->bookmarks, (array)$json->types, + array_map( + function($language) { + return Language::parse($language); + }, + (array)$json->languages + ), $json->tags, (array)$json->forms, $experiments, @@ -196,4 +211,9 @@ public function getOauthToken() : string { return $this->oauth_token; } + + public function getLanguages() : array + { + return $this->languages; + } } diff --git a/src/Prismic/Language.php b/src/Prismic/Language.php new file mode 100644 index 00000000..b7c95f01 --- /dev/null +++ b/src/Prismic/Language.php @@ -0,0 +1,81 @@ +id = $id; + $this->name = $name; + } + + + /** + * + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * + * @param \stdClass $json + * @return Language + * + * @throws Exception\InvalidArgumentException + */ + public static function parse(\stdClass $json): self + { + if (! isset($json->id, $json->name)) { + throw new Exception\InvalidArgumentException( + 'The properties id, name should exist in the JSON object for a Language' + ); + } + return new self( + $json->id, + $json->name + ); + } + +} \ No newline at end of file diff --git a/tests/Prismic/ApiDataTest.php b/tests/Prismic/ApiDataTest.php index 319e77fe..d37dcb0d 100644 --- a/tests/Prismic/ApiDataTest.php +++ b/tests/Prismic/ApiDataTest.php @@ -4,6 +4,7 @@ namespace Prismic\Test; use Prismic\ApiData; +use Prismic\Language; use Prismic\Ref; use Prismic\Experiments; use stdClass; @@ -11,6 +12,7 @@ class ApiDataTest extends TestCase { + /** @var ApiData */ private $data; public function setUp() @@ -52,6 +54,9 @@ public function testApiDataHasExpectedValues() $this->assertCount(2, $this->data->getForms()); $this->assertContainsOnlyInstancesOf(stdClass::class, $this->data->getForms()); + $this->assertCount(4, $this->data->getLanguages()); + $this->assertContainsOnly(Language::class, $this->data->getLanguages()); + $this->assertInstanceOf(Experiments::class, $this->data->getExperiments()); $this->assertSame('http://lesbonneschoses.prismic.io/auth', $this->data->getOauthInitiate()); diff --git a/tests/Prismic/LanguageTest.php b/tests/Prismic/LanguageTest.php new file mode 100644 index 00000000..d24304a7 --- /dev/null +++ b/tests/Prismic/LanguageTest.php @@ -0,0 +1,43 @@ +json = $json = \json_decode($this->getJsonFixture('language.json')); + $this->language = Language::parse($json); + } + + public function testCorrectId() + { + $this->assertSame($this->json->id, $this->language->getId()); + $this->assertSame($this->json->name, $this->language->getName()); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testWrongObjectType() + { + Language::parse(new \stdClass); + } + +} \ No newline at end of file diff --git a/tests/fixtures/data.json b/tests/fixtures/data.json index 900f3003..44ccb7cd 100644 --- a/tests/fixtures/data.json +++ b/tests/fixtures/data.json @@ -30,6 +30,24 @@ "job-offer": "Job offer", "product": "Product" }, + "languages": [ + { + "id": "en-us", + "name": "English - United States" + }, + { + "id": "nl-nl", + "name": "Dutch - Netherlands" + }, + { + "id": "es-es", + "name": "Spanish - Spain (Traditional)" + }, + { + "id": "fr-fr", + "name": "French - France" + } + ], "tags": ["Cupcake", "Pie", "Featured", "Macaron"], "forms": { "everything": { diff --git a/tests/fixtures/language.json b/tests/fixtures/language.json new file mode 100644 index 00000000..10bd52f7 --- /dev/null +++ b/tests/fixtures/language.json @@ -0,0 +1,4 @@ +{ + "id": "en-us", + "name": "English - United States" +} \ No newline at end of file