From 3563142af6d61c21b694850741a05337567829a9 Mon Sep 17 00:00:00 2001 From: Jeroen Boersma Date: Wed, 20 Mar 2019 21:34:19 +0100 Subject: [PATCH 1/2] Added Language reference + added Language + updated ApiData * updated tests --- src/Prismic/ApiData.php | 34 +++++++++++--- src/Prismic/Language.php | 81 ++++++++++++++++++++++++++++++++++ tests/Prismic/ApiDataTest.php | 5 +++ tests/Prismic/LanguageTest.php | 43 ++++++++++++++++++ tests/fixtures/data.json | 18 ++++++++ tests/fixtures/language.json | 4 ++ 6 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 src/Prismic/Language.php create mode 100644 tests/Prismic/LanguageTest.php create mode 100644 tests/fixtures/language.json 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 From 45fccc20f1de0d5f98758aa2accca043c90faf1c Mon Sep 17 00:00:00 2001 From: Jeroen Boersma Date: Wed, 20 Mar 2019 21:02:45 +0000 Subject: [PATCH 2/2] Fixed codestyles * minor differences in ide settings --- src/Prismic/ApiData.php | 2 +- src/Prismic/Language.php | 7 ++----- tests/Prismic/LanguageTest.php | 4 +--- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Prismic/ApiData.php b/src/Prismic/ApiData.php index fa836fa3..c14de72f 100644 --- a/src/Prismic/ApiData.php +++ b/src/Prismic/ApiData.php @@ -134,7 +134,7 @@ function ($ref) { (array)$json->bookmarks, (array)$json->types, array_map( - function($language) { + function ($language) { return Language::parse($language); }, (array)$json->languages diff --git a/src/Prismic/Language.php b/src/Prismic/Language.php index b7c95f01..91baf187 100644 --- a/src/Prismic/Language.php +++ b/src/Prismic/Language.php @@ -8,7 +8,6 @@ namespace Prismic; - class Language { @@ -33,8 +32,7 @@ class Language private function __construct( string $id, string $name - ) - { + ) { $this->id = $id; $this->name = $name; } @@ -77,5 +75,4 @@ public static function parse(\stdClass $json): self $json->name ); } - -} \ No newline at end of file +} diff --git a/tests/Prismic/LanguageTest.php b/tests/Prismic/LanguageTest.php index d24304a7..8da3c092 100644 --- a/tests/Prismic/LanguageTest.php +++ b/tests/Prismic/LanguageTest.php @@ -8,7 +8,6 @@ namespace Prismic\Test; - use Prismic\Exception\InvalidArgumentException; use Prismic\Language; @@ -39,5 +38,4 @@ public function testWrongObjectType() { Language::parse(new \stdClass); } - -} \ No newline at end of file +}