Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interface inject #442

Merged
merged 7 commits into from Jun 25, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Update validator

  • Loading branch information...
stelin committed Jun 24, 2019
commit d959a4f5facfc24070fbde547f0557213e5184ee

Large diffs are not rendered by default.

@@ -0,0 +1,22 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Contract;
/**
* Class RuleInterface
*
* @since 2.0
*/
interface RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed $default
*
* @return array
*/
public function validate(array $data, string $propertyName, $item, $default = null): array;
}
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Rule;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\Email;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\Helper\ValidatorHelper;
/**
* Class EmailRule
*
* @since 2.0
*
* @Bean(Email::class)
*/
class EmailRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed|null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
$value = $data[$propertyName];
if (ValidatorHelper::validateEmail($value)) {
return $data;
}
/* @var Email $item */
$message = $item->getMessage();
$message = (empty($message)) ? sprintf('%s must be a email', $propertyName) : $message;
throw new ValidatorException($message);
}
}
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Rule;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\Enum;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\Helper\ValidatorHelper;
/**
* Class EnumRule
*
* @since 2.0
*
* @Bean(Enum::class)
*/
class EnumRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed|null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
/* @var Enum $item */
$values = $item->getValues();
$value = $data[$propertyName];
if (ValidatorHelper::validateEnum($value, $values)) {
return $data;
}
$message = $item->getMessage();
$message = (empty($message)) ? sprintf('%s is invalid enum', $propertyName) : $message;
throw new ValidatorException($message);
}
}
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Rule;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\Ip;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\Helper\ValidatorHelper;
/**
* Class IpRule
*
* @since 2.0
*
* @Bean(Ip::class)
*/
class IpRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed|null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
$value = $data[$propertyName];
if (ValidatorHelper::validateIp($value)) {
return $data;
}
/* @var Ip $item */
$message = $item->getMessage();
$message = (empty($message)) ? sprintf('%s is invalid ip', $propertyName) : $message;
throw new ValidatorException($message);
}
}
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Rule;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\IsArray;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
/**
* Class IsArrayRule
*
* @since 2.0
*
* @Bean(IsArray::class)
*/
class IsArrayRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed|null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
/* @var IsArray $item */
$message = $item->getMessage();
if (!isset($data[$propertyName]) && $default !== null) {
$data[$propertyName] = (array)$default;
return $data;
}
if (!isset($data[$propertyName]) && $default === null) {
$message = (empty($message)) ? sprintf('%s must exist!', $propertyName) : $message;
throw new ValidatorException($message);
}
$value = $data[$propertyName];
if (is_array($value)) {
return $data;
}
$message = (empty($message)) ? sprintf('%s must bool!', $propertyName) : $message;
throw new ValidatorException($message);
}
}
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Rule;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\IsBool;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
/**
* Class IsBoolRule
*
* @since 2.0
*
* @Bean(IsBool::class)
*/
class IsBoolRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed|null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
/* @var IsBool $item */
$message = $item->getMessage();
if (!isset($data[$propertyName]) && $default !== null) {
$data[$propertyName] = (bool)$default;
return $data;
}
if (!isset($data[$propertyName]) && $default === null) {
$message = (empty($message)) ? sprintf('%s must exist!', $propertyName) : $message;
throw new ValidatorException($message);
}
$value = $data[$propertyName];
if ($value == 'true' || $value == 'false' || is_bool($value)) {
$data[$propertyName] = (bool)$value;
return $data;
}
$message = (empty($message)) ? sprintf('%s must bool!', $propertyName) : $message;
throw new ValidatorException($message);
}
}
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Rule;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\IsFloat;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
/**
* Class IsFloatRule
*
* @since 2.0
*
* @Bean(IsFloat::class)
*/
class IsFloatRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed|null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
/* @var IsFloat $item */
$message = $item->getMessage();
if (!isset($data[$propertyName]) && $default !== null) {
$data[$propertyName] = (float)$default;
return $data;
}
if (!isset($data[$propertyName]) && $default === null) {
$message = (empty($message)) ? sprintf('%s must exist!', $propertyName) : $message;
throw new ValidatorException($message);
}
$value = $data[$propertyName];
$value = filter_var($value, FILTER_VALIDATE_FLOAT);
if ($value !== false) {
$data[$propertyName] = $value;
return $data;
}
$message = (empty($message)) ? sprintf('%s must float!', $propertyName) : $message;
throw new ValidatorException($message);
}
}
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);
namespace Swoft\Validator\Rule;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Annotation\Mapping\IsInt;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
/**
* Class IsIntRule
*
* @since 2.0
*
* @Bean(IsInt::class)
*/
class IsIntRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param mixed|null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
/* @var IsInt $item */
$message = $item->getMessage();
if (!isset($data[$propertyName]) && $default !== null) {
$data[$propertyName] = (int)$default;
return $data;
}
if (!isset($data[$propertyName]) && $default === null) {
$message = (empty($message)) ? sprintf('%s must exist!', $propertyName) : $message;
throw new ValidatorException($message);
}
$value = $data[$propertyName];
$value = filter_var($value, FILTER_VALIDATE_INT);
if ($value !== false) {
$data[$propertyName] = $value;
return $data;
}
$message = (empty($message)) ? sprintf('%s must int!', $propertyName) : $message;
throw new ValidatorException($message);
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.