Skip to content

Commit

Permalink
Improve Validator, passing property name now, and use property name i…
Browse files Browse the repository at this point in the history
…n exception message
  • Loading branch information
huangzhhui committed Mar 7, 2018
1 parent e2786cc commit 7637d12
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 227 deletions.
72 changes: 58 additions & 14 deletions src/Bean/Annotation/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*/
Expand All @@ -50,7 +44,7 @@ class Strings
private $default = null;

/**
* Integer constructor.
* Strings constructor.
*
* @param array $values
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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;
}
}
16 changes: 5 additions & 11 deletions src/Bean/Annotation/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 39 additions & 37 deletions src/Bean/Collector/ValidatorCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bean/Parser/StringsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class StringsParser extends AbstractParser
public function parser(
string $className,
$objectAnnotation = null,
string $propertyName = "",
string $methodName = "",
string $propertyName = '',
string $methodName = '',
$propertyValue = null
) {
ValidatorCollector::collect($className, $objectAnnotation, $propertyName, $methodName, $propertyValue);
Expand Down
Loading

0 comments on commit 7637d12

Please sign in to comment.