Skip to content

Commit 9ae77fd

Browse files
author
igor-chepurnoi
committed
fix code style
1 parent 525eaa6 commit 9ae77fd

9 files changed

+107
-62
lines changed

.php_cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
$finder = Symfony\CS\Finder::create()
4+
->exclude('vendor')
5+
->in([__DIR__]);
6+
7+
$config = Symfony\CS\Config::create()
8+
->fixers([
9+
'-phpdoc_params',
10+
'-phpdoc_short_description',
11+
'-phpdoc_inline_tag',
12+
'-pre_increment',
13+
'-heredoc_to_nowdoc',
14+
'-spaces_cast',
15+
'-include',
16+
'-phpdoc_no_package',
17+
'concat_with_spaces',
18+
'ordered_use',
19+
'short_array_syntax',
20+
])
21+
->finder($finder);
22+
23+
return $config;

.travis.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ php:
55
- 5.5
66
- 5.6
77
- 7.0
8-
- hhvm
9-
10-
# run build against hhvm but allow them to fail
11-
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
12-
matrix:
13-
fast_finish: true
14-
allow_failures:
15-
- php: hhvm
168

179
# faster builds on new travis setup not using sudo
1810
sudo: false
@@ -21,6 +13,7 @@ sudo: false
2113
cache:
2214
directories:
2315
- $HOME/.composer/cache
16+
- vendor
2417

2518
install:
2619
- travis_retry composer self-update && composer --version
@@ -29,4 +22,5 @@ install:
2922
- travis_retry composer install --prefer-dist --no-interaction
3023

3124
script:
32-
- phpunit --verbose $PHPUNIT_FLAGS
25+
- vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --dry-run --diff
26+
- phpunit --verbose $PHPUNIT_FLAGS

ECCValidator.php

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use yii\validators\Validator;
88

99
/**
10-
*
1110
* ECCValidator class
1211
*
1312
* Credit Card Validator
@@ -17,14 +16,14 @@
1716
*
1817
* References of the Mod 10 algorithm
1918
* http://en.wikipedia.org/wiki/Luhn_algorithm#Mod_10.2B5_Variant
20-
*
2119
*/
2220
class ECCValidator extends Validator
2321
{
2422
/**
25-
*
2623
* Detected Credit Card list
24+
*
2725
* @var string
26+
*
2827
* @link http://en.wikipedia.org/wiki/Bank_card_number#cite_note-NoMoreBankCard-4
2928
*/
3029
const MAESTRO = 'Maestro';
@@ -41,7 +40,6 @@ class ECCValidator extends Validator
4140
const LASER = 'Laser';
4241
const ALL = 'All';
4342
/**
44-
*
4543
* @var array holds the regex patterns to check for valid
4644
* Credit Card number prefixes
4745
*/
@@ -58,29 +56,30 @@ class ECCValidator extends Validator
5856
self::SWITCH_CARD => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
5957
self::ELECTRON => '/^(?:417500|4026\\d{2}|4917\\d{2}|4913\\d{2}|4508\\d{2}|4844\\d{2})\\d{10}$/',
6058
self::LASER => '/^(?:6304|6706|6771|6709)\\d{12}(\\d{2,3})?$/',
61-
self::ALL => '/^(5[1-5][0-9]{14}|4[0-9]{12}([0-9]{3})?|3[47][0-9]{13}|3(0[0-5]|[68][0-9])[0-9]{11}|(6011\d{12}|65\d{14})|(3[0-9]{4}|2131|1800)[0-9]{11}|2(?:014|149)\\d{11}|8699[0-9]{11}|(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?|(?:5020|6\\d{3})\\d{12}|56(10\\d\\d|022[1-5])\\d{10}|(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)|(?:417500|4026\\d{2}|4917\\d{2}|4913\\d{2}|4508\\d{2}|4844\\d{2})\\d{10}|(?:417500|4026\\d{2}|4917\\d{2}|4913\\d{2}|4508\\d{2}|4844\\d{2})\\d{10})$/'
59+
self::ALL => '/^(5[1-5][0-9]{14}|4[0-9]{12}([0-9]{3})?|3[47][0-9]{13}|3(0[0-5]|[68][0-9])[0-9]{11}|(6011\d{12}|65\d{14})|(3[0-9]{4}|2131|1800)[0-9]{11}|2(?:014|149)\\d{11}|8699[0-9]{11}|(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?|(?:5020|6\\d{3})\\d{12}|56(10\\d\\d|022[1-5])\\d{10}|(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)|(?:417500|4026\\d{2}|4917\\d{2}|4913\\d{2}|4508\\d{2}|4844\\d{2})\\d{10}|(?:417500|4026\\d{2}|4917\\d{2}|4913\\d{2}|4508\\d{2}|4844\\d{2})\\d{10})$/',
6260
];
6361
/**
64-
*
6562
* @var string set with selected Credit Card type to check -ie ECCValidator::MAESTRO
6663
*/
6764
public $format = self::ALL;
6865
/**
69-
* @var boolean whether the attribute value can be null or empty. Defaults to true,
70-
* meaning that if the attribute is empty, it is considered valid.
66+
* @var bool whether the attribute value can be null or empty. Defaults to true,
67+
* meaning that if the attribute is empty, it is considered valid
7168
*/
7269
public $allowEmpty = true;
7370

