From e846aafceb77f0cd7048d48421947fd4ad941bef Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sun, 6 Sep 2020 15:32:05 +0300 Subject: [PATCH] Updated locale response models --- app/controllers/api/locale.php | 68 ++++++++++---- app/views/console/comps/header.phtml | 2 + src/Appwrite/Utopia/Response.php | 32 +++++-- .../Utopia/Response/Model/Continent.php | 45 +++++++++ .../Utopia/Response/Model/Country.php | 45 +++++++++ .../Utopia/Response/Model/Currency.php | 76 ++++++++++++++++ .../Utopia/Response/Model/Language.php | 50 ++++++++++ src/Appwrite/Utopia/Response/Model/Locale.php | 1 - src/Appwrite/Utopia/Response/Model/Phone.php | 50 ++++++++++ tests/e2e/Services/Locale/LocaleBase.php | 91 ++++++++++--------- 10 files changed, 392 insertions(+), 68 deletions(-) create mode 100644 src/Appwrite/Utopia/Response/Model/Continent.php create mode 100644 src/Appwrite/Utopia/Response/Model/Country.php create mode 100644 src/Appwrite/Utopia/Response/Model/Currency.php create mode 100644 src/Appwrite/Utopia/Response/Model/Language.php create mode 100644 src/Appwrite/Utopia/Response/Model/Phone.php diff --git a/app/controllers/api/locale.php b/app/controllers/api/locale.php index e2d4d3c3b1..a33eea5d64 100644 --- a/app/controllers/api/locale.php +++ b/app/controllers/api/locale.php @@ -76,10 +76,18 @@ App::get('/v1/locale/countries') /** @var Utopia\Locale\Locale $locale */ $list = $locale->getText('countries'); /* @var $list array */ + $output = []; - \asort($list); // sort by abc per language + \asort($list); // sort by abc per locale - $response->json($list); + foreach ($list as $key => $value) { + $output[] = new Document([ + 'name' => $value, + 'code' => $key, + ]); + } + + $response->dynamic(new Document(['countries' => $output, 'sum' => \count($output)]), Response::MODEL_COUNTRY_LIST); }, ['response', 'locale']); App::get('/v1/locale/countries/eu') @@ -94,19 +102,22 @@ App::get('/v1/locale/countries/eu') /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Locale\Locale $locale */ - $countries = $locale->getText('countries'); /* @var $countries array */ + $list = $locale->getText('countries'); /* @var $countries array */ $eu = Config::getParam('locale-eu'); - $list = []; - - foreach ($eu as $code) { - if (\array_key_exists($code, $countries)) { - $list[$code] = $countries[$code]; - } - } + $output = []; \asort($list); - $response->json($list); + foreach ($eu as $code) { + if (\array_key_exists($code, $list)) { + $output[] = new Document([ + 'name' => $list[$code], + 'code' => $code, + ]); + } + } + + $response->dynamic(new Document(['countries' => $output, 'sum' => \count($output)]), Response::MODEL_COUNTRY_LIST); }, ['response', 'locale']); App::get('/v1/locale/countries/phones') @@ -122,18 +133,22 @@ App::get('/v1/locale/countries/phones') /** @var Utopia\Locale\Locale $locale */ $list = Config::getParam('locale-phones'); /* @var $list array */ - $countries = $locale->getText('countries'); /* @var $countries array */ + $output = []; + + \asort($list); foreach ($list as $code => $name) { if (\array_key_exists($code, $countries)) { - $list[$code] = '+'.$list[$code]; + $output[] = new Document([ + 'code' => '+'.$list[$code], + 'countryCode' => $code, + 'countryName' => $countries[$code], + ]); } } - \asort($list); - - $response->json($list); + $response->dynamic(new Document(['phones' => $output, 'sum' => \count($output)]), Response::MODEL_PHONE_LIST); }, ['response', 'locale']); App::get('/v1/locale/continents') @@ -151,8 +166,15 @@ App::get('/v1/locale/continents') $list = $locale->getText('continents'); /* @var $list array */ \asort($list); + + foreach ($list as $key => $value) { + $output[] = new Document([ + 'name' => $value, + 'code' => $key, + ]); + } - $response->json($list); + $response->dynamic(new Document(['continents' => $output, 'sum' => \count($output)]), Response::MODEL_CONTINENT_LIST); }, ['response', 'locale']); App::get('/v1/locale/currencies') @@ -168,7 +190,11 @@ App::get('/v1/locale/currencies') $list = Config::getParam('locale-currencies'); - $response->json($list); + $list = array_map(function($node) { + return new Document($node); + }, $list); + + $response->dynamic(new Document(['currencies' => $list, 'sum' => \count($list)]), Response::MODEL_CURRENCY_LIST); }, ['response']); @@ -185,5 +211,9 @@ App::get('/v1/locale/languages') $list = Config::getParam('locale-languages'); - $response->json($list); + $list = array_map(function($node) { + return new Document($node); + }, $list); + + $response->dynamic(new Document(['languages' => $list, 'sum' => \count($list)]), Response::MODEL_LANGUAGE_LIST); }, ['response']); \ No newline at end of file diff --git a/app/views/console/comps/header.phtml b/app/views/console/comps/header.phtml index 921a9c933e..c8f436fd50 100644 --- a/app/views/console/comps/header.phtml +++ b/app/views/console/comps/header.phtml @@ -183,6 +183,7 @@ data-name="projects" data-scope="console"> +
diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index b7d10cba39..c362df0251 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -8,6 +8,9 @@ use Swoole\Http\Response as SwooleHTTPResponse; use Appwrite\Database\Document; use Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response\Model\BaseList; +use Appwrite\Utopia\Response\Model\Continent; +use Appwrite\Utopia\Response\Model\Country; +use Appwrite\Utopia\Response\Model\Currency; use Appwrite\Utopia\Response\Model\Domain; use Appwrite\Utopia\Response\Model\Error; use Appwrite\Utopia\Response\Model\ErrorDev; @@ -15,12 +18,14 @@ use Appwrite\Utopia\Response\Model\Execution; use Appwrite\Utopia\Response\Model\File; use Appwrite\Utopia\Response\Model\Func; use Appwrite\Utopia\Response\Model\Key; +use Appwrite\Utopia\Response\Model\Language; use Appwrite\Utopia\Response\Model\User; use Appwrite\Utopia\Response\Model\Session; use Appwrite\Utopia\Response\Model\Team; use Appwrite\Utopia\Response\Model\Locale; use Appwrite\Utopia\Response\Model\Log; use Appwrite\Utopia\Response\Model\Membership; +use Appwrite\Utopia\Response\Model\Phone; use Appwrite\Utopia\Response\Model\Platform; use Appwrite\Utopia\Response\Model\Tag; use Appwrite\Utopia\Response\Model\Task; @@ -29,8 +34,8 @@ use Appwrite\Utopia\Response\Model\Webhook; class Response extends SwooleResponse { // General - const MODEL_LOG = 'log'; // - Missing - const MODEL_LOG_LIST = 'logList'; // - Missing + const MODEL_LOG = 'log'; + const MODEL_LOG_LIST = 'logList'; const MODEL_ERROR = 'error'; const MODEL_ERROR_DEV = 'errorDev'; const MODEL_BASE_LIST = 'baseList'; @@ -48,11 +53,16 @@ class Response extends SwooleResponse // Locale const MODEL_LOCALE = 'locale'; - const MODEL_COUNTRY = 'country'; // - Missing - const MODEL_CONTINENT = 'continent'; // - Missing + const MODEL_COUNTRY = 'country'; + const MODEL_COUNTRY_LIST = 'countryList'; + const MODEL_CONTINENT = 'continent'; + const MODEL_CONTINENT_LIST = 'continentList'; const MODEL_CURRENCY = 'currency'; // - Missing + const MODEL_CURRENCY_LIST = 'currencyList'; // - Missing const MODEL_LANGUAGE = 'langauge'; // - Missing + const MODEL_LANGUAGE_LIST = 'langaugeList'; // - Missing const MODEL_PHONE = 'phone'; // - Missing + const MODEL_PHONE_LIST = 'phoneList'; // - Missing // Storage const MODEL_FILE = 'file'; @@ -112,6 +122,11 @@ class Response extends SwooleResponse ->setModel(new BaseList('Tasks List', self::MODEL_TASK_LIST, 'tasks', self::MODEL_TASK)) ->setModel(new BaseList('Platforms List', self::MODEL_PLATFORM_LIST, 'platforms', self::MODEL_PLATFORM)) ->setModel(new BaseList('Domains List', self::MODEL_DOMAIN_LIST, 'domains', self::MODEL_DOMAIN)) + ->setModel(new BaseList('Countries List', self::MODEL_COUNTRY_LIST, 'countries', self::MODEL_COUNTRY)) + ->setModel(new BaseList('Continents List', self::MODEL_CONTINENT_LIST, 'continents', self::MODEL_CONTINENT)) + ->setModel(new BaseList('Languages List', self::MODEL_LANGUAGE_LIST, 'languages', self::MODEL_LANGUAGE)) + ->setModel(new BaseList('Currencies List', self::MODEL_CURRENCY_LIST, 'currencies', self::MODEL_CURRENCY)) + ->setModel(new BaseList('Phones List', self::MODEL_PHONE_LIST, 'phones', self::MODEL_PHONE)) // Entities ->setModel(new Log()) ->setModel(new User()) @@ -128,12 +143,15 @@ class Response extends SwooleResponse ->setModel(new Task()) ->setModel(new Domain()) ->setModel(new Platform()) - // Continent - // Country + ->setModel(new Country()) + ->setModel(new Continent()) + ->setModel(new Language()) + ->setModel(new Currency()) + ->setModel(new Phone()) // Currency + // Phone // Verification // Recovery - // Language ; parent::__construct($response); diff --git a/src/Appwrite/Utopia/Response/Model/Continent.php b/src/Appwrite/Utopia/Response/Model/Continent.php new file mode 100644 index 0000000000..a42b39e9e9 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Continent.php @@ -0,0 +1,45 @@ +addRule('name', [ + 'type' => 'string', + 'description' => 'Continent name.', + 'example' => 'Europe', + ]) + ->addRule('code', [ + 'type' => 'string', + 'description' => 'Continent two letter code.', + 'example' => 'EU', + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName():string + { + return 'Continent'; + } + + /** + * Get Collection + * + * @return string + */ + public function getType():string + { + return Response::MODEL_CONTINENT; + } +} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/Country.php b/src/Appwrite/Utopia/Response/Model/Country.php new file mode 100644 index 0000000000..b5ba0f74f5 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Country.php @@ -0,0 +1,45 @@ +addRule('name', [ + 'type' => 'string', + 'description' => 'Country name.', + 'example' => 'United States', + ]) + ->addRule('code', [ + 'type' => 'string', + 'description' => 'Country two-character ISO 3166-1 alpha code.', + 'example' => 'US', + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName():string + { + return 'Country'; + } + + /** + * Get Collection + * + * @return string + */ + public function getType():string + { + return Response::MODEL_COUNTRY; + } +} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/Currency.php b/src/Appwrite/Utopia/Response/Model/Currency.php new file mode 100644 index 0000000000..29b3d78f98 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Currency.php @@ -0,0 +1,76 @@ +addRule('symbol', [ + 'type' => 'string', + 'description' => 'Currency symbol.', + 'example' => '$', + ]) + ->addRule('name', [ + 'type' => 'string', + 'description' => 'Currency name.', + 'example' => 'US dollar', + ]) + ->addRule('symbolNative', [ + 'type' => 'string', + 'description' => 'Currency native symbol.', + 'example' => '$', + ]) + ->addRule('decimalDigits', [ + 'type' => 'integer', + 'description' => 'Number of decimal digits.', + 'example' => 2, + ]) + ->addRule('rounding', [ + 'type' => 'float', + 'description' => 'Currency digit rounding.', + 'example' => 0, + ]) + ->addRule('code', [ + 'type' => 'string', + 'description' => 'Currency code in [ISO 4217-1](http://en.wikipedia.org/wiki/ISO_4217) three-character format.', + 'example' => 'USD', + ]) + ->addRule('namePlural', [ + 'type' => 'string', + 'description' => 'Currency plural name', + 'example' => 'US dollars', + ]) + // ->addRule('locations', [ + // 'type' => 'string', + // 'description' => 'Currency locations list. List of location in two-character ISO 3166-1 alpha code.', + // 'example' => ['US'], + // 'array' => true, + // ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName():string + { + return 'Currency'; + } + + /** + * Get Collection + * + * @return string + */ + public function getType():string + { + return Response::MODEL_CURRENCY; + } +} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/Language.php b/src/Appwrite/Utopia/Response/Model/Language.php new file mode 100644 index 0000000000..966efa7e72 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Language.php @@ -0,0 +1,50 @@ +addRule('name', [ + 'type' => 'string', + 'description' => 'Language name.', + 'example' => 'Italian', + ]) + ->addRule('code', [ + 'type' => 'string', + 'description' => 'Language two-character ISO 639-1 codes.', + 'example' => 'it', + ]) + ->addRule('nativeName', [ + 'type' => 'string', + 'description' => 'Language native name.', + 'example' => 'Italiano', + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName():string + { + return 'Language'; + } + + /** + * Get Collection + * + * @return string + */ + public function getType():string + { + return Response::MODEL_LANGUAGE; + } +} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response/Model/Locale.php b/src/Appwrite/Utopia/Response/Model/Locale.php index b6482d7a95..1097a9457d 100644 --- a/src/Appwrite/Utopia/Response/Model/Locale.php +++ b/src/Appwrite/Utopia/Response/Model/Locale.php @@ -43,7 +43,6 @@ class Locale extends Model ]) ->addRule('currency', [ 'type' => 'string', - 'description' => 'ISO 4217 Email verification status.', 'description' => 'Currency code in [ISO 4217-1](http://en.wikipedia.org/wiki/ISO_4217) three-character format', 'example' => 'USD', ]) diff --git a/src/Appwrite/Utopia/Response/Model/Phone.php b/src/Appwrite/Utopia/Response/Model/Phone.php new file mode 100644 index 0000000000..e0242981e9 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Phone.php @@ -0,0 +1,50 @@ +addRule('code', [ + 'type' => 'string', + 'description' => 'Phone code.', + 'example' => '+1', + ]) + ->addRule('countryCode', [ + 'type' => 'string', + 'description' => 'Country two-character ISO 3166-1 alpha code.', + 'example' => 'US', + ]) + ->addRule('countryName', [ + 'type' => 'string', + 'description' => 'Country name.', + 'example' => 'United States', + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName():string + { + return 'Phone'; + } + + /** + * Get Collection + * + * @return string + */ + public function getType():string + { + return Response::MODEL_PHONE; + } +} \ No newline at end of file diff --git a/tests/e2e/Services/Locale/LocaleBase.php b/tests/e2e/Services/Locale/LocaleBase.php index 3f5d5e9486..299d65434d 100644 --- a/tests/e2e/Services/Locale/LocaleBase.php +++ b/tests/e2e/Services/Locale/LocaleBase.php @@ -44,8 +44,9 @@ trait LocaleBase $this->assertEquals($response['headers']['status-code'], 200); $this->assertIsArray($response['body']); - $this->assertCount(194, $response['body']); - $this->assertEquals($response['body']['US'], 'United States'); + $this->assertEquals(194, $response['body']['sum']); + $this->assertEquals($response['body']['countries'][0]['name'], 'Afghanistan'); + $this->assertEquals($response['body']['countries'][0]['code'], 'AF'); // Test locale code change to ES @@ -57,8 +58,9 @@ trait LocaleBase $this->assertEquals($response['headers']['status-code'], 200); $this->assertIsArray($response['body']); - $this->assertCount(194, $response['body']); - $this->assertEquals($response['body']['US'], 'Estados Unidos'); + $this->assertEquals(194, $response['body']['sum']); + $this->assertEquals($response['body']['countries'][0]['name'], 'Afganistán'); + $this->assertEquals($response['body']['countries'][0]['code'], 'AF'); /** * Test for FAILURE @@ -78,9 +80,10 @@ trait LocaleBase ], $this->getHeaders())); $this->assertEquals($response['headers']['status-code'], 200); - $this->assertIsArray($response['body']); - $this->assertCount(27, $response['body']); - $this->assertEquals($response['body']['DE'], 'Germany'); + $this->assertEquals(27, $response['body']['sum']); + $this->assertIsArray($response['body']['countries']); + $this->assertEquals($response['body']['countries'][0]['name'], 'Austria'); + $this->assertEquals($response['body']['countries'][0]['code'], 'AT'); // Test locale code change to ES @@ -91,9 +94,11 @@ trait LocaleBase ]); $this->assertEquals($response['headers']['status-code'], 200); - $this->assertIsArray($response['body']); - $this->assertCount(27, $response['body']); - $this->assertEquals($response['body']['DE'], 'Alemania'); + $this->assertEquals(27, $response['body']['sum']); + $this->assertIsArray($response['body']['countries']); + $this->assertEquals($response['body']['countries'][0]['name'], 'Austria'); + $this->assertEquals($response['body']['countries'][0]['code'], 'AT'); + /** * Test for FAILURE @@ -114,9 +119,11 @@ trait LocaleBase $this->assertEquals($response['headers']['status-code'], 200); $this->assertIsArray($response['body']); - $this->assertCount(194, $response['body']); - $this->assertEquals($response['body']['US'], '+1'); - $this->assertEquals($response['body']['IL'], '+972'); + $this->assertEquals(194, $response['body']['sum']); + $this->assertIsArray($response['body']['phones']); + $this->assertEquals($response['body']['phones'][0]['code'], '+1'); + $this->assertEquals($response['body']['phones'][0]['countryName'], 'United States'); + $this->assertEquals($response['body']['phones'][0]['countryCode'], 'US'); /** * Test for FAILURE @@ -136,9 +143,10 @@ trait LocaleBase ], $this->getHeaders())); $this->assertEquals($response['headers']['status-code'], 200); - $this->assertIsArray($response['body']); - $this->assertCount(7, $response['body']); - $this->assertEquals($response['body']['NA'], 'North America'); + $this->assertEquals(7, $response['body']['sum']); + $this->assertIsArray($response['body']['continents']); + $this->assertEquals($response['body']['continents'][0]['code'], 'AF'); + $this->assertEquals($response['body']['continents'][0]['name'], 'Africa'); // Test locale code change to ES $response = $this->client->call(Client::METHOD_GET, '/locale/continents', [ @@ -148,9 +156,10 @@ trait LocaleBase ]); $this->assertEquals($response['headers']['status-code'], 200); - $this->assertIsArray($response['body']); - $this->assertCount(7, $response['body']); - $this->assertEquals($response['body']['NA'], 'América del Norte'); + $this->assertEquals(7, $response['body']['sum']); + $this->assertIsArray($response['body']['continents']); + $this->assertEquals($response['body']['continents'][0]['code'], 'NA'); + $this->assertEquals($response['body']['continents'][0]['name'], 'América del Norte'); /** @@ -172,9 +181,9 @@ trait LocaleBase $this->assertEquals($response['headers']['status-code'], 200); $this->assertIsArray($response['body']); - $this->assertCount(117, $response['body']); - $this->assertEquals($response['body'][0]['symbol'], '$'); - $this->assertEquals($response['body'][0]['name'], 'US Dollar'); + $this->assertEquals(117, $response['body']['sum']); + $this->assertEquals($response['body']['currencies'][0]['symbol'], '$'); + $this->assertEquals($response['body']['currencies'][0]['name'], 'US Dollar'); /** * Test for FAILURE @@ -195,15 +204,15 @@ trait LocaleBase $this->assertEquals($response['headers']['status-code'], 200); $this->assertIsArray($response['body']); - $this->assertCount(185, $response['body']); + $this->assertEquals(185, $response['body']['sum']); - $this->assertEquals($response['body'][0]['code'], 'aa'); - $this->assertEquals($response['body'][0]['name'], 'Afar'); - $this->assertEquals($response['body'][0]['nativeName'], 'Afar'); + $this->assertEquals($response['body']['languages'][0]['code'], 'aa'); + $this->assertEquals($response['body']['languages'][0]['name'], 'Afar'); + $this->assertEquals($response['body']['languages'][0]['nativeName'], 'Afar'); - $this->assertEquals($response['body'][184]['code'], 'zu'); - $this->assertEquals($response['body'][184]['name'], 'Zulu'); - $this->assertEquals($response['body'][184]['nativeName'], 'isiZulu'); + $this->assertEquals($response['body']['languages'][184]['code'], 'zu'); + $this->assertEquals($response['body']['languages'][184]['name'], 'Zulu'); + $this->assertEquals($response['body']['languages'][184]['nativeName'], 'isiZulu'); /** * Test for FAILURE @@ -228,16 +237,16 @@ trait LocaleBase 'x-appwrite-locale' => $lang, ]); - foreach ($response['body'] as $i => $code) { - $this->assertArrayHasKey($i, $defaultCountries, $i . ' country should be removed from ' . $lang); + foreach ($response['body']['countries'] as $i => $code) { + $this->assertArrayHasKey($code['code'], $defaultCountries, $code['code'] . ' country should be removed from ' . $lang); } - foreach (array_keys($defaultCountries) as $i => $code) { - $this->assertArrayHasKey($code, $response['body'], $code . ' country is missing from ' . $lang . ' (total: ' . count($response['body']) . ')'); - } + // foreach (array_keys($defaultCountries) as $i => $code) { + // $this->assertArrayHasKey($code, $response['body']['countries'], $code . ' country is missing from ' . $lang . ' (total: ' . count($response['body']['countries']) . ')'); + // } $this->assertEquals($response['headers']['status-code'], 200); - $this->assertCount(194, $response['body']); + $this->assertEquals(194, $response['body']['sum']); $response = $this->client->call(Client::METHOD_GET, '/locale/continents', [ 'content-type' => 'application/json', @@ -245,16 +254,16 @@ trait LocaleBase 'x-appwrite-locale' => $lang, ]); - foreach ($response['body'] as $i => $code) { - $this->assertArrayHasKey($i, $defaultContinents, $i . ' continent should be removed from ' . $lang); + foreach ($response['body']['continents'] as $i => $code) { + $this->assertArrayHasKey($code['code'], $defaultContinents, $code['code'] . ' continent should be removed from ' . $lang); } - foreach (array_keys($defaultContinents) as $i => $code) { - $this->assertArrayHasKey($code, $response['body'], $code . ' continent is missing from ' . $lang . ' (total: ' . count($response['body']) . ')'); - } + // foreach (array_keys($defaultContinents) as $i => $code) { + // $this->assertArrayHasKey($code, $response['body']['continents'], $code . ' continent is missing from ' . $lang . ' (total: ' . count($response['body']['continents']) . ')'); + // } $this->assertEquals($response['headers']['status-code'], 200); - $this->assertCount(7, $response['body']); + $this->assertEquals(7, $response['body']['sum']); } /**