Skip to content

Commit fc9f2f8

Browse files
committed
XandValidator
0 parents  commit fc9f2f8

File tree

9 files changed

+217
-0
lines changed

9 files changed

+217
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# JetBrains IDE's project files
2+
.idea/*

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CHANGELOG(Registro de alterações)
2+
3+
### [0.0.1] - XandValidator ([@thtmorais](https://github.com/thtmorais))

Diff for: CONTRIBUTING.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
To contribute to this project, all the comments below must be followed.
2+
3+
## CHANGELOG
4+
5+
All changes made to a submission must be registered in the CHANGELOG
6+
7+
Enter version number. Ex.:
8+
9+
[0.0.0] - Yii PHP Framework([@thtmorais](https://github.com/thtmorais))
10+
11+
Just below the version number, you must enter which changes were made with your details. Ex.:
12+
13+
- Yii PHP Framework
14+
15+
## Conventions
16+
17+
It is strictly forbidden not to follow conventions and good practices in this project. We use the [Yii PHP Framework](https://yiiframework.com) and its organization and class hierarchy methods must be respected.
18+
19+
## Desenvolvedores
20+
21+
### Matheus Evangelista Morais - [thtmorais](https://github.com/thtmorais)

Diff for: LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Matheus Evangelista Morais
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 all
13+
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 THE
21+
SOFTWARE.

Diff for: README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Validation suite for Yii PHP Framework 2
2+
---
3+
4+
Installation
5+
---
6+
7+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
8+
9+
Either run
10+
11+
```
12+
composer require thtmorais/yii2-validators "*"
13+
```
14+
15+
or add
16+
17+
```
18+
"thtmorais/yii2-validators": "*"
19+
```
20+
21+
to the require section of your `composer.json` file.
22+
23+
24+
Usage
25+
-----
26+
27+
Once the extension is installed, simply use it in your code by:
28+
29+
### XandValidator
30+
31+
```php
32+
<?php
33+
34+
namespace app\models;
35+
36+
use thtmorais\validators\XandValidator;
37+
38+
/**
39+
* Class Model
40+
*/
41+
class Model extends \yii\base\Model
42+
{
43+
/**
44+
* @var string
45+
*/
46+
public $google_client_id;
47+
48+
/**
49+
* @var string
50+
*/
51+
public $google_client_secret;
52+
53+
/**
54+
* @var string
55+
*/
56+
public $gitlab_client_id;
57+
58+
/**
59+
* @var string
60+
*/
61+
public $gitlab_client_secret;
62+
63+
/**
64+
* @var string
65+
*/
66+
public $gitlab_domain;;
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function rules()
72+
{
73+
return [
74+
[['google_client_id', 'google_client_secret'], XandValidator::class, 'fields' => ['google_client_id', 'google_client_secret']],
75+
[['gitlab_client_id', 'gitlab_client_secret', 'gitlab_domain'], XandValidator::class, 'fields' => ['gitlab_client_id', 'gitlab_client_secret', 'gitlab_domain']],
76+
];
77+
}
78+
}
79+
```

Diff for: composer.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "thtmorais/yii2-validators",
3+
"description": "Validation suite for Yii PHP Framework 2",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2","validators", "xand-validator", "xand", "validator", "XAND"],
6+
"license": "MIT",
7+
"support": {
8+
"issues": "https://github.com/thtmorais/yii2-validators/issues",
9+
"source": "https://github.com/thtmorais/yii2-validators"
10+
},
11+
"authors": [
12+
{
13+
"name": "Matheus Evangelista Morais",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require": {
18+
"yiisoft/yii2": "~2.0.0"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"thtmorais\\validators\\": "src"
23+
}
24+
}
25+
}

Diff for: src/XandValidator.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace thtmorais\validators;
4+
5+
use Yii;
6+
use yii\validators\Validator;
7+
use yii\i18n\PhpMessageSource;
8+
9+
/**
10+
* Class XandValidator
11+
* @package thtmorais\validators
12+
*/
13+
class XandValidator extends Validator
14+
{
15+
/**
16+
* @var array
17+
*/
18+
public $fields;
19+
20+
public function init()
21+
{
22+
parent::init();
23+
24+
Yii::setAlias('@validators', dirname(__DIR__));
25+
Yii::$app->i18n->translations['validators'] = [
26+
'class' => PhpMessageSource::class,
27+
'basePath' => '@validators/src/messages'
28+
];
29+
}
30+
31+
/**
32+
* @param $model
33+
* @param $attribute
34+
* @return void
35+
*/
36+
public function validateAttribute($model, $attribute)
37+
{
38+
$values = [];
39+
40+
foreach ($this->fields as $field) {
41+
$values[] = $model->$field;
42+
}
43+
44+
$filledCount = count(array_filter($values, function ($value) {
45+
return !empty($value);
46+
}));
47+
48+
if ($filledCount != 0 && $filledCount != count($this->fields)) {
49+
$this->addError($model, $attribute, Yii::t('validators', 'All or none of the specified fields must be filled.'));
50+
51+
foreach ($this->fields as $field) {
52+
$this->addError($model, $field, Yii::t('validators', 'All or none of the specified fields must be filled.'));
53+
}
54+
}
55+
}
56+
}

Diff for: src/messages/en-US/validators.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'All or none of the specified fields must be filled.' => 'All or none of the specified fields must be filled.'
5+
];

Diff for: src/messages/pt-BR/validators.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'All or none of the specified fields must be filled.' => 'Todos ou nenhum dos campos especificados devem ser preenchidos.'
5+
];

0 commit comments

Comments
 (0)