Skip to content

Commit

Permalink
- Move Documentation from ReadME to Docs
Browse files Browse the repository at this point in the history
- Fix PHPDocs, and Code Style
  • Loading branch information
amjadbanimattar committed Apr 23, 2024
1 parent be1c5de commit b46b3cc
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 225 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/.phpunit.cache
/build/
/coverage.clover
.history
57 changes: 0 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,63 +68,6 @@ $post = Post::create($data);
echo $post->translate('fr')->title; // Mon premier post
```

### **Validating Unique and Exists Rule**

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...

$person = new Person(['name' => 'john doe']);
$person->save();

// Option 1
$data = [
'name' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name' => ['required', new TranslatableUnique(Person::class, 'name')],
]);

// Option 2
$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];

$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')],
]);

```

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableExists;
...

$person = new Person(['name' => 'john doe']);
$person->save();

// Option 1
$data = [
'name' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name' => ['required', new TranslatableExists(Person::class, 'name')],
]);

// Option 2
$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];

$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableExists(Person::class, 'name:en')],
]);
```

## Tutorials

- [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent)
Expand Down
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
- [Attributes](usage/attributes.md)
- [Forms](usage/forms.md)
- [Pivot Model](usage/pivot-model.md)
- [Custom Validation Rules](usage/custom-validation-rule.md)
107 changes: 107 additions & 0 deletions docs/usage/custom-validation-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
### **Validating Unique and Exists Rule**

You can use custom rules to validate unique and exists rules for translatable attributes.

#### TranslatableUnique

Ensure that the attribute value is unique by checking its absence in the database; if the value already exists, raise a validation exception.

##### Option 1

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
'name' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name' => ['required', new TranslatableUnique(Person::class, 'name')],
]);

```

##### Option 2

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];

$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')],
]);

```

##### Option 2

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];

$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')],
]);

```


#### TranslatableExists

Verify if the attribute value exists by confirming its presence in the database; if the value does not exist, raise a validation exception.


##### Option 1
```php
use Astrotomic\Translatable\Validation\Rules\TranslatableExists;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
'name' => 'john doe',
'email' => 'john@example.com'
];
$validator = Validator::make($data, [
'name' => ['required', new TranslatableExists(Person::class, 'name')],
]);
```

##### Option 2
```php
use Astrotomic\Translatable\Validation\Rules\TranslatableExists;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
'name:en' => 'john doe',
'email' => 'john@example.com'
];

$validator = Validator::make($data, [
'name:en' => ['required', Rule::translatableExists(Person::class, 'name:en')],
]);
```
6 changes: 3 additions & 3 deletions src/Translatable/Contracts/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function disableDeleteTranslationsCascade(): void;
public static function enableDeleteTranslationsCascade(): void;

/**
* @param string|array<string>|null $locales
* @param string|array<string>|null $locales
*/
public function deleteTranslations(string|array|null $locales = null): void;

Expand All @@ -31,7 +31,7 @@ public function getTranslation(?string $locale = null, ?bool $withFallback = nul

public function getTranslationOrNew(?string $locale = null): Model;

/**
/**
* @return array<string,array<string,mixed>>
*/
public function getTranslationsArray(): array;
Expand All @@ -41,7 +41,7 @@ public function hasTranslation(?string $locale = null): bool;
public function isTranslationAttribute(string $key): bool;

/**
* @param null|array<string> $except
* @param null|array<string> $except
*/
public function replicateWithTranslations(?array $except = null): Model;

Expand Down
3 changes: 2 additions & 1 deletion src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Astrotomic\Translatable;

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Traits\Relationship;
use Astrotomic\Translatable\Traits\Scopes;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -305,7 +306,7 @@ public function setAttribute($key, $value)
return parent::setAttribute($key, $value);
}

public function setDefaultLocale(?string $locale)
public function setDefaultLocale(?string $locale): TranslatableContract
{
$this->defaultLocale = $locale;

Expand Down
8 changes: 4 additions & 4 deletions src/Translatable/TranslatableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class TranslatableServiceProvider extends ServiceProvider
public function boot(): void
{
$this->publishes([
__DIR__ . '/../config/translatable.php' => config_path('translatable.php'),
__DIR__.'/../config/translatable.php' => config_path('translatable.php'),
], 'translatable');

$this->loadTranslationsFrom(__DIR__ . '/../lang', 'translatable');
$this->loadTranslationsFrom(__DIR__.'/../lang', 'translatable');
$this->publishes([
__DIR__ . '/../lang' => $this->app->langPath('vendor/translatable'),
__DIR__.'/../lang' => $this->app->langPath('vendor/translatable'),
], 'translatable-lang');
}

public function register(): void
{
$this->mergeConfigFrom(
__DIR__ . '/../config/translatable.php',
__DIR__.'/../config/translatable.php',
'translatable'
);

Expand Down
30 changes: 13 additions & 17 deletions src/Translatable/Validation/RuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ public function __construct(Repository $config, ?int $format = null, ?string $pr
/**
* Create a set of validation rules.
*
* @param array<mixed> $rules The validation rules to be parsed.
* @param int|null $format The format to be used for parsing (e.g., 'dot' or 'bracket').
* @param string|null $prefix The prefix to be applied to each rule key.
* @param string|null $suffix The suffix to be applied to each rule key.
* @param array<string>|null $locales The locales to be used for translating rule attributes.
*
* @param array<mixed> $rules The validation rules to be parsed.
* @param int|null $format The format to be used for parsing (e.g., 'dot' or 'bracket').
* @param string|null $prefix The prefix to be applied to each rule key.
* @param string|null $suffix The suffix to be applied to each rule key.
* @param array<string>|null $locales The locales to be used for translating rule attributes.
* @return array<string,mixed> The parsed validation rules.
*/
public static function make(array $rules, ?int $format = null, ?string $prefix = null, ?string $suffix = null, ?array $locales = null): array
Expand All @@ -63,10 +62,8 @@ public static function make(array $rules, ?int $format = null, ?string $prefix =
/**
* Set the locales to be used for translating rule attributes.
*
* @param array<string>|null $locales The locales to be set. If null, all available locales will be used.
*
* @return self
*
* @param array<string>|null $locales The locales to be set. If null, all available locales will be used.
*
* @throws \InvalidArgumentException If a provided locale is not defined in the available locales.
*/
public function setLocales(?array $locales = null): self
Expand All @@ -81,7 +78,7 @@ public function setLocales(?array $locales = null): self
}

foreach ($locales as $locale) {
if (!$helper->has($locale)) {
if (! $helper->has($locale)) {
throw new InvalidArgumentException(sprintf('The locale [%s] is not defined in available locales.', $locale));
}
}
Expand All @@ -94,16 +91,15 @@ public function setLocales(?array $locales = null): self
/**
* Parse the input array of rules, applying format and translation to translatable attributes.
*
* @param array<mixed> $input The input array of rules to be parsed.
*
* @param array<mixed> $input The input array of rules to be parsed.
* @return array<mixed> The parsed array of rules.
*/
public function parse(array $input): array
{
$rules = [];

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

continue;
Expand Down Expand Up @@ -154,10 +150,10 @@ protected function getReplacement(string $locale): string
{
switch ($this->format) {
case self::FORMAT_KEY:
return '$1:' . $locale;
return '$1:'.$locale;
default:
case self::FORMAT_ARRAY:
return $locale . '.$1';
return $locale.'.$1';
}
}

Expand All @@ -166,7 +162,7 @@ protected function getPattern(): string
$prefix = preg_quote($this->prefix);
$suffix = preg_quote($this->suffix);

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

protected function isTranslatable(string $key): bool
Expand Down
Loading

0 comments on commit b46b3cc

Please sign in to comment.