From 52964a425a15dc0a1cad982c1deb03680fede29f Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Fri, 10 Jul 2020 16:29:15 +0300 Subject: [PATCH] Moved functions to template class --- app/controllers/web/home.php | 44 ++++------------------------ src/Appwrite/Template/Template.php | 30 +++++++++++++++++++ tests/unit/Template/TemplateTest.php | 12 ++++++++ 3 files changed, 47 insertions(+), 39 deletions(-) diff --git a/app/controllers/web/home.php b/app/controllers/web/home.php index 903458d6c..334d51c3d 100644 --- a/app/controllers/web/home.php +++ b/app/controllers/web/home.php @@ -1,5 +1,6 @@ ['Project' => []], APP_PLATFORM_SERVER => ['Project' => [], 'Key' => []], @@ -414,7 +380,7 @@ App::get('/open-api-2.json') continue; } - $desc = (!empty($route->getLabel('sdk.description', ''))) ? \realpath('../'.$route->getLabel('sdk.description', '')) : null; + $desc = (!empty($route->getLabel('sdk.description', ''))) ? \realpath(__DIR__.'/../../../'.$route->getLabel('sdk.description', '')) : null; $temp = [ 'summary' => $route->getDesc(), @@ -440,7 +406,7 @@ App::get('/open-api-2.json') 'weight' => $route->getOrder(), 'cookies' => $route->getLabel('sdk.cookies', false), 'type' => $route->getLabel('sdk.methodType', ''), - 'demo' => 'docs/examples/'.fromCamelCaseToDash($route->getLabel('sdk.namespace', 'default')).'/'.fromCamelCaseToDash($temp['operationId']).'.md', + 'demo' => 'docs/examples/'. Template::fromCamelCaseToDash($route->getLabel('sdk.namespace', 'default')).'/'.Template::fromCamelCaseToDash($temp['operationId']).'.md', 'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $route->getLabel('sdk.description', ''), 'rate-limit' => $route->getLabel('abuse-limit', 0), 'rate-time' => $route->getLabel('abuse-time', 3600), @@ -478,7 +444,7 @@ App::get('/open-api-2.json') switch ((!empty($validator)) ? \get_class($validator) : '') { case 'Utopia\Validator\Text': $node['type'] = 'string'; - $node['x-example'] = '['.\strtoupper(fromCamelCase($node['name'])).']'; + $node['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']'; break; case 'Utopia\Validator\Boolean': $node['type'] = 'boolean'; @@ -486,7 +452,7 @@ App::get('/open-api-2.json') break; case 'Appwrite\Database\Validator\UID': $node['type'] = 'string'; - $node['x-example'] = '['.\strtoupper(fromCamelCase($node['name'])).']'; + $node['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']'; break; case 'Utopia\Validator\Email': $node['type'] = 'string'; diff --git a/src/Appwrite/Template/Template.php b/src/Appwrite/Template/Template.php index 127afef15..695667ca2 100644 --- a/src/Appwrite/Template/Template.php +++ b/src/Appwrite/Template/Template.php @@ -95,4 +95,34 @@ class Template extends View return \http_build_query($parsed); } + + /** + * From Camel Case + * + * @var string $input + * + * @return string + */ + public static function fromCamelCaseToSnake($input): string + { + \preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); + $ret = $matches[0]; + foreach ($ret as &$match) { + $match = $match == \strtoupper($match) ? \strtolower($match) : \lcfirst($match); + } + + return \implode('_', $ret); + } + + /** + * From Camel Case to Dash Case + * + * @var string $input + * + * @return string + */ + public static function fromCamelCaseToDash($input): string + { + return \str_replace([' ', '_'], '-', \strtolower(\preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $input))); + } } diff --git a/tests/unit/Template/TemplateTest.php b/tests/unit/Template/TemplateTest.php index b09fe67b2..7404e6abf 100644 --- a/tests/unit/Template/TemplateTest.php +++ b/tests/unit/Template/TemplateTest.php @@ -53,4 +53,16 @@ class TemplateTest extends TestCase { $this->assertEquals($this->object->mergeQuery('key1=value1&key2=value2', ['key1' => 'value3', 'key4' => 'value4']), 'key1=value3&key2=value2&key4=value4'); } + + public function testFromCamelCaseToSnake() + { + $this->assertEquals('app_write', Template::fromCamelCaseToSnake('appWrite')); + $this->assertEquals('app_write', Template::fromCamelCaseToSnake('App Write')); + } + + public function testFromCamelCaseToDash() + { + $this->assertEquals('app-write', Template::fromCamelCaseToDash('appWrite')); + $this->assertEquals('app-write', Template::fromCamelCaseToDash('App Write')); + } } \ No newline at end of file