From 176730006352af1e55192dabd5f173ad2f94f646 Mon Sep 17 00:00:00 2001 From: OvieDev Date: Mon, 19 Sep 2022 20:23:18 +0200 Subject: [PATCH] Merged pre-release to master branch --- composer.json | 3 + source/Scheme.php | 108 ++++++++++++++++++++++++++++++++ source/Translator.php | 34 ++-------- tests/SchemeTests.php | 71 +++++++++++++++++++++ tests/test_component_schem.json | 10 +++ 5 files changed, 198 insertions(+), 28 deletions(-) create mode 100644 source/Scheme.php create mode 100644 tests/SchemeTests.php create mode 100644 tests/test_component_schem.json diff --git a/composer.json b/composer.json index b3ff1dc..453a9fe 100644 --- a/composer.json +++ b/composer.json @@ -16,5 +16,8 @@ ], "require": { "simplehtmldom/simplehtmldom": "2.0-RC2" + }, + "require-dev": { + "phpunit/phpunit": "9.5.24" } } diff --git a/source/Scheme.php b/source/Scheme.php new file mode 100644 index 0000000..3c2cdd8 --- /dev/null +++ b/source/Scheme.php @@ -0,0 +1,108 @@ +json = json_decode($json_raw, true); + if (!isset($this->json)) { + throw new Exception("Invalid JSON"); + } + $this->schema = $this->json['schema']; + $this->keys = $this->json['keys']; + foreach ($this->keys as $key) { + if (count($key)!=count($this->schema)) { + throw new Exception("Key breaks schema"); + } + } + $this->fallback = $fallback; + } + + public function reset_scheme(): void + { + $this->schema = $this->json['schema']; + $this->keys = $this->json['keys']; + } + + public function modify_key(string $keyname, array $keys): void + { + if (count($keys)!=count($this->schema)) { + throw new Exception("Key order doesn't match the schema"); + } + $this->keys[$keyname] = $keys; + } + + public function delete_key(string $keyname): void + { + if (!array_key_exists($keyname, $this->keys)) { + throw new Exception("Key doesn't exists"); + } + + unset($this->keys[$keyname]); + } + + public function push_to_schema(string $lang, bool $use_fallback, array $values): void + { + $this->schema[] = $lang; + if ($use_fallback) { + foreach ($this->keys as &$key) { + $key[] = $this->fallback; + } + } + else { + if (count($values)==count($this->keys)) { + $i = 0; + foreach ($this->keys as &$key) { + $key[] = $values[$i]; + $i++; + } + } + else { + throw new Exception('$values don\'t match keys number'); + } + } + } + + public function pop_from_schema(): void { + array_pop($this->schema); + foreach ($this->keys as &$key) { + array_pop($key); + } + } + + public function get_keys() + { + return $this->keys; + } + + public function get_schema() + { + return $this->schema; + } + + public function get_key_with_lang(string $key, string $lang): string + { + if (!in_array($lang, $this->schema)) { + $lang = $this->schema[0]; + } + if (!array_key_exists($key, $this->keys)) { + throw new Exception("Key doesn't exists"); + } + + return $this->keys[$key][array_search($lang, $this->schema)]; + } + +} \ No newline at end of file diff --git a/source/Translator.php b/source/Translator.php index dbbe45f..83a4d3f 100644 --- a/source/Translator.php +++ b/source/Translator.php @@ -2,20 +2,17 @@ namespace OvieDev\Toungette; -use Exception; +use OvieDev\Toungette\Scheme; use simplehtmldom\HtmlDocument; class Translator { - private string $template_path; private string $page_template; - private array $json_template; + public Scheme $scheme; public string $lang; - private array $langs; public string $text; public function __construct($template, $page, $lang=null) { - $this->template_path = $template; $this->page_template = $page; if (isset($_GET['lang'])) { $this->lang = $_GET['lang']; @@ -26,32 +23,13 @@ public function __construct($template, $page, $lang=null) { else { $this->lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); } - $this->parse_lang_template(); - } - - public function parse_lang_template(): void - { - $json_raw = file_get_contents($this->template_path); - if (!$json_raw) { - throw new Exception("Couldn't find file or open it"); - } - $json = json_decode($json_raw, true); - if (!isset($json)) { - throw new Exception("Invalid JSON"); - } - $this->json_template = $json; - $this->langs = $this->json_template['schema']; - foreach ($this->json_template['keys'] as $key) { - if (count($key)!=count($this->langs)) { - throw new Exception("Key breaks schema"); - } - } + $this->scheme = new Scheme($template, "Fallback text"); } private function get_lang_index(): int { $index = 0; - foreach ($this->langs as $l) { + foreach ($this->scheme->get_schema() as $l) { if ($l==$this->lang) { return $index; } @@ -90,9 +68,9 @@ private function translate_links($page): string public function translate(): void { $html = file_get_contents($this->page_template); - foreach (array_keys($this->json_template['keys']) as $key) { + foreach (array_keys($this->scheme->get_keys()) as $key) { $pattern = '/(?json_template['keys'][$key][$this->get_lang_index()], $html); + $html = preg_replace($pattern, $this->scheme->get_keys()[$key][$this->get_lang_index()], $html); } $html = preg_replace('/(?<=[\s\t\n])\//', '', $html); $html = $this->translate_links($html); diff --git a/tests/SchemeTests.php b/tests/SchemeTests.php new file mode 100644 index 0000000..1a501c7 --- /dev/null +++ b/tests/SchemeTests.php @@ -0,0 +1,71 @@ +scheme = new Scheme("schem.json", "fallback"); + } + + public function test_add(): void { + $this->scheme->modify_key("key.new", ["hey", "im", "new"]); + + $this->assertSame(["hey", "im", "new"], $this->scheme->get_keys()["key.new"]); + } + + /** + * @depends test_add + */ + public function test_reset(): void { + $this->scheme = new Scheme("schem.json", "fallback"); + $this->scheme->modify_key("key.new", ["hey", "im", "new"]); + $this->scheme->reset_scheme(); + + $this->assertSame(1, count($this->scheme->get_keys())); + } + + + public function test_remove_key(): void + { + $this->scheme->delete_key("key.coolkey"); + + $this->assertArrayNotHasKey("key.coolkey", $this->scheme->get_keys()); + + $this->expectException(Exception::class); + $this->scheme->delete_key("key.notexist"); + } + + public function test_push(): void + { + $this->scheme->push_to_schema("de", true, []); + + $this->assertContains("de", $this->scheme->get_schema()); + $this->assertContains($this->scheme->fallback, $this->scheme->get_keys()["key.coolkey"]); + + $this->scheme->push_to_schema("fr", false, ["not a fallback"]); + + $this->assertContains("fr", $this->scheme->get_schema()); + $this->assertContains("not a fallback", $this->scheme->get_keys()["key.coolkey"]); + } + + /** + * @depends test_push + */ + public function test_pop(): void + { + $this->scheme->push_to_schema("de", true, []); + + $this->scheme->pop_from_schema(); + $this->assertSame(3, count($this->scheme->get_schema())); + } + + public function test_find_key(): void + { + $val = $this->scheme->get_key_with_lang("key.coolkey", "en"); + $this->assertSame("hi", $val); + } +} diff --git a/tests/test_component_schem.json b/tests/test_component_schem.json new file mode 100644 index 0000000..e6b253d --- /dev/null +++ b/tests/test_component_schem.json @@ -0,0 +1,10 @@ +{ + "schema": ["en", "pl", "es"], + "keys": { + "key.coolkey": [ + "hi", + "cool", + "key" + ] + } +} \ No newline at end of file