/var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/cakephp/src/ORM/Rule
Edit: /var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/cakephp/src/ORM/Rule/IsUnique.php (3150B)
*/
protected $_fields;
/**
* The unique check options
*
* @var array
*/
protected $_options = [
'allowMultipleNulls' => false,
];
/**
* Constructor.
*
* ### Options
*
* - `allowMultipleNulls` Allows any field to have multiple null values. Defaults to false.
*
* @param array $fields The list of fields to check uniqueness for
* @param array $options The options for unique checks.
*/
public function __construct(array $fields, array $options = [])
{
$this->_fields = $fields;
$this->_options = $options + $this->_options;
}
/**
* Performs the uniqueness check
*
* @param \Cake\Datasource\EntityInterface $entity The entity from where to extract the fields
* where the `repository` key is required.
* @param array $options Options passed to the check,
* @return bool
*/
public function __invoke(EntityInterface $entity, array $options): bool
{
if (!$entity->extract($this->_fields, true)) {
return true;
}
$fields = $entity->extract($this->_fields);
if ($this->_options['allowMultipleNulls'] && array_filter($fields, 'is_null')) {
return true;
}
$alias = $options['repository']->getAlias();
$conditions = $this->_alias($alias, $fields);
if ($entity->isNew() === false) {
$keys = (array)$options['repository']->getPrimaryKey();
$keys = $this->_alias($alias, $entity->extract($keys));
if (Hash::filter($keys)) {
$conditions['NOT'] = $keys;
}
}
return !$options['repository']->exists($conditions);
}
/**
* Add a model alias to all the keys in a set of conditions.
*
* @param string $alias The alias to add.
* @param array $conditions The conditions to alias.
* @return array
*/
protected function _alias(string $alias, array $conditions): array
{
$aliased = [];
foreach ($conditions as $key => $value) {
$aliased["$alias.$key IS"] = $value;
}
return $aliased;
}
}