7471
/**
7572
* Validate attribute
73+
*
7674
* @see Validator::validateAttribute()
75+
*
7776
* @param \yii\base\Model $object
7877
* @param string $attribute
78+
*
7979
* @throws Exception
8080
*/
8181
public function validateAttribute($object, $attribute)
8282
{
83-
8483
$value = $object->$attribute;
8584
if ($this->allowEmpty && $this->isEmpty($value)) {
8685
return;
@@ -92,51 +91,52 @@ public function validateAttribute($object, $attribute)
9291
}
9392

9493
/**
95-
*
9694
* Validates a Credit Card number
9795
*
9896
* @param string $creditCardNumber
9997
*
10098
* @throws \yii\base\Exception
99+
*
101100
* @return bool
102101
* @return bool
103102
*/
104103
public function validateNumber($creditCardNumber)
105104
{
106-
107-
if (!$this->checkType())
105+
if (!$this->checkType()) {
108106
throw new Exception(Yii::t('ECCValidator', 'The "format" property must be specified with a supported Credit Card format.'));
107+
}
109108

110109
$creditCardNumber = preg_replace('/[ -]+/', '', $creditCardNumber);
111110

112111
return $this->checkFormat($creditCardNumber) && $this->mod10($creditCardNumber);
113112
}
114113

115114
/**
116-
*
117115
* Validates a Credit Card date
118116
*
119-
* @param integer $creditCardExpiredMonth
120-
* @param integer $creditCardExpiredYear
117+
* @param int $creditCardExpiredMonth
118+
* @param int $creditCardExpiredYear
121119
*
122120
* @return bool
123121
*/
124122
public function validateDate($creditCardExpiredMonth, $creditCardExpiredYear)
125123
{
126-
127124
$currentYear = intval(date('Y'));
128125
$currentMonth = intval(date('m'));
129126

130-
if (is_scalar($creditCardExpiredMonth)) $creditCardExpiredMonth = intval($creditCardExpiredMonth);
131-
if (is_scalar($creditCardExpiredYear)) $creditCardExpiredYear = intval($creditCardExpiredYear);
127+
if (is_scalar($creditCardExpiredMonth)) {
128+
$creditCardExpiredMonth = intval($creditCardExpiredMonth);
129+
}
130+
if (is_scalar($creditCardExpiredYear)) {
131+
$creditCardExpiredYear = intval($creditCardExpiredYear);
132+
}
132133

133134
return is_integer($creditCardExpiredMonth) && is_integer($creditCardExpiredYear) && $creditCardExpiredMonth <= 12
134135
&& ($creditCardExpiredMonth >= 1 && $creditCardExpiredYear > $currentYear
135136
&& $creditCardExpiredYear < $currentYear + 21) || ($creditCardExpiredYear == $currentYear && $creditCardExpiredMonth >= $currentMonth);
136137
}
137138

138139
/**
139-
*
140140
* Validates Credit Card holder
141141
*
142142
* @param string $creditCardHolder
@@ -145,31 +145,29 @@ public function validateDate($creditCardExpiredMonth, $creditCardExpiredYear)
145145
*/
146146
public function validateName($creditCardHolder)
147147
{
148-
149148
return !empty($creditCardHolder) && preg_match('/^[A-Z ]+$/i', $creditCardHolder);
150149
}
151150

152151
/**
153-
*
154152
* Validates holder, number, and dates of Credit Card numbers
155153
*
156154
* @param string $creditCardHolder
157155
* @param string $creditCardNumber
158-
* @param integer $creditCardExpiredMonth
159-
* @param integer $creditCardExpiredYear
156+
* @param int $creditCardExpiredMonth
157+
* @param int $creditCardExpiredYear
160158
*
161159
* @return bool
162160
*/
163161
public function validateAll($creditCardHolder, $creditCardNumber, $creditCardExpiredMonth, $creditCardExpiredYear)
164162
{
165-
166163
return $this->validateName($creditCardHolder) && $this->validateNumber($creditCardNumber) && $this->validateDate($creditCardExpiredMonth, $creditCardExpiredYear);
167-
168164
}
169165

170166
/**
171167
* Checks Credit Card Prefixes
168+
*
172169
* @param $cardNumber
170+
*
173171
* @return bool
174172
*/
175173
protected function checkFormat($cardNumber)
@@ -179,8 +177,11 @@ protected function checkFormat($cardNumber)
179177

180178
/**
181179
* Check credit card number by Mod 10 algorithm
180+
*
182181
* @param $cardNumber
182+
*
183183
* @return bool
184+
*
184185
* @see http://en.wikipedia.org/wiki/Luhn_algorithm#Mod_10.2B5_Variant
185186
*/
186187
protected function mod10($cardNumber)
@@ -199,34 +200,35 @@ protected function mod10($cardNumber)
199200
}
200201
$numSum += $currentNum;
201202
}
202-
return ($numSum % 10 == 0);
203+
204+
return $numSum % 10 == 0;
203205
}
204206

