Skip to content

Commit 423a04b

Browse files
committed
convert php 7 code to php 5 ...
1 parent 64638a5 commit 423a04b

13 files changed

+150
-431
lines changed

src/AbstractValidation.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Created by PhpStorm.
45
* User: inhere
@@ -23,9 +24,8 @@ abstract class AbstractValidation implements ValidationInterface
2324
{
2425
use ValidationTrait {
2526
//set as traitSet;
26-
get as traitGet;// Methods to define an alias, can be used in the current class.
27+
get as traitGet;
2728
}
28-
2929
/**
3030
* @var array
3131
*/
@@ -43,11 +43,7 @@ abstract class AbstractValidation implements ValidationInterface
4343
public function __construct(array $data = [], array $rules = [], array $translates = [], $scene = '', $startValidate = false)
4444
{
4545
$this->data = $data;
46-
$this
47-
->setRules($rules)
48-
->setScene($scene)
49-
->setTranslates($translates);
50-
46+
$this->setRules($rules)->setScene($scene)->setTranslates($translates);
5147
if ($startValidate) {
5248
$this->validate();
5349
}
@@ -67,5 +63,4 @@ public static function make(array $data, array $rules = [], array $translates =
6763
{
6864
return new static($data, $rules, $translates, $scene, $startValidate);
6965
}
70-
71-
}
66+
}

src/FieldValidation.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Created by PhpStorm.
45
* User: inhere
@@ -24,12 +25,7 @@ class FieldValidation extends AbstractValidation
2425
*/
2526
public function rules()
2627
{
27-
return [
28-
// ['field', 'required|string:5,10|...', ...],
29-
// ['field0', ['required', 'string:5,10'], ...],
30-
// ['field1', 'rule1|rule2|...', ...],
31-
// ['field2', 'rule1|rule3|...', ...],
32-
];
28+
return [];
3329
}
3430

3531
/**
@@ -39,46 +35,39 @@ public function rules()
3935
protected function collectRules()
4036
{
4137
$scene = $this->scene;
42-
4338
// 循环规则, 搜集当前场景可用的规则
4439
foreach ($this->getRules() as $rule) {
4540
// check field
4641
if (!isset($rule[0]) || !$rule[0]) {
4742
throw new \InvalidArgumentException('Please setting the field(string) to wait validate! position: rule[0].');
4843
}
49-
5044
// check validators
5145
if (!isset($rule[1]) || !$rule[1]) {
5246
throw new \InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1].');
5347
}
54-
5548
// an rule for special scene.
5649
if (!empty($rule['on'])) {
5750
$sceneList = \is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
58-
5951
if ($scene && !\in_array($scene, $sceneList, true)) {
6052
continue;
6153
}
62-
6354
unset($rule['on']);
6455
$this->_usedRules[] = $rule;
6556
}
66-
6757
$field = array_shift($rule);
68-
6958
if (\is_object($rule[0])) {
70-
yield $field => $rule;
59+
(yield $field => $rule);
7160
} else {
7261
// 'required|string:5,10;' OR 'required|in:5,10'
7362
$rules = \is_array($rule[0]) ? $rule[0] : array_map('trim', explode('|', $rule[0]));
74-
7563
foreach ($rules as $aRule) {
7664
$rule = $this->parseRule($aRule, $rule);
77-
78-
yield $field => $rule;
65+
(yield $field => $rule);
7966
}
8067
}
8168
}
69+
70+
yield [];
8271
}
8372

8473
/**
@@ -89,23 +78,20 @@ protected function collectRules()
8978
protected function parseRule($rule, $row)
9079
{
9180
$rule = trim($rule, ': ');
92-
9381
if (false === strpos($rule, ':')) {
9482
$row[0] = $rule;
83+
9584
return $row;
9685
}
97-
9886
list($name, $args) = explode(':', $rule, 2);
9987
$args = trim($args, ', ');
10088
$row[0] = $name;
101-
10289
switch ($name) {
10390
case 'in':
10491
case 'enum':
10592
case 'ontIn':
10693
$row[] = array_map('trim', explode(',', $args));
10794
break;
108-
10995
case 'size':
11096
case 'range':
11197
case 'string':
@@ -124,4 +110,4 @@ protected function parseRule($rule, $row)
124110

125111
return $row;
126112
}
127-
}
113+
}

src/Filter/FilterList.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @date 2015.08.05 sanitize
45
* 过滤器(strainer/filter): 过滤数据,去除不合要求的数据,返回过滤后的数据(始终返回字符串, 全部不符合返回空字符串)
@@ -56,14 +57,12 @@ public static function abs($var)
5657
public static function float($var, $flags = FILTER_FLAG_ALLOW_FRACTION)
5758
{
5859
$settings = [];
59-
6060
if ((int)$flags !== 0) {
6161
$settings['flags'] = (int)$flags;
6262
}
63-
6463
$ret = filter_var($var, FILTER_SANITIZE_NUMBER_FLOAT, $settings);
6564

66-
return strpos($ret, '.') ? (float)$ret : $ret;
65+
return strpos($ret, '.') ? (double)$ret : $ret;
6766
}
6867

6968
/**
@@ -81,7 +80,6 @@ public static function float($var, $flags = FILTER_FLAG_ALLOW_FRACTION)
8180
public static function string($var, $flags = 0)
8281
{
8382
$settings = [];
84-
8583
if ((int)$flags !== 0) {
8684
$settings['flags'] = (int)$flags;
8785
}
@@ -176,7 +174,6 @@ public static function strToTime($var)
176174
public static function encoded($var, $flags = 0)
177175
{
178176
$settings = [];
179-
180177
if ((int)$flags !== 0) {
181178
$settings['flags'] = (int)$flags;
182179
}
@@ -206,7 +203,6 @@ public static function quotes($var)
206203
public static function specialChars($var, $flags = 0)
207204
{
208205
$settings = [];
209-
210206
if ((int)$flags !== 0) {
211207
$settings['flags'] = (int)$flags;
212208
}
@@ -223,7 +219,6 @@ public static function specialChars($var, $flags = 0)
223219
public static function fullSpecialChars($var, $flags = 0)
224220
{
225221
$settings = [];
226-
227222
if ((int)$flags !== 0) {
228223
$settings['flags'] = (int)$flags;
229224
}
@@ -285,7 +280,6 @@ public static function email($var)
285280
public static function unsafeRaw($string, $flags = 0)
286281
{
287282
$settings = [];
288-
289283
if ((int)$flags !== 0) {
290284
$settings['flags'] = (int)$flags;
291285
}
@@ -303,4 +297,4 @@ public static function callback($val, $callback)
303297
{
304298
return filter_var($val, FILTER_CALLBACK, ['options' => $callback]);
305299
}
306-
}
300+
}

src/Filter/Filtration.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Created by PhpStorm.
45
* User: Inhere
@@ -14,7 +15,6 @@
1415
* Class Filtration
1516
* @package Inhere\Validate\Filter
1617
* usage:
17-
*
1818
* $data = Filtration::make($_POST, [
1919
* ['tagId,userId,freeTime', 'int'],
2020
* ['name', 'string|trim', 'default' => 'tom'],
@@ -24,12 +24,10 @@
2424
class Filtration
2525
{
2626
use DataFiltersTrait;
27-
2827
/**
2928
* @var array
3029
*/
3130
private $_data;
32-
3331
/**
3432
* the rules is by setRules()
3533
* @var array
@@ -90,26 +88,21 @@ public function applyRules(array $rules = [], array $data = [])
9088
$data = $data ?: $this->_data;
9189
$rules = $rules ?: $this->_rules;
9290
$filtered = [];
93-
9491
foreach ($rules as $rule) {
9592
if (!isset($rule[0], $rule[1])) {
9693
continue;
9794
}
98-
99-
if (!$fields = $rule[0]) {
95+
if (!($fields = $rule[0])) {
10096
continue;
10197
}
102-
10398
$fields = \is_string($fields) ? array_map('trim', explode(',', $fields)) : (array)$fields;
104-
10599
foreach ($fields as $field) {
106100
if (!isset($data[$field])) {
107-
$filtered[$field] = $rule['default'] ?? null;
101+
$filtered[$field] = isset($rule['default']) ? $rule['default'] : null;
108102
} else {
109103
$filtered[$field] = $this->valueFiltering($data[$field], $rule[1]);
110104
}
111105
}
112-
113106
}
114107

115108
return $filtered;
@@ -140,9 +133,7 @@ public function get($field, $filters = null, $default = null)
140133
if (!isset($this->_data[$field])) {
141134
return $default;
142135
}
143-
144136
$value = $this->_data[$field];
145-
146137
if (!$filters) {
147138
return $value;
148139
}
@@ -157,7 +148,6 @@ public function get($field, $filters = null, $default = null)
157148
public function reset($clearFilters = false)
158149
{
159150
$this->_data = $this->_rules = [];
160-
161151
if ($clearFilters) {
162152
self::clearFilters();
163153
}
@@ -168,15 +158,15 @@ public function reset($clearFilters = false)
168158
/**
169159
* @return array
170160
*/
171-
public function getData(): array
161+
public function getData()
172162
{
173163
return $this->_data;
174164
}
175165

176166
/**
177167
* @return array
178168
*/
179-
public function all(): array
169+
public function all()
180170
{
181171
return $this->_data;
182172
}
@@ -195,8 +185,8 @@ public function setRules(array $rules)
195185
/**
196186
* @return array
197187
*/
198-
public function getRules(): array
188+
public function getRules()
199189
{
200190
return $this->_rules;
201191
}
202-
}
192+
}

src/RuleValidation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Created by PhpStorm.
45
* User: inhere
@@ -22,5 +23,4 @@
2223
*/
2324
class RuleValidation extends Validation
2425
{
25-
26-
}
26+
}

0 commit comments

Comments
 (0)