Skip to content

Commit

Permalink
Works with data objects
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 1, 2024
1 parent cfd568b commit 4fdc5f4
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 37 deletions.
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,28 @@
"illuminate/support": "^10.0 || ^11.0",
"laravel-lang/locale-list": "^1.4"
},
"require-dev": {
"orchestra/testbench": "^9.1",
"pestphp/pest": "^2.34"
},
"minimum-stability": "stable",
"prefer-stable": true,
"autoload": {
"psr-4": {
"LaravelLang\\Config\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
"dragon-code/codestyler": true,
"ergebnis/composer-normalize": true,
"friendsofphp/php-cs-fixer": true,
"pestphp/pest-plugin": true,
"symfony/thanks": true
},
"preferred-install": "dist",
Expand All @@ -59,5 +69,8 @@
"LaravelLang\\Config\\ServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/pest"
}
}
24 changes: 12 additions & 12 deletions config/public.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@

'align' => (bool)env('LOCALIZATION_ALIGN', true),

/*
* The language codes chosen for the files in this repository may not
* match the preferences for your project.
*
* Specify here mappings of localizations with your project.
*/

'aliases' => [
// \LaravelLang\LocaleList\Locale::German->value => 'de-DE',
// \LaravelLang\LocaleList\Locale::GermanSwitzerland->value => 'de-CH',
],

/*
* This option determines the mechanism for converting translation
* keys into a typographic version.
Expand Down Expand Up @@ -80,16 +92,4 @@
],
],
],

/*
* The language codes chosen for the files in this repository may not
* match the preferences for your project.
*
* Specify here mappings of localizations with your project.
*/

'aliases' => [
// \LaravelLang\LocaleList\Locale::German->value => 'de-DE',
// \LaravelLang\LocaleList\Locale::GermanSwitzerland->value => 'de-CH',
],
];
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
19 changes: 19 additions & 0 deletions src/Concerns/HasKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Concerns;

use BackedEnum;

trait HasKey
{
protected function resolveKey(int | string | BackedEnum $key): int | string
{
if ($key instanceof BackedEnum) {
return $key->value ?? $key->name;
}

return $key;
}
}
28 changes: 28 additions & 0 deletions src/Concerns/HasValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Concerns;

use BackedEnum;

trait HasValues
{
use HasKey;

public function get(int | string | BackedEnum $key): mixed
{
if (!is_null($this->default)) {
$default = $this->default . '.' . $this->resolveKey($key);
}

return $this->value($this->key . '.' . $this->resolveKey($key), $default ?? null);
}

protected function value(string $key, ?string $default = null): mixed
{
return $default
? config()->get($key, config()->get($default))
: config()->get($key);
}
}
5 changes: 3 additions & 2 deletions src/Data/HiddenData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class HiddenData
public function __construct(
public PushableData $plugins,
public PushableData $packages,
public readonly array $map,
) {}
public NonPushableData $map,
) {
}
}
23 changes: 23 additions & 0 deletions src/Data/NonPushableData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Data;

use LaravelLang\Config\Concerns\HasValues;

class NonPushableData
{
use HasValues;

public function __construct(
protected readonly string $key,
protected readonly ?string $default = null
) {
}

public function all(): array
{
return config($this->key, config($this->default, []));
}
}
23 changes: 15 additions & 8 deletions src/Data/PushableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@

namespace LaravelLang\Config\Data;

use BackedEnum;
use LaravelLang\Config\Concerns\HasValues;

class PushableData
{
use HasValues;

public function __construct(
public array $items
) {}
protected readonly string $key,
protected readonly ?string $default = null
) {
}

public function get(): array
public function all(): array
{
return $this->items;
return config($this->key, config($this->default));
}

public function push(mixed $value): static
{
$this->items[] = $value;
config()->push($this->key, $value);

return $this;
}

