Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Validators\BuiltIn;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
use Maslosoft\Mangan\Validators\Traits\Messages;
use Maslosoft\Mangan\Validators\Traits\OnScenario;
use Maslosoft\Mangan\Validators\Traits\Safe;
use Maslosoft\Mangan\Validators\Traits\SkipOnError;
/**
* NumberValidator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class NumberValidator implements ValidatorInterface
{
use AllowEmpty,
Messages,
OnScenario,
Safe,
SkipOnError;
/**
* Whether the attribute value can only be an integer. Defaults to false.
* @var boolean
*/
public $integerOnly = false;
/**
* Upper limit of the number. Defaults to null, meaning no upper limit.
* @var integer|float
*/
public $max = NULL;
/**
* Lower limit of the number. Defaults to null, meaning no lower limit.
* @var integer|float
*/
public $min = NULL;
/**
* Deprecated: Use `msgTooSmall` instead
* @var string user-defined error message used when the value is too big.
* @deprecated Use `msgTooSmall` instead
*/
public $tooBig = NULL;
/**
* Deprecated: Use `msgTooBig` instead
* @var string user-defined error message used when the value is too small.
* @deprecated Use `msgTooBig` instead
*/
public $tooSmall = NULL;
/**
* Custom message to show if value is not number
* @Label('{attribute} must be a number')
* @var string
*/
public $msgNumber = '';
/**
* Custom message to show if value is not integer when integer checking is
* enabled
* @Label('{attribute} must be an integer')
* @var string
*/
public $msgInteger = '';
/**
* Custom message to show if value is over maximum
* @Label('{attribute} is too small (minimum is {min})')
* @var string
*/
public $msgTooSmall = '';
/**
* Custom message to show if value is under minimum
* @Label('{attribute} is too big (maximum is {max})')
* @var string
*/
public $msgTooBig = '';
public function isValid(AnnotatedInterface $model, $attribute)
{
$value = $model->$attribute;
if ($this->allowEmpty && empty($value))
{
return true;
}
$label = ManganMeta::create($model)->field($attribute)->label;
if (!is_scalar($value))
{
$this->addError('msgNumber', ['{attribute}' => $label]);
return false;
}
if (!is_numeric($value))
{
$this->addError('msgNumber', ['{attribute}' => $label]);
return false;
}
if ($this->integerOnly)
{
if (!filter_var($value, FILTER_VALIDATE_INT))
{
$this->addError('msgInteger', ['{attribute}' => $label]);
return false;
}
}
else
{
if (!filter_var($value, FILTER_VALIDATE_FLOAT))
{
$this->addError('msgNumber', ['{attribute}' => $label]);
return false;
}
}
if ($this->min !== null && $value < $this->min)
{
if (!empty($this->tooSmall))
{
$this->addError($this->tooSmall, ['{min}' => $this->min, '{attribute}' => $label]);
return false;
}
$this->addError('msgTooSmall', ['{min}' => $this->min, '{attribute}' => $label]);
return false;
}
if ($this->max !== null && $value > $this->max)
{
if (!empty($this->tooBig))
{
$this->addError($this->tooBig, ['{max}' => $this->max, '{attribute}' => $label]);
}
$this->addError('msgTooBig', ['{max}' => $this->max, '{attribute}' => $label]);
return false;
}
return true;
}
}
API documentation generated by ApiGen