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