-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dd0fbd
commit 955c01e
Showing
6 changed files
with
618 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace ipl\Validator; | ||
|
||
use Exception; | ||
use ipl\I18n\Translation; | ||
|
||
class BetweenValidator extends BaseValidator | ||
{ | ||
use Translation; | ||
|
||
/** @var mixed Min value */ | ||
protected $min; | ||
|
||
/** @var mixed Max value */ | ||
protected $max; | ||
|
||
/** | ||
* Whether to do inclusive comparisons, allowing equivalence to min and/or max | ||
* | ||
* If false, then strict comparisons are done, and the value may equal neither | ||
* the min nor max options | ||
* | ||
* @var boolean | ||
*/ | ||
protected $inclusive; | ||
|
||
/** | ||
* Validates whether value is between the given min and max | ||
* | ||
* Required options: | ||
* | ||
* - min: (scalar) Minimum border | ||
* - max: (scalar) Maximum border | ||
* | ||
* Optional options: | ||
* | ||
* - inclusive: (bool) Whether inclusive border values, default true | ||
* | ||
* @param array $options | ||
* | ||
* @throws Exception When required option is missing | ||
*/ | ||
public function __construct(array $options) | ||
{ | ||
if (! isset($options['min'], $options['max'])) { | ||
throw new Exception("Missing option. 'min' and 'max' has to be given"); | ||
} | ||
|
||
$this->setMin($options['min']) | ||
->setMax($options['max']) | ||
->setInclusive($options['inclusive'] ?? true); | ||
} | ||
|
||
/** | ||
* Returns the min option | ||
* | ||
* @return mixed | ||
*/ | ||
public function getMin() | ||
{ | ||
return $this->min; | ||
} | ||
|
||
/** | ||
* Sets the min option | ||
* | ||
* @param mixed $min | ||
* | ||
* @return self | ||
*/ | ||
public function setMin($min): self | ||
{ | ||
$this->min = $min; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the max option | ||
* | ||
* @return mixed | ||
*/ | ||
public function getMax() | ||
{ | ||
return $this->max; | ||
} | ||
|
||
/** | ||
* Sets the max option | ||
* | ||
* @param mixed $max | ||
* | ||
* @return self | ||
*/ | ||
public function setMax($max): self | ||
{ | ||
$this->max = $max; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the inclusive option | ||
* | ||
* @return bool | ||
*/ | ||
public function getInclusive(): bool | ||
{ | ||
return $this->inclusive; | ||
} | ||
|
||
/** | ||
* Sets the inclusive option | ||
* | ||
* @param bool $inclusive | ||
* | ||
* @return self | ||
*/ | ||
public function setInclusive($inclusive = true): self | ||
{ | ||
$this->inclusive = (bool) $inclusive; | ||
|
||
return $this; | ||
} | ||
|
||
public function isValid($value) | ||
{ | ||
// Multiple isValid() calls must not stack validation messages | ||
$this->clearMessages(); | ||
|
||
if (empty($value)) { | ||
return true; | ||
} | ||
|
||
if ($this->getInclusive()) { | ||
if ($this->getMin() > $value || $value > $this->getMax()) { | ||
$this->addMessage(sprintf( | ||
$this->translate("'%s' is not between '%s' and '%s', inclusively"), | ||
$value, | ||
$this->getMin(), | ||
$this->getMax() | ||
)); | ||
|
||
return false; | ||
} | ||
} elseif ($this->getMin() >= $value || $value >= $this->getMax()) { | ||
$this->addMessage(sprintf( | ||
$this->translate("'%s' is strictly not between '%s' and '%s'"), | ||
$value, | ||
$this->getMin(), | ||
$this->getMax() | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
namespace ipl\Validator; | ||
|
||
use ipl\I18n\Translation; | ||
|
||
class DirectoryValidator extends BaseValidator | ||
{ | ||
use Translation; | ||
|
||
/** @var bool if set to true check whether directory is readable */ | ||
protected $shouldReadable; | ||
|
||
/** @var bool if set to true check whether directory is writeable */ | ||
protected $shouldWriteable; | ||
|
||
/** | ||
* Validates whether value is a directory | ||
* | ||
* Optional options: | ||
* | ||
* - writeable: (bool) If set to true check whether directory is writeable, default false | ||
* - readable: (bool) If set to true check whether directory is readable, default false | ||
* | ||
* @param array $options | ||
*/ | ||
public function __construct(array $options = []) | ||
{ | ||
$this->setShouldReadable($options['readable'] ?? false); | ||
$this->setShouldWriteable($options['writable'] ?? false); | ||
} | ||
|
||
/** | ||
* Get readable option | ||
* | ||
* @return bool Whether the directory should be readable | ||
*/ | ||
public function getShouldReadable(): bool | ||
{ | ||
return $this->shouldReadable; | ||
} | ||
|
||
/** | ||
* Set whether directory should be readable | ||
* | ||
* @param bool $shouldReadable | ||
*/ | ||
public function setShouldReadable($shouldReadable = true): self | ||
{ | ||
$this->shouldReadable = (bool) $shouldReadable; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get writeable option | ||
* | ||
* @return bool Whether the directory should be writeable | ||
*/ | ||
public function getShouldWriteable(): bool | ||
{ | ||
return $this->shouldWriteable; | ||
} | ||
|
||
/** | ||
* Set whether directory should be writeable | ||
* | ||
* @param bool $shouldWriteable | ||
*/ | ||
public function setShouldWriteable($shouldWriteable = true): void | ||
{ | ||
$this->shouldWriteable = (bool) $shouldWriteable; | ||
} | ||
|
||
public function isValid($value) | ||
{ | ||
// Multiple isValid() calls must not stack validation messages | ||
$this->clearMessages(); | ||
|
||
if (empty($value)) { | ||
return true; | ||
} | ||
|
||
if (! is_dir($value)) { | ||
$this->addMessage(sprintf( | ||
$this->translate("'%s' is not a directory"), | ||
$value | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
//TODO: Separate validator for this?? | ||
if ($this->getShouldReadable() && ! is_readable($value)) { | ||
$this->addMessage(sprintf( | ||
$this->translate("'%s' directory is not readable"), | ||
$value | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
if ($this->getShouldWriteable() && ! is_writable($value)) { | ||
$this->addMessage(sprintf( | ||
$this->translate("'%s' directory is not writeable"), | ||
$value | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace ipl\Validator; | ||
|
||
use ipl\I18n\Translation; | ||
|
||
class GreaterThanValidator extends BaseValidator | ||
{ | ||
use Translation; | ||
|
||
/** @var mixed Comparison value for greater than */ | ||
protected $min; | ||
|
||
/** | ||
* Validates whether the value is greater than the given min | ||
* | ||
* @param mixed $min comparison value for greater than | ||
*/ | ||
public function __construct($min) | ||
{ | ||
$this->setMin($min); | ||
} | ||
|
||
/** | ||
* Get the min option | ||
* | ||
* @return mixed | ||
*/ | ||
public function getMin() | ||
{ | ||
return $this->min; | ||
} | ||
|
||
/** | ||
* Set the min option | ||
* | ||
* @param mixed $min | ||
*/ | ||
public function setMin($min): void | ||
{ | ||
$this->min = $min; | ||
} | ||
|
||
public function isValid($value) | ||
{ | ||
// Multiple isValid() calls must not stack validation messages | ||
$this->clearMessages(); | ||
|
||
if (empty($value)) { | ||
return true; | ||
} | ||
|
||
if ($this->getMin() >= $value) { | ||
$this->addMessage(sprintf( | ||
$this->translate("'%s' is not greater than '%s'"), | ||
$value, | ||
$this->min | ||
)); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.