-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
198 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,5 +16,8 @@ | |
], | ||
"require": { | ||
"simplehtmldom/simplehtmldom": "2.0-RC2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "9.5.24" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace OvieDev\Toungette; | ||
|
||
use Exception; | ||
|
||
class Scheme | ||
{ | ||
public string $fallback; | ||
public readonly array $json; | ||
private array $schema; | ||
private array $keys; | ||
|
||
public function __construct($file, $fallback) | ||
{ | ||
$json_raw = file_get_contents($file); | ||
if (!$json_raw) { | ||
throw new Exception("Couldn't find file or open it"); | ||
} | ||
$this->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)]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
use OvieDev\Toungette\Scheme; | ||
|
||
final class SchemeTests extends TestCase{ | ||
|
||
private Scheme $scheme; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"schema": ["en", "pl", "es"], | ||
"keys": { | ||
"key.coolkey": [ | ||
"hi", | ||
"cool", | ||
"key" | ||
] | ||
} | ||
} |