@@ -10,15 +10,15 @@ |
||
| 10 | 10 | { |
| 11 | 11 | public function validate(ValueObject $data) |
| 12 | 12 | { |
| 13 | - if(! $this->isIntValue($data->value())) { |
|
| 13 | + if (!$this->isIntValue($data->value())) { |
|
| 14 | 14 | $this->error = "{$data->name()} must be integer"; |
| 15 | 15 | return false; |
| 16 | 16 | } |
| 17 | 17 | return true; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - protected function isIntValue($value){ |
|
| 21 | - if(is_bool($value)) return false; |
|
| 22 | - return is_int($value) || preg_match('/^\d+$/',$value); |
|
| 20 | + protected function isIntValue($value) { |
|
| 21 | + if (is_bool($value)) return false; |
|
| 22 | + return is_int($value) || preg_match('/^\d+$/', $value); |
|
| 23 | 23 | } |
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | public function validate(ValueObject $data) |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - if(! $this->isEmail($data->value())) { |
|
| 14 | + if (!$this->isEmail($data->value())) { |
|
| 15 | 15 | $this->error = "incorrect email address in {$data->name()}"; |
| 16 | 16 | return false; |
| 17 | 17 | } |
@@ -10,7 +10,7 @@ |
||
| 10 | 10 | { |
| 11 | 11 | public function validate(ValueObject $data) |
| 12 | 12 | { |
| 13 | - if(mb_strlen($data->value()) > $this->options[0]) { |
|
| 13 | + if (mb_strlen($data->value()) > $this->options[0]) { |
|
| 14 | 14 | $this->error = "{$data->name()} is too long (maximum {$this->options[0]} characters"; |
| 15 | 15 | return false; |
| 16 | 16 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | { |
| 12 | 12 | public function validate(ValueObject $data) |
| 13 | 13 | { |
| 14 | - if($data instanceof NullValueObject) { |
|
| 14 | + if ($data instanceof NullValueObject) { |
|
| 15 | 15 | $this->error = "{$data->name()} must be specified"; |
| 16 | 16 | return false; |
| 17 | 17 | } |
@@ -14,8 +14,8 @@ |
||
| 14 | 14 | $this->data = $data; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | - public function get($key){ |
|
| 18 | - if(!isset($this->data[$key])) return new NullValueObject($key, null); |
|
| 17 | + public function get($key) { |
|
| 18 | + if (!isset($this->data[$key])) return new NullValueObject($key, null); |
|
| 19 | 19 | |
| 20 | 20 | return new ValueObject($key, $this->data[$key]); |
| 21 | 21 | } |
@@ -17,19 +17,19 @@ |
||
| 17 | 17 | $this->errors = $errors; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - public function success(){ |
|
| 20 | + public function success() { |
|
| 21 | 21 | return $this->result; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - public function fails(){ |
|
| 25 | - return ! $this->success(); |
|
| 24 | + public function fails() { |
|
| 25 | + return !$this->success(); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - public function data(){ |
|
| 28 | + public function data() { |
|
| 29 | 29 | return $this->validated; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - public function errors(){ |
|
| 32 | + public function errors() { |
|
| 33 | 33 | return $this->errors; |
| 34 | 34 | } |
| 35 | 35 | |
@@ -24,52 +24,52 @@ |
||
| 24 | 24 | { |
| 25 | 25 | $this->name = $name; |
| 26 | 26 | $this->value = $value; |
| 27 | - if(!in_array($type, $this->types())) throw new ValueObjectInvalidTypeException('Wrong type: '. $type); |
|
| 27 | + if (!in_array($type, $this->types())) throw new ValueObjectInvalidTypeException('Wrong type: '.$type); |
|
| 28 | 28 | $this->type = $type; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public function __invoke(){ |
|
| 31 | + public function __invoke() { |
|
| 32 | 32 | return $this->value; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - public function name(){ |
|
| 35 | + public function name() { |
|
| 36 | 36 | return $this->name; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public function value(){ |
|
| 39 | + public function value() { |
|
| 40 | 40 | return $this->value; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function type(){ |
|
| 43 | + public function type() { |
|
| 44 | 44 | return $this->type; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - public function isInt(){ |
|
| 47 | + public function isInt() { |
|
| 48 | 48 | return $this->type === self::INT; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | |
| 52 | - public function isString(){ |
|
| 52 | + public function isString() { |
|
| 53 | 53 | return $this->type === self::STRING; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - public function isBool(){ |
|
| 56 | + public function isBool() { |
|
| 57 | 57 | return $this->type === self::BOOL; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - public function isFloat(){ |
|
| 60 | + public function isFloat() { |
|
| 61 | 61 | return $this->type === self::FLOAT; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - public function isUndefinedType(){ |
|
| 64 | + public function isUndefinedType() { |
|
| 65 | 65 | return is_null($this->type); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - protected function types(){ |
|
| 68 | + protected function types() { |
|
| 69 | 69 | return [null, self::BOOL, self::FLOAT, self::INT, self::STRING]; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - public static function get($value, $type = null){ |
|
| 72 | + public static function get($value, $type = null) { |
|
| 73 | 73 | return new static($value, $type); |
| 74 | 74 | } |
| 75 | 75 | |
@@ -25,20 +25,20 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | $dataProvider = new DataProvider($data); |
| 27 | 27 | |
| 28 | - foreach ($rules as $dataKey => $rule){ |
|
| 28 | + foreach ($rules as $dataKey => $rule) { |
|
| 29 | 29 | $testedValue = $dataProvider->get($dataKey); |
| 30 | - $validations = explode(" ",$rule); |
|
| 30 | + $validations = explode(" ", $rule); |
|
| 31 | 31 | |
| 32 | 32 | $valueIsValid = true; |
| 33 | - foreach ($validations as $validation){ |
|
| 33 | + foreach ($validations as $validation) { |
|
| 34 | 34 | $validation = $this->validationFactory->get($validation); |
| 35 | - if(!$validation->validate($testedValue)){ |
|
| 35 | + if (!$validation->validate($testedValue)) { |
|
| 36 | 36 | $this->errors[$dataKey][] = $validation->error(); |
| 37 | 37 | $valueIsValid = false; |
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - if($valueIsValid) { |
|
| 41 | + if ($valueIsValid) { |
|
| 42 | 42 | $this->validated[$testedValue->name()] = $testedValue->value(); |
| 43 | 43 | } else { |
| 44 | 44 | $validationResult = false; |
@@ -48,15 +48,15 @@ discard block |
||
| 48 | 48 | return $validationResult; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - public function errors(){ |
|
| 51 | + public function errors() { |
|
| 52 | 52 | return $this->errors; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - public function validated(){ |
|
| 55 | + public function validated() { |
|
| 56 | 56 | return $this->validated; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - public static function process($data, $rules){ |
|
| 59 | + public static function process($data, $rules) { |
|
| 60 | 60 | $validator = new static(); |
| 61 | 61 | $result = $validator->validate($data, $rules); |
| 62 | 62 | |