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
<?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\Base;
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Validators\BuiltIn\CountValidator;
use Maslosoft\Mangan\Validators\BuiltIn\StringValidator;
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
use Maslosoft\Mangan\Validators\Traits\Messages;
/**
* Base class for size validators.
*
* This can be used as a base for validators checking sizes:
*
* * String Length
* * Number of elements
* * File size
*
* Override msg* attributes with custom `Label` annotations to
* provide proper error messages.
*
* @see StringValidator
* @see CountValidator
* @see ValidatorInterface
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
abstract class SizeValidator
{
use AllowEmpty,
Messages;
/**
* @var integer maximum length. Defaults to null, meaning no maximum limit.
*/
public $max;
/**
* @var integer minimum length. Defaults to null, meaning no minimum limit.
*/
public $min;
/**
* @var integer exact length. Defaults to null, meaning no exact length limit.
*/
public $is;
/**
* @var string user-defined error message used when the value is too short.
* @deprecated use `msgTooShort` instead
*/
public $tooShort;
/**
* @var string user-defined error message used when the value is too long.
* @deprecated use `msgTooLong` instead
*/
public $tooLong;
/**
* @Label('{attribute} is invalid')
* @var string
*/
public $msgInvalid = '';
/**
* @Label('{attribute} is too small')
* @var string
*/
public $msgTooShort = '';
/**
* @Label('{attribute} is too large')
* @var string
*/
public $msgTooLong = '';
/**
* @Label('{attribute} is of the wrong size')
* @var string
*/
public $msgLength = '';
protected function isValidValueOf($model, $attribute, $value, $label = '')
{
if ($this->allowEmpty && empty($value))
{
return true;
}
if (empty($label))
{
$label = ManganMeta::create($model)->field($attribute)->label;
}
if (!is_int($value))
{
$this->addError('msgInvalid', ['{attribute}' => $label]);
return false;
}
if ($this->min !== null && $value < $this->min)
{
if ($this->tooShort)
{
$this->addError($this->tooShort, ['{min}' => $this->min, '{attribute}' => $label]);
return false;
}
$this->addError('msgTooShort', array('{min}' => $this->min, '{attribute}' => $label));
return false;
}
if ($this->max !== null && $value > $this->max)
{
if ($this->tooLong)
{
$this->addError($this->tooLong, array('{max}' => $this->max, '{attribute}' => $label));
return false;
}
$this->addError('msgTooLong', array('{max}' => $this->max, '{attribute}' => $label));
return false;
}
if ($this->is !== null && $value !== $this->is)
{
$this->addError('msgLength', array('{length}' => $this->is, '{attribute}' => $label));
return false;
}
return true;
}
}
API documentation generated by ApiGen