Skip to content

Commit 1d041be

Browse files
committed
fix some error. add more unit tests
1 parent 6439140 commit 1d041be

File tree

9 files changed

+234
-95
lines changed

9 files changed

+234
-95
lines changed

src/Filter/Filters.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public static function uppercase($str): string
254254
}
255255

256256
/**
257-
* @param string $str
257+
* @param string|mixed $str
258258
* @return string
259259
*/
260260
public static function ucfirst($str): string
@@ -267,7 +267,7 @@ public static function ucfirst($str): string
267267
}
268268

269269
/**
270-
* @param string $str
270+
* @param string|mixed $str
271271
* @return string
272272
*/
273273
public static function ucwords($str): string
@@ -284,8 +284,8 @@ public static function ucwords($str): string
284284
}
285285

286286
/**
287-
* string to snakeCase
288-
* @param string $val
287+
* string to snake case
288+
* @param string|mixed $val
289289
* @param string $sep
290290
* @return string
291291
*/
@@ -315,7 +315,7 @@ public static function snakeCase($val, string $sep = '_'): string
315315

316316
/**
317317
* string to camelcase
318-
* @param string $val
318+
* @param string|mixed $val
319319
* @param bool $ucFirst
320320
* @return string
321321
*/
@@ -587,11 +587,7 @@ public static function email($val): string
587587
*/
588588
public static function unsafeRaw($string, $flags = 0)
589589
{
590-
$settings = [];
591-
592-
if ((int)$flags !== 0) {
593-
$settings['flags'] = (int)$flags;
594-
}
590+
$settings = (int)$flags !== 0 ? ['flags' => (int)$flags] : [];
595591

596592
return \filter_var($string, \FILTER_UNSAFE_RAW, $settings);
597593
}

src/Traits/ErrorMessageTrait.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2017-03-17
6-
* Time: 11:26
7-
*/
82

93
namespace Inhere\Validate\Traits;
104

115
use Inhere\Validate\Helper;
12-
use Inhere\Validate\Validator\Messages;
6+
use Inhere\Validate\Validator\GlobalMessage;
137
use Inhere\Validate\Validators;
148

159
/**
@@ -76,7 +70,7 @@ protected function shouldStop(): bool
7670
}
7771

7872
/**
79-
* 是否有错误
73+
* Is there an error?
8074
* @return boolean
8175
*/
8276
public function hasError(): bool
@@ -94,6 +88,7 @@ public function isFail(): bool
9488

9589
/**
9690
* @return bool
91+
* @deprecated will delete, please use isFail() instead
9792
*/
9893
public function fail(): bool
9994
{
@@ -110,6 +105,7 @@ public function failed(): bool
110105

111106
/**
112107
* @return bool
108+
* @deprecated will delete, please use isOk() or isPassed() instead
113109
*/
114110
public function ok(): bool
115111
{
@@ -125,7 +121,7 @@ public function isOk(): bool
125121
}
126122

127123
/**
128-
* @deprecated will delete
124+
* @deprecated will delete, please use isOk() or isPassed() instead
129125
* @return bool
130126
*/
131127
public function passed(): bool
@@ -169,10 +165,10 @@ public function addError(string $field, string $msg)
169165
}
170166

171167
/**
172-
* @param string|null $field Only get errors of the field.
168+
* @param string $field Only get errors of the field.
173169
* @return array
174170
*/
175-
public function getErrors(string $field = null): array
171+
public function getErrors(string $field = ''): array
176172
{
177173
if ($field) {
178174
$errors = [];
@@ -197,27 +193,32 @@ public function clearErrors()
197193
}
198194

199195
/**
200-
* 得到第一个错误信息
201-
* @author inhere
196+
* Get the first error message
202197
* @param bool $onlyMsg
203198
* @return array|string
204199
*/
205200
public function firstError(bool $onlyMsg = true)
206201
{
207-
$errors = $this->_errors;
208-
$first = \array_shift($errors);
202+
if (!$errors = $this->_errors) {
203+
return $onlyMsg ? '' : [];
204+
}
205+
206+
$first = \array_shift($errors);
209207
return $onlyMsg ? $first['msg'] : $first;
210208
}
211209

212210
/**
213-
* 得到最后一个错误信息
211+
* Get the last error message
214212
* @param bool $onlyMsg
215213
* @return array|string
216214
*/
217215
public function lastError(bool $onlyMsg = true)
218216
{
219-
$ers = $this->_errors;
220-
$last = \array_pop($ers);
217+
if (!$errors = $this->_errors) {
218+
return $onlyMsg ? '' : [];
219+
}
220+
221+
$last = \array_pop($errors);
221222
return $onlyMsg ? $last['msg'] : $last;
222223
}
223224

@@ -294,19 +295,19 @@ public function getMessage($validator, string $field, array $args = [], $message
294295

295296
// get message from built in dict.
296297
if (!$message) {
297-
$message = $this->findMessage($field, $rawName) ?: Messages::getDefault();
298+
$message = $this->findMessage($field, $rawName) ?: GlobalMessage::getDefault();
298299
// is array. It's defined multi error messages
299300
} elseif (\is_array($message)) {
300301
$message = $message[$rawName] ?? $this->findMessage($field, $rawName);
301302

302303
if (!$message) { // use default
303-
return \strtr(Messages::getDefault(), $params);
304+
return \strtr(GlobalMessage::getDefault(), $params);
304305
}
305306
} else {
306307
$message = (string)$message;
307308
}
308309

309-
/** @see Messages::$messages['size'] */
310+
/** @see GlobalMessage::$messages['size'] */
310311
if (\is_array($message)) {
311312
$msgKey = \count($params) - 1;
312313
$message = $message[$msgKey] ?? $message[0];
@@ -345,7 +346,7 @@ protected function findMessage(string $field, string $rawName)
345346
} elseif (isset($this->_messages[$realName])) {
346347
$message = $this->_messages[$realName];
347348
} else { // get from default
348-
$message = Messages::get($realName);
349+
$message = GlobalMessage::get($realName);
349350
}
350351

351352
return $message;
@@ -421,7 +422,7 @@ public function isPrettifyName(): bool
421422
/**
422423
* @param bool $prettifyName
423424
*/
424-
public function setPrettifyName(bool $prettifyName): void
425+
public function setPrettifyName(bool $prettifyName = true): void
425426
{
426427
$this->_prettifyName = $prettifyName;
427428
}

src/ValidationInterface.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2015-08-11
6-
* Time: 10:23
3+
* @author inhere
4+
* @date: 2015-08-11
75
*/
86

97
namespace Inhere\Validate;
@@ -62,21 +60,20 @@ public function ok(): bool;
6260
public function isPassed(): bool;
6361

6462
/**
63+
* @param string $field
6564
* @return array
6665
*/
67-
public function getErrors(): array;
66+
public function getErrors(string $field = ''): array;
6867

6968
/**
70-
* 得到第一个错误信息
71-
* @author inhere
69+
* Get the first error message
7270
* @param bool $onlyMsg Only return message string.
7371
* @return array|string
7472
*/
7573
public function firstError(bool $onlyMsg = true);
7674

7775
/**
78-
* 得到最后一个错误信息
79-
* @author inhere
76+
* Get the last error message
8077
* @param bool $onlyMsg
8178
* @return array|string
8279
*/

src/ValidationTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function validate(array $onlyChecked = [], bool $stopOnError = null)
171171
return $this;
172172
}
173173

174-
$this->resetValidation(true);
174+
$this->resetValidation();
175175
$this->setStopOnError($stopOnError);
176176
$this->prepareValidation();
177177

@@ -406,7 +406,7 @@ protected function collectSafeValue(string $field, $value)
406406
/**
407407
* @param bool|false $clearErrors
408408
*/
409-
protected function resetValidation(bool $clearErrors = false)
409+
public function resetValidation(bool $clearErrors = true)
410410
{
411411
$this->_validated = false;
412412
$this->_safeData = $this->_usedRules = [];

src/Validator/Messages.php renamed to src/Validator/GlobalMessage.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: inhere
5-
* Date: 2019-01-20
6-
* Time: 00:54
7-
*/
82

93
namespace Inhere\Validate\Validator;
104

115
/**
12-
* Class Messages
13-
* @package Inhere\Validate
6+
* Class GlobalMessage
7+
* - global error message storage
148
*/
15-
final class Messages
9+
final class GlobalMessage
1610
{
1711
/**
1812
* Default error messages

test/Filter/FiltersTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ public function testBool()
2525
'off' => false,
2626
'false' => false,
2727
'False' => false,
28+
null => false,
2829
];
2930

3031
foreach ($samples as $sample => $expected) {
3132
$this->assertSame($expected, Filters::bool($sample));
3233
}
34+
35+
$this->assertFalse(Filters::bool([]));
3336
}
3437

3538
public function testAliases()
@@ -119,17 +122,25 @@ public function testChangeCase()
119122

120123
// ucfirst
121124
$this->assertSame('Abc', Filters::ucfirst('abc'));
125+
$this->assertSame('', Filters::ucfirst(''));
126+
$this->assertSame('', Filters::ucfirst([]));
122127

123128
// ucwords
124129
$this->assertSame('Hello World', Filters::ucwords('hello world'));
130+
$this->assertSame('', Filters::ucwords(''));
131+
$this->assertSame('', Filters::ucwords([]));
125132

126133
// snake case
127134
$this->assertSame('hello_world', Filters::snake('HelloWorld'));
128135
$this->assertSame('hello-world', Filters::snake('HelloWorld', '-'));
136+
$this->assertSame('', Filters::snake(''));
137+
$this->assertSame('', Filters::snake([]));
129138

130139
// camel case
131140
$this->assertSame('helloWorld', Filters::camel('hello_world'));
132141
$this->assertSame('HelloWorld', Filters::camel('hello_world', true));
142+
$this->assertSame('', Filters::camel(''));
143+
$this->assertSame('', Filters::camel([]));
133144
}
134145

135146
public function testTime()
@@ -153,5 +164,45 @@ public function testStr2list()
153164
$this->assertSame($expected, Filters::str2array($sample));
154165
$this->assertSame($expected, Filters::explode($sample));
155166
}
167+
168+
$this->assertSame([], Filters::explode(''));
169+
$this->assertSame([], Filters::str2list(' , '));
170+
}
171+
172+
public function testUnique()
173+
{
174+
$this->assertSame([1], Filters::unique([1, 1]));
175+
$this->assertSame(['a', 'b'], Filters::unique(['a', 'b', 'a']));
176+
$this->assertSame(['a', 2 => 'b'], Filters::unique(['a', 'a', 'b', 'a']));
177+
}
178+
179+
public function testEncodeOrClearTag()
180+
{
181+
// clearTags
182+
$samples = [
183+
'' => '',
184+
'<p>text</p>' => 'text',
185+
'<p>text' => 'text',
186+
'<p><a>text</a></p>' => 'text',
187+
];
188+
foreach ($samples as $sample => $expected) {
189+
$this->assertSame($expected, Filters::clearTags($sample));
190+
}
191+
192+
$this->assertSame('<a>text</a>', Filters::clearTags('<p><a>text</a></p>', '<a>'));
193+
194+
// encoded
195+
$this->assertSame('abc.com%3Fa%3D7%2B9', Filters::encoded('abc.com?a=7+9'));
196+
$this->assertSame('abc.com%3Fa%3D7%2B9%26b%3D', Filters::encoded('abc.com?a=7+9&b=你', \FILTER_FLAG_STRIP_HIGH));
197+
$this->assertSame('abc.com%3Fa%3D7%2B9%26b%3D%E4%BD%A0', Filters::encoded('abc.com?a=7+9&b=你'));
198+
$this->assertSame('abc.com%3Fa%3D7%2B9%26b%3D%E4%BD%A0', Filters::encoded('abc.com?a=7+9&b=你', \FILTER_FLAG_ENCODE_LOW));
199+
$this->assertSame('abc.com%3Fa%3D7%2B9%26b%3D%E4%BD%A0', Filters::encoded('abc.com?a=7+9&b=你', \FILTER_FLAG_ENCODE_HIGH));
200+
201+
$this->assertSame('', Filters::url(''));
202+
$this->assertSame('abc.com?a=7+9', Filters::url('abc.com?a=7+9'));
203+
$this->assertSame('abc.com?a=7+9&b=', Filters::url('abc.com?a=7+9&b=你'));
204+
205+
$this->assertSame('', Filters::email(''));
206+
$this->assertSame('abc@email.com', Filters::email('abc@email.com'));
156207
}
157208
}

0 commit comments

Comments
 (0)