Skip to content

Commit

Permalink
Fix namespace in validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Pospisil committed Feb 28, 2017
1 parent 7bc939d commit 03f0d66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
use Nette\Http\IRequest;
use Nette\Http\Request;
use Nette\Object;
use Nette\Utils\ArrayHash;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
use RestServer\Exceptions\InvalidParameterException;
use RestServer\Exceptions\MissingRequiredParameterException;
use Tracy\Debugger;


/**
* Parameters
Expand Down
17 changes: 16 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php
namespace RestServer;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
use RestServer\Exceptions\InvalidParameterException;
use RestServer\Exceptions\MissingRequiredParameterException;

/**
* Copyright (c) Jan Pospisil (http://www.jan-pospisil.cz)
Expand All @@ -12,6 +16,8 @@ class Validator extends \Nette\Object {
public function validate($name, $isRequired, array $validators, $value){
if(!$value && $isRequired)
throw new MissingRequiredParameterException('Parameter "'.$name.'" is required.');
if(!$value && !$isRequired)
return null;
if(!$validators)
return $value;
if($value === null && !$isRequired)
Expand All @@ -26,11 +32,20 @@ public function validate($name, $isRequired, array $validators, $value){
throw new InvalidParameterException('Parameter "'.$name.'" must be int.');
$value = (int) $value;
} elseif($validator == Check::LENGTH){
if(!Strings::length($value) != $validatorVal)
if(Strings::length($value) != $validatorVal)
throw new InvalidParameterException('Parameter "'.$name.'" must be exactly '.$validatorVal.' chars length.');
} elseif($validator == Check::MIN_LENGTH){
if(Strings::length($value) < $validatorVal)
throw new InvalidParameterException('Parameter "'.$name.'" must be at least '.$validatorVal.' chars length.');
} elseif($validator == Check::FLOAT){
$value = strtr($value, array(',' => '.'));
if(!Validators::isNumeric($value))
throw new InvalidParameterException('Parameter "'.$name.'" must be float.');
$value = (float) $value;
} elseif($validator == Check::BOOL){
if(!is_bool($value) && $value != 1 && $value != 0)
throw new InvalidParameterException('Parameter "'.$name.'" must be boolean.');
$value = (bool) $value;
}

else {
Expand Down

0 comments on commit 03f0d66

Please sign in to comment.