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
<?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\Annotations\Validators;
use Maslosoft\Addendum\Helpers\ParamsExpander;
use Maslosoft\Mangan\Meta\ValidatorMeta;
use Maslosoft\Mangan\Validators\Proxy\CompareProxy;
use Maslosoft\Mangan\Validators\Traits\AllowEmpty;
use Maslosoft\Mangan\Validators\Traits\Strict;
/**
* CompareValidator compares the specified attribute value with another value and validates if they are equal.
*
* The value being compared with can be another attribute value
* (specified via {@link compareAttribute}) or a constant (specified via
* {@link compareValue}. When both are specified, the latter takes
* precedence. If neither is specified, the attribute will be compared
* with another attribute whose name is by appending "_repeat" to the source
* attribute name.
*
* The comparison can be either {@link strict} or not.
*
* CompareValidator supports different comparison operators.
* Previously, it only compares to see if two values are equal or not.
*
* When using the {@link message} property to define a custom error message, the message
* may contain additional placeholders that will be replaced with the actual content. In addition
* to the "{attribute}" placeholder, recognized by all validators (see {@link Validator}),
* CompareValidator allows for the following placeholders to be specified:
* <ul>
* <li>{compareValue}: replaced with the constant value being compared with {@link compareValue}.</li>
* </ul>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.validators
* @since 1.0
*/
class CompareValidatorAnnotation extends ValidatorAnnotation
{
use AllowEmpty,
Strict;
/**
* @var string the name of the attribute to be compared with
*/
public $compareAttribute = NULL;
/**
* @var string the constant value to be compared with
*/
public $compareValue = NULL;
/**
* @var string the operator for comparison. Defaults to '='.
* The followings are valid operators:
* <ul>
* <li>'=' or '==': validates to see if the two values are equal. If {@link strict} is true, the comparison
* will be done in strict mode (i.e. checking value type as well).</li>
* <li>'!=': validates to see if the two values are NOT equal. If {@link strict} is true, the comparison
* will be done in strict mode (i.e. checking value type as well).</li>
* <li>'>': validates to see if the value being validated is greater than the value being compared with.</li>
* <li>'>=': validates to see if the value being validated is greater than or equal to the value being compared with.</li>
* <li>'<': validates to see if the value being validated is less than the value being compared with.</li>
* <li>'<=': validates to see if the value being validated is less than or equal to the value being compared with.</li>
* </ul>
*/
public $operator = '=';
public function init()
{
$this->proxy = CompareProxy::class;
$this->getEntity()->validators[] = new ValidatorMeta(ParamsExpander::expand($this, [
'compareAttribute',
'compareValue',
'strict',
'allowEmpty',
'operator',
'message',
'skipOnError',
'on',
'safe',
'enableClientValidation',
'except',
'proxy'
]));
}
}
API documentation generated by ApiGen