Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rule factory #34

Merged
merged 9 commits into from Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .codeclimate.yml
Expand Up @@ -10,6 +10,8 @@ checks:
enabled: false
method-complexity:
enabled: true
config:
threshold: 6
method-count:
enabled: true
method-lines:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
vendor/
composer.lock
coverage/
.phpunit.result.cache
112 changes: 112 additions & 0 deletions src/Translatable/Validation/RuleFactory.php
@@ -0,0 +1,112 @@
<?php

namespace Astrotomic\Translatable\Validation;

use InvalidArgumentException;
use Astrotomic\Translatable\Locales;
use Illuminate\Contracts\Config\Repository;

class RuleFactory
{
const FORMAT_ARRAY = 1;
const FORMAT_KEY = 2;

/**
* @var int
*/
protected $format;

/**
* @var string
*/
protected $prefix;

/**
* @var string
*/
protected $suffix;

/**
* @var null|array
*/
protected $locales = null;

public function __construct(Repository $config, ?int $format = null, ?string $prefix = null, ?string $suffix = null)
{
$this->format = $format ?? $config->get('translatable.rule_factory.format');
$this->prefix = $prefix ?? $config->get('translatable.rule_factory.prefix');
$this->suffix = $suffix ?? $config->get('translatable.rule_factory.suffix');
}

public static function make(array $rules, ?int $format = null, ?string $prefix = null, ?string $suffix = null, ?array $locales = null): array
{
$factory = app()->make(static::class, compact('format', 'prefix', 'suffix'));

$factory->setLocales($locales);

return $factory->parse($rules);
}

public function setLocales(?array $locales = null): self
{
/** @var Locales */
$helper = app(Locales::class);

if (is_null($locales)) {
$this->locales = $helper->all();

return $this;
}

foreach ($locales as $locale) {
if (! $helper->has($locale)) {
throw new InvalidArgumentException(sprintf('The locale [%s] is not defined in available locales.', $locale));
}
}

$this->locales = $locales;

return $this;
}

public function parse(array $input): array
{
$rules = [];

foreach ($input as $key => $value) {
if (! $this->isTranslatable($key)) {
$rules[$key] = $value;
continue;
}

foreach ($this->locales as $locale) {
$rules[$this->formatKey($locale, $key)] = $value;
}
}

return $rules;
}

protected function formatKey(string $locale, string $key): string
{
switch ($this->format) {
case self::FORMAT_ARRAY:
return preg_replace($this->getPattern(), $locale.'.$1', $key);
case self::FORMAT_KEY:
return preg_replace($this->getPattern(), '$1:'.$locale, $key);
}
}

protected function getPattern(): string
{
$prefix = preg_quote($this->prefix);
$suffix = preg_quote($this->suffix);

return '/'.$prefix.'([^\.'.$prefix.$suffix.']+)'.$suffix.'/';
}

protected function isTranslatable(string $key): bool
{
return strpos($key, $this->prefix) !== false && strpos($key, $this->suffix) !== false;
}
}
15 changes: 15 additions & 0 deletions src/config/translatable.php
Expand Up @@ -131,4 +131,19 @@
|
*/
'to_array_always_loads_translations' => true,

/*
|--------------------------------------------------------------------------
| Configure the default behavior of the rule factory
|--------------------------------------------------------------------------
| The default values used to control the behavior of the RuleFactory.
| Here you can set your own default format and delimiters for
| your whole app.
*
*/
'rule_factory' => [
'format' => \Astrotomic\Translatable\Validation\RuleFactory::FORMAT_ARRAY,
'prefix' => '%',
'suffix' => '%',
],
];