205207
/**
206-
*
207208
* Checks if Credit Card Format is a supported one
208209
* and builds new pattern format in case user has
209210
* a mixed match search (mastercard|visa)
210211
*
211-
* @access private
212-
* @return boolean
212+
* @return bool
213213
*/
214214
protected function checkType()
215215
{
216-
217216
if (is_scalar($this->format)) {
218217
return array_key_exists($this->format, $this->patterns);
219-
} else if (is_array($this->format)) {
220-
$pattern = array();
218+
} elseif (is_array($this->format)) {
219+
$pattern = [];
221220
foreach ($this->format as $f) {
222-
if (!array_key_exists($f, $this->patterns)) return false;
221+
if (!array_key_exists($f, $this->patterns)) {
222+
return false;
223+
}
223224
$pattern[] = substr($this->patterns[$f], 2, strlen($this->patterns[$f]) - 4);
224225
}
225226
$this->format = 'custom';
226-
$this->patterns[$this->format] = '/^(' . join('|', $pattern) . ')$/';
227+
$this->patterns[$this->format] = '/^(' . implode('|', $pattern) . ')$/';
228+
227229
return true;
228230
}
229-
return false;
230231

232+
return false;
231233
}
232234
}

PhoneValidator.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace yii2mod\validators;
44

5+
use libphonenumber\NumberParseException;
6+
use libphonenumber\PhoneNumberFormat;
7+
use libphonenumber\PhoneNumberUtil;
58
use Yii;
69
use yii\validators\Validator;
7-
use libphonenumber\PhoneNumberUtil;
8-
use libphonenumber\PhoneNumberFormat;
9-
use libphonenumber\NumberParseException;
1010

1111
/**
1212
* Phone validator class that validates phone numbers for given
@@ -16,23 +16,23 @@
1616
class PhoneValidator extends Validator
1717
{
1818
/**
19-
* @var bool $strict If country is not set or selected adds error
19+
* @var bool If country is not set or selected adds error
2020
*/
2121
public $strict = true;
2222
/**
23-
* @var string $country The country is fixed
23+
* @var string The country is fixed
2424
*/
2525
public $country;
2626
/**
27-
* @var string $countryAttribute The country attribute of model
27+
* @var string The country attribute of model
2828
*/
2929
public $countryAttribute;
3030
/**
31-
* @var string $countryCodeAttribute
31+
* @var string
3232
*/
3333
public $countryCodeAttribute;
3434
/**
35-
* @var bool $format If phone number is valid formats value with international phone number
35+
* @var bool If phone number is valid formats value with international phone number
3636
*/
3737
public $format = true;
3838
/**
@@ -50,8 +50,10 @@ class PhoneValidator extends Validator
5050

5151
/**
5252
* Validate attribute
53+
*
5354
* @param \yii\base\Model $model
5455
* @param string $attribute
56+
*
5557
* @return bool|void
5658
*/
5759
public function validateAttribute($model, $attribute)
@@ -67,6 +69,7 @@ public function validateAttribute($model, $attribute)
6769
// if empty country and used strict mode
6870
if (empty($country) && $this->strict) {
6971
$this->addError($model, $attribute, Yii::t('app', $this->strictModeMessage));
72+
7073
return false;
7174
}
7275
if (empty($country)) {
@@ -79,12 +82,13 @@ public function validateAttribute($model, $attribute)
7982
if ($this->format) {
8083
$model->$attribute = $phoneUtil->format($numberProto, PhoneNumberFormat::INTERNATIONAL);
8184
}
85+
8286
return true;
8387
} else {
8488
$this->addError($model, $attribute, Yii::t('app', $this->message));
89+
8590
return false;
8691
}
87-
8892
} catch (NumberParseException $e) {
8993
$this->addError($model, $attribute, Yii::t('app', $this->numberParseExceptionMessage));
9094
}

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
22
"name": "yii2mod/yii2-validators",
3-
"description": "Yii2 validators",
3+
"description": "Collection of useful validators for Yii Framework 2.0",
44
"type": "yii2-extension",
5-
"keywords": ["yii2", "extension"],
5+
"keywords": [
6+
"yii2",
7+
"yii2 validators",
8+
"yii2 card validator",
9+
"yii2 phone validator"
10+
],
611
"license": "MIT",
712
"authors": [
813
{
@@ -18,6 +23,9 @@
1823
"yiisoft/yii2": "*",
1924
"giggsey/libphonenumber-for-php": "~7.0"
2025
},
26+
"require-dev": {
27+
"friendsofphp/php-cs-fixer": "~1.7"
28+
},
2129
"autoload": {
2230
"psr-4": {
2331
"yii2mod\\validators\\": ""

0 commit comments

Comments
 (0)