public function set(int|string $key, mixed $value): static
public function set(int | string | BackedEnum $key, mixed $value): mixed
{
$this->items[$key] = $value;
config()->set($key = $this->key . '.' . $this->resolveKey($key), $value);

return $this;
return $this->get($key);
}
}
5 changes: 3 additions & 2 deletions src/Data/SharedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class SharedData
public function __construct(
public bool $inline,
public bool $align,
public array $aliases,
public NonPushableData $aliases,
public SmartPunctuationData $smartPunctuation,
) {}
) {
}
}
2 changes: 1 addition & 1 deletion src/Data/SmartPunctuationData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class SmartPunctuationData
public function __construct(
public bool $enabled,
public array $common,
public array $locales,
public NonPushableData $locales,
) {}
}
37 changes: 25 additions & 12 deletions src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Config\Repository;
use LaravelLang\Config\Data\HiddenData;
use LaravelLang\Config\Data\NonPushableData;
use LaravelLang\Config\Data\PushableData;
use LaravelLang\Config\Data\SharedData;
use LaravelLang\Config\Data\SmartPunctuationData;
Expand All @@ -18,38 +19,50 @@ public static function shared(): SharedData
return new SharedData(
inline: static::value(Name::Shared, 'inline'),
align: static::value(Name::Shared, 'align'),
aliases: static::value(Name::Shared, 'aliases'),
aliases: static::value(Name::Shared, 'aliases', object: PushableData::class),
smartPunctuation: static::smartPunctuation()
);
}

public static function hidden(): HiddenData
{
return new HiddenData(
plugins: static::value(Name::Hidden, 'plugins', PushableData::class),
packages: static::value(Name::Hidden, 'packages', PushableData::class),
map: static::value(Name::Hidden, 'map'),
plugins: static::value(Name::Hidden, 'plugins', object: PushableData::class),
packages: static::value(Name::Hidden, 'packages', object: PushableData::class),
map: static::value(Name::Hidden, 'map', object: NonPushableData::class),
);
}

protected static function smartPunctuation(): SmartPunctuationData
{
return new SmartPunctuationData(
enabled: static::value(Name::Shared, 'enable'),
common: static::value(Name::Shared, 'common'),
locales: static::value(Name::Shared, 'locales'),
enabled: static::value(Name::Shared, 'smart_punctuation.enabled'),
common: static::value(Name::Shared, 'smart_punctuation.common'),

locales: static::value(
Name::Shared,
'smart_punctuation.locales',
'smart_punctuation.common',
PushableData::class
),
);
}

protected static function value(Name $name, string $key, ?string $object = null): mixed
protected static function value(Name $name, string $key, ?string $default = null, ?string $object = null): mixed
{
$value = static::repository($name->value . '.' . $key);
if (is_null($object)) {
return static::repository($name->value . '.' . $key, $default);
}

return is_null($object) ? $value : new $object($value);
return new $object($name->value . '.' . $key, $name->value, '.' . $default);
}

protected static function repository(string $key): mixed
protected static function repository(string $key, ?string $default = null): mixed
{
return app(Repository::class)->get($key);
if (!is_null($default)) {
$default = static::repository($default);
}

return app(Repository::class)->get($key, $default);
}
}
3 changes: 3 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(strict_types=1);
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Tests;

use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
}
49 changes: 49 additions & 0 deletions tests/Unit/Facades/Config/HiddenWithEnvTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

use LaravelLang\Config\Facades\Config;

beforeAll(function () {
putenv('LOCALIZATION_INLINE=true');
putenv('LOCALIZATION_ALIGN=true');
putenv('LOCALIZATION_SMART_ENABLED=true');
});

test('plugins', function () {
config(['localization-private.plugins' => ['foo', 'bar']]);

expect(Config::hidden()->plugins->all())
->toBeArray()
->toBe(['foo', 'bar'])
->toBe(config('localization-private.plugins'));

Config::hidden()->plugins->push('baz');

expect(Config::hidden()->plugins->all())
->toBeArray()
->toBe(['foo', 'bar', 'baz'])
->toBe(config('localization-private.plugins'));
});

test('packages', function () {
config(['localization-private.packages' => ['foo', 'bar']]);

expect(Config::hidden()->packages->all())
->toBeArray()
->toBe(['foo', 'bar'])
->toBe(config('localization-private.packages'));

Config::hidden()->packages->push('baz');

expect(Config::hidden()->packages->all())
->toBeArray()
->toBe(['foo', 'bar', 'baz'])
->toBe(config('localization-private.packages'));
});

test('map', function () {
expect(Config::hidden()->map->all())
->toBeArray()
->toBe(config('localization-private.map'));
});
Loading

0 comments on commit 4fdc5f4

Please sign in to comment.