-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Validator, passing property name now, and use property name i…
…n exception message
- Loading branch information
1 parent
e2786cc
commit 7637d12
Showing
13 changed files
with
215 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,10 @@ | |
namespace Swoft\Bean\Annotation; | ||
|
||
/** | ||
* 字符串验证器 | ||
* String validator | ||
* | ||
* @Annotation | ||
* @Target("METHOD") | ||
* | ||
* @uses Strings | ||
* @version 2017年11月13日 | ||
* @author stelin <[email protected]> | ||
* @copyright Copyright 2010-2016 swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
*/ | ||
class Strings | ||
{ | ||
|
@@ -22,21 +16,21 @@ class Strings | |
private $from = ValidatorFrom::POST; | ||
|
||
/** | ||
* 字段名称 | ||
* Parameter name | ||
* | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* 最小值 | ||
* Min value | ||
* | ||
* @var int | ||
*/ | ||
private $min = PHP_INT_MIN; | ||
|
||
/** | ||
* 最小值 | ||
* Max value | ||
* | ||
* @var int | ||
*/ | ||
|
@@ -50,7 +44,7 @@ class Strings | |
private $default = null; | ||
|
||
/** | ||
* Integer constructor. | ||
* Strings constructor. | ||
* | ||
* @param array $values | ||
*/ | ||
|
@@ -73,6 +67,24 @@ public function __construct(array $values) | |
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFrom(): string | ||
{ | ||
return $this->from; | ||
} | ||
|
||
/** | ||
* @param string $from | ||
* @return Strings | ||
*/ | ||
public function setFrom(string $from): Strings | ||
{ | ||
$this->from = $from; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
|
@@ -81,6 +93,16 @@ public function getName(): string | |
return $this->name; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return Strings | ||
*/ | ||
public function setName(string $name): Strings | ||
{ | ||
$this->name = $name; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
|
@@ -89,6 +111,16 @@ public function getMin(): int | |
return $this->min; | ||
} | ||
|
||
/** | ||
* @param int $min | ||
* @return Strings | ||
*/ | ||
public function setMin(int $min): Strings | ||
{ | ||
$this->min = $min; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
|
@@ -97,6 +129,16 @@ public function getMax(): int | |
return $this->max; | ||
} | ||
|
||
/** | ||
* @param int $max | ||
* @return Strings | ||
*/ | ||
public function setMax(int $max): Strings | ||
{ | ||
$this->max = $max; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
|
@@ -106,10 +148,12 @@ public function getDefault() | |
} | ||
|
||
/** | ||
* @return string | ||
* @param null|string $default | ||
* @return Strings | ||
*/ | ||
public function getFrom(): string | ||
public function setDefault($default): Strings | ||
{ | ||
return $this->from; | ||
$this->default = $default; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,36 +3,30 @@ | |
namespace Swoft\Bean\Annotation; | ||
|
||
/** | ||
* the annotation of value | ||
* Value annotation | ||
* | ||
* 1. 注入值 | ||
* 2. 注入property配置文件值 | ||
* 3. 注入env环境变量 | ||
* | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @uses Value | ||
* @version 2017年11月14日 | ||
* @author stelin <[email protected]> | ||
* @copyright Copyright 2010-2016 swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
*/ | ||
class Value | ||
{ | ||
/** | ||
* the key of properties | ||
* Property name | ||
* | ||
* @var string | ||
*/ | ||
private $name = ""; | ||
private $name = ''; | ||
|
||
/** | ||
* the key of env config | ||
* Env name | ||
* | ||
* @var string | ||
*/ | ||
private $env = ""; | ||
private $env = ''; | ||
|
||
/** | ||
* Value constructor. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,9 @@ | |
use Swoft\Validator\StringsValidator; | ||
|
||
/** | ||
* the collector of validator | ||
* Class ValidatorCollector | ||
* | ||
* @uses ValidatorCollector | ||
* @version 2018年01月08日 | ||
* @author stelin <[email protected]> | ||
* @copyright Copyright 2010-2016 swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
* @package Swoft\Bean\Collector | ||
*/ | ||
class ValidatorCollector implements CollectorInterface | ||
{ | ||
|
@@ -36,14 +32,20 @@ class ValidatorCollector implements CollectorInterface | |
* @param string $propertyName | ||
* @param string $methodName | ||
* @param null $propertyValue | ||
* @return mixed|void | ||
*/ | ||
public static function collect(string $className, $objectAnnotation = null, string $propertyName = "", string $methodName = "", $propertyValue = null) | ||
{ | ||
public static function collect( | ||
string $className, | ||
$objectAnnotation = null, | ||
string $propertyName = '', | ||
string $methodName = '', | ||
$propertyValue = null | ||
) { | ||
if ($objectAnnotation instanceof Strings) { | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
$default = $objectAnnotation->getDefault(); | ||
|
||
$params = [$min, $max, $default]; | ||
|
@@ -52,28 +54,28 @@ public static function collect(string $className, $objectAnnotation = null, stri | |
'params' => $params, | ||
]; | ||
|
||
return ; | ||
return; | ||
} | ||
|
||
if($objectAnnotation instanceof Floats){ | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
if ($objectAnnotation instanceof Floats) { | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
$default = $objectAnnotation->getDefault(); | ||
|
||
$params = [$min, $max, $default]; | ||
self::$validator[$className][$methodName]['validator'][$from][$name] = [ | ||
'validator' => FloatsValidator::class, | ||
'params' => $params, | ||
]; | ||
return ; | ||
return; | ||
} | ||
if($objectAnnotation instanceof Number){ | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
if ($objectAnnotation instanceof Number) { | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
$default = $objectAnnotation->getDefault(); | ||
|
||
$params = [$min, $max, $default]; | ||
|
@@ -82,43 +84,43 @@ public static function collect(string $className, $objectAnnotation = null, stri | |
'validator' => NumberValidator::class, | ||
'params' => $params, | ||
]; | ||
return ; | ||
return; | ||
} | ||
|
||
if($objectAnnotation instanceof Integer){ | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
if ($objectAnnotation instanceof Integer) { | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$min = $objectAnnotation->getMin(); | ||
$max = $objectAnnotation->getMax(); | ||
$default = $objectAnnotation->getDefault(); | ||
|
||
$params = [$min, $max, $default]; | ||
self::$validator[$className][$methodName]['validator'][$from][$name] = [ | ||
'validator' => IntegerValidator::class, | ||
'params' => $params, | ||
]; | ||
return ; | ||
return; | ||
} | ||
|
||
if($objectAnnotation instanceof Enum){ | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$values = $objectAnnotation->getValues(); | ||
if ($objectAnnotation instanceof Enum) { | ||
$from = $objectAnnotation->getFrom(); | ||
$name = $objectAnnotation->getName(); | ||
$values = $objectAnnotation->getValues(); | ||
$default = $objectAnnotation->getDefault(); | ||
|
||
$params = [$values, $default]; | ||
self::$validator[$className][$methodName]['validator'][$from][$name] = [ | ||
'validator' => EnumValidator::class, | ||
'params' => $params, | ||
]; | ||
return ; | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getCollector() | ||
public static function getCollector(): array | ||
{ | ||
return self::$validator; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.