Skip to content

Commit 26ac957

Browse files
committed
Create package
0 parents  commit 26ac957

14 files changed

+788
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[{package.json,*.scss,*.css}]
15+
indent_size = 2
16+
17+
[*.md]
18+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
.idea
3+
composer.lock
4+
phpunit.xml

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2017 Ivan Vermeyen (<ivan@codezero.be>)
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Laravel Unique Translation
2+
3+
#### Check if a translated value in a JSON column is unique in the database.
4+
5+
Imagine you want store a `slug` for a `Post` model in different languages.
6+
7+
The amazing [`spatie/laravel-translatable`](https://github.com/spatie/laravel-translatable) package makes this a cinch!
8+
9+
But then you want to make sure each translation is unique for its language.
10+
11+
That's where this package comes in to play.
12+
13+
## Requirements
14+
15+
- PHP >= 7.0
16+
- MySQL >= 5.7
17+
- [Laravel](https://laravel.com/) >= 5.5
18+
- [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable)
19+
20+
## Installation
21+
22+
Require the package via Composer:
23+
24+
```
25+
composer require codezero/laravel-unique-translation
26+
```
27+
Laravel will automatically register the [ServiceProvider](https://github.com/codezero-be/laravel-unique-translation/blob/master/src/UniqueTranslationServiceProvider.php).
28+
29+
## Usage
30+
31+
For the following examples, I will use a `slug` in a `posts` table as the subject of our validation.
32+
33+
### Validate a Single Translation
34+
35+
Your form can submit a single slug:
36+
37+
```html
38+
<input name="slug">
39+
```
40+
41+
We can then check if it is unique **in the current locale**:
42+
43+
```php
44+
$attributes = request()->validate([
45+
'slug' => ['unique_translation:posts'],
46+
]);
47+
```
48+
49+
You could also use the Rule instance:
50+
51+
```php
52+
use CodeZero\UniqueTranslation\UniqueTranslationRule;
53+
54+
$attributes = request()->validate([
55+
'slug' => [new UniqueTranslationRule('posts')],
56+
]);
57+
```
58+
59+
### Validate an Array of Translations
60+
61+
Your form can also submit an array of slugs.
62+
63+
```html
64+
<input name="slug[en]">
65+
<input name="slug[nl]">
66+
```
67+
68+
We need to validate the entire array in this case. Mind the `slug.*` key.
69+
70+
```php
71+
$attributes = request()->validate([
72+
'slug.*' => ['unique_translation:posts'],
73+
// or...
74+
'slug.*' => [new UniqueTranslationRule('posts')],
75+
]);
76+
```
77+
78+
### Specify a Column
79+
80+
Maybe your form field has a name of `post_slug` and your database field `slug`:
81+
82+
```php
83+
$attributes = request()->validate([
84+
'post_slug.*' => ['unique_translation:posts,slug'],
85+
// or...
86+
'post_slug.*' => [new UniqueTranslationRule('posts', 'slug')],
87+
]);
88+
```
89+
90+
### Ignore a Record with ID
91+
92+
If you're updating a record, you may want to ignore the post itself from the unique check.
93+
94+
```php
95+
$attributes = request()->validate([
96+
'slug.*' => ["unique_translation:posts,slug,{$post->id}"],
97+
// or...
98+
'slug.*' => [(new UniqueTranslationRule('posts'))->ignore($post->id)],
99+
]);
100+
```
101+
102+
### Ignore Records with a Specific Column and Value
103+
104+
If your ID column has a different name, or you just want to use another column:
105+
106+
```php
107+
$attributes = request()->validate([
108+
'slug.*' => ['unique_translation:posts,slug,ignore_value,ignore_column'],
109+
// or...
110+
'slug.*' => [(new UniqueTranslationRule('posts'))->ignore('ignore_value', 'ignore_column')],
111+
]);
112+
```
113+
114+
## Example
115+
116+
Your existing `slug` column (JSON) in a `posts` table:
117+
118+
```json
119+
{
120+
"en":"not-abc",
121+
"nl":"abc"
122+
}
123+
```
124+
125+
Your form input to create a new record:
126+
127+
128+
```html
129+
<input name="slug[en]" value="abc">
130+
<input name="slug[nl]" value="abc">
131+
```
132+
133+
Your validation logic:
134+
135+
```php
136+
$attributes = request()->validate([
137+
'slug.*' => ['unique_translation:posts'],
138+
]);
139+
```
140+
141+
The result is that `slug[en]` is valid, since the only `en` value in the database is `not-abc`.
142+
143+
And `slug[nl]` would fail, because there already is a `nl` value of `abc`.
144+
145+
146+
## Testing
147+
148+
```
149+
vendor/bin/phpunit
150+
```
151+
152+
## Security
153+
154+
If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.
155+
156+
## License
157+
158+
The MIT License (MIT). Please see [License File](https://github.com/codezero-be/laravel-unique-translation/blob/master/LICENSE.md) for more information.

composer.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "codezero/laravel-unique-translation",
3+
"description": "",
4+
"keywords": [],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Ivan Vermeyen",
9+
"email": "ivan@codezero.be"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.0.0"
14+
},
15+
"require-dev": {
16+
"fzaninotto/faker": "~1.4",
17+
"mockery/mockery": "0.9.*",
18+
"orchestra/testbench": "~3.5.0",
19+
"phpunit/phpunit": "~6.0",
20+
"spatie/laravel-translatable": "^2.1"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"CodeZero\\UniqueTranslation\\": "src"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"CodeZero\\UniqueTranslation\\Tests\\": "tests"
30+
}
31+
},
32+
"extra": {
33+
"laravel": {
34+
"providers": [
35+
"CodeZero\\UniqueTranslation\\UniqueTranslationServiceProvider"
36+
]
37+
}
38+
},
39+
"minimum-stability": "stable",
40+
"sort-packages": true,
41+
"optimize-autoloader": true
42+
}

phpunit.xml.dist

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="CodeZero">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./app</directory>
19+
</whitelist>
20+
</filter>
21+
<php>
22+
<env name="APP_ENV" value="testing"/>
23+
<env name="CACHE_DRIVER" value="array"/>
24+
<env name="SESSION_DRIVER" value="array"/>
25+
<env name="QUEUE_DRIVER" value="sync"/>
26+
<env name="DB_CONNECTION" value="mysql"/>
27+
<env name="DB_DATABASE" value="testing"/>
28+
<env name="DB_USERNAME" value="root"/>
29+
<env name="DB_PASSWORD" value="root"/>
30+
</php>
31+
</phpunit>

0 commit comments

Comments
 (0)