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 160
<?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\Criteria;
use Maslosoft\Mangan\Finder;
use Maslosoft\Mangan\Helpers\PkManager;
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\ScenarioManager;
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;
use Maslosoft\Mangan\Validators\Traits\When;
/**
* ImmutableValidator validates that the attribute value
* is same as in the stored in database, if it was stored already.
*
* @author Florian Fackler <florian.fackler@mintao.com>
* @version $Id$
* @package system.validators
* @since 1.0
*/
class ImmutableValidator implements ValidatorInterface
{
use AllowEmpty,
SkipOnError,
Messages,
OnScenario,
Safe,
When;
/**
* Set this value to check against trueish value stored in database.
* If empty this will check for validated attribute.
*
* @var string
*/
public $against = '';
/**
* @var string the document class name that should be used to
* look for the attribute value being validated. Defaults to null, meaning using
* the class of the object currently being validated.
*
* @see attributeName
* @since 1.0.8
*/
public $className;
/**
* @var string the ActiveRecord class attribute name that should be
* used to look for the attribute value being validated. Defaults to null,
* meaning using the name of the attribute being validated.
*
* @see className
* @since 1.0.8
*/
public $attributeName;
/**
* @var array additional query criteria. This will be combined with the condition
* that checks if the attribute value exists in the corresponding table column.
* This array will be used to instantiate a {@link Criteria} object.
* @since 1.0.8
*/
public $criteria = [];
/**
* @Label('{attribute} cannot be changed once set')
* @var string
*/
public $msgImmutable = '';
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param AnnotatedInterface $model the object being validated
* @param string $attribute the attribute being validated
*/
public function isValid(AnnotatedInterface $model, $attribute)
{
if (!$this->whenValidate($model))
{
return true;
}
$value = $model->$attribute;
if ($this->allowEmpty && empty($value))
{
return true;
}
$className = empty($this->className) ? get_class($model) : $this->className;
$compareModel = new $className;
$pk = PkManager::getFromModel($model);
PkManager::applyToModel($compareModel, $pk);
$criteria = PkManager::prepareFromModel($compareModel);
if ($this->criteria !== [])
{
$criteria->mergeWith($this->criteria);
}
ScenarioManager::setScenario($compareModel, ValidatorInterface::ScenarioValidate);
$finder = new Finder($compareModel);
$found = $finder->find($criteria);
// Not found entirely
if (null === $found)
{
return true;
}
// Decide against which field to check
if (empty($this->against))
{
$against = $attribute;
}
else
{
$against = $this->against;
}
// Not stored in DB
if (empty($found->$against))
{
return true;
}
// Stored in DB, but value is same
if ($found->$attribute === $model->$attribute)
{
return true;
}
$label = ManganMeta::create($model)->field($attribute)->label;
$this->addError('msgImmutable', ['{attribute}' => $label, '{value}' => $value]);
return false;
}
}
API documentation generated by ApiGen