Skip to content

Commit

Permalink
Merge pull request #4 from ElegantEngineeringTech/dev
Browse files Browse the repository at this point in the history
V2 new driver architecture
  • Loading branch information
QuentinGab authored Nov 17, 2024
2 parents e5928b4 + a035876 commit 13be271
Show file tree
Hide file tree
Showing 42 changed files with 1,784 additions and 1,923 deletions.
512 changes: 186 additions & 326 deletions README.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions config/translator.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?php

use Elegantly\Translator\Drivers\PhpDriver;

return [

/**
* Possible values are: 'php', 'json' or any class-string<Driver>
*/
'driver' => PhpDriver::class,

/*
|--------------------------------------------------------------------------
| Language Paths
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 5
level: 7
paths:
- src
- config
Expand Down
9 changes: 9 additions & 0 deletions src/Caches/SearchCodeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ class SearchCodeCache
{
const FILENAME = 'translationsByFiles.php';

/**
* @var array<string, array{ created_at: int, translations: string[] }>
*/
protected ?array $value = null;

public function __construct(public Filesystem $storage)
{
//
}

/**
* @return array<string, array{ created_at: int, translations: string[] }>
*/
public function getValue(): ?array
{
if ($this->value) {
Expand All @@ -29,6 +35,9 @@ public function getValue(): ?array
return $this->load();
}

/**
* @return array<string, array{ created_at: int, translations: string[] }>
*/
public function load(): ?array
{
if ($this->storage->exists(static::FILENAME)) {
Expand Down
61 changes: 4 additions & 57 deletions src/Collections/JsonTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,11 @@

namespace Elegantly\Translator\Collections;

use Illuminate\Support\Collection;
use Elegantly\Translator\Drivers\JsonDriver;

/**
* @extends Collection<string, string|int|float|null>
*/
final class JsonTranslations extends Collection implements TranslationsInterface
class JsonTranslations extends Translations
{
public function set(string|int|null $key, mixed $value): static
{
return $this->put($key, $value);
}
public string $driver = JsonDriver::class;

public function sanitize(): static
{
return $this;
}

public function sortNatural(): static
{
return $this->sortKeys(SORT_NATURAL);
}

public function toDotTranslations(bool $filter = false): Collection
{
return $this
->toBase()
->when(
$filter,
fn ($c) => $c->filter(fn ($value) => ! blank($value))
);
}

public function toTranslationsKeys(bool $filter = false): Collection
{
return $this->toDotTranslations($filter)->keys();
}

public function toTranslationsValues(bool $filter = false): Collection
{
return $this->toDotTranslations($filter)->values();
}

public function diffTranslationsKeys(Collection $translations): Collection
{
$translationsKeys = $translations
->dot()
->filter(fn ($item) => ! blank($item))
->keys()
->toBase();

return $this
->toTranslationsKeys()
->diff($translationsKeys)
->values();
}

public function toFile(): string
{
return $this->toJson(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
//
}
Loading

0 comments on commit 13be271

Please sign in to comment.