Skip to content

Commit

Permalink
WIP (some more validators)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Oct 11, 2022
1 parent 5dd0fbd commit 955c01e
Show file tree
Hide file tree
Showing 6 changed files with 618 additions and 0 deletions.
160 changes: 160 additions & 0 deletions src/BetweenValidator.php
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;
}
}
114 changes: 114 additions & 0 deletions src/DirectoryValidator.php
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;
}
}
65 changes: 65 additions & 0 deletions src/GreaterThanValidator.php
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;
}
}
Loading

0 comments on commit 955c01e

Please sign in to comment.