/var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/cakephp/src/ORM/Rule
NameSizeModeActions
ExistsIn.php55870644editdlrm
IsUnique.php31500644editdlrm
LinkConstraint.php61970644editdlrm
ValidCount.php17000644editdlrm
Edit: /var/www/vhosts/ihelp.ro/httpdocs/vendor/cakephp/cakephp/src/ORM/Rule/ExistsIn.php (5587B)
*/ protected $_fields; /** * The repository where the field will be looked for * * @var \Cake\ORM\Table|\Cake\ORM\Association|string */ protected $_repository; /** * Options for the constructor * * @var array */ protected $_options = []; /** * Constructor. * * Available option for $options is 'allowNullableNulls' flag. * Set to true to accept composite foreign keys where one or more nullable columns are null. * * @param array|string $fields The field or fields to check existence as primary key. * @param \Cake\ORM\Table|\Cake\ORM\Association|string $repository The repository where the * field will be looked for, or the association name for the repository. * @param array $options The options that modify the rule's behavior. * Options 'allowNullableNulls' will make the rule pass if given foreign keys are set to `null`. * Notice: allowNullableNulls cannot pass by database columns set to `NOT NULL`. */ public function __construct($fields, $repository, array $options = []) { $options += ['allowNullableNulls' => false]; $this->_options = $options; $this->_fields = (array)$fields; $this->_repository = $repository; } /** * Performs the existence check * * @param \Cake\Datasource\EntityInterface $entity The entity from where to extract the fields * @param array $options Options passed to the check, * where the `repository` key is required. * @throws \RuntimeException When the rule refers to an undefined association. * @return bool */ public function __invoke(EntityInterface $entity, array $options): bool { if (is_string($this->_repository)) { if (!$options['repository']->hasAssociation($this->_repository)) { throw new RuntimeException(sprintf( "ExistsIn rule for '%s' is invalid. '%s' is not associated with '%s'.", implode(', ', $this->_fields), $this->_repository, get_class($options['repository']) )); } $repository = $options['repository']->getAssociation($this->_repository); $this->_repository = $repository; } $fields = $this->_fields; $source = $target = $this->_repository; if ($target instanceof Association) { $bindingKey = (array)$target->getBindingKey(); $realTarget = $target->getTarget(); } else { $bindingKey = (array)$target->getPrimaryKey(); $realTarget = $target; } if (!empty($options['_sourceTable']) && $realTarget === $options['_sourceTable']) { return true; } if (!empty($options['repository'])) { $source = $options['repository']; } if ($source instanceof Association) { $source = $source->getSource(); } if (!$entity->extract($this->_fields, true)) { return true; } if ($this->_fieldsAreNull($entity, $source)) { return true; } if ($this->_options['allowNullableNulls']) { $schema = $source->getSchema(); foreach ($fields as $i => $field) { if ($schema->getColumn($field) && $schema->isNullable($field) && $entity->get($field) === null) { unset($bindingKey[$i], $fields[$i]); } } } $primary = array_map( function ($key) use ($target) { return $target->aliasField($key) . ' IS'; }, $bindingKey ); $conditions = array_combine( $primary, $entity->extract($fields) ); return $target->exists($conditions); } /** * Checks whether the given entity fields are nullable and null. * * @param \Cake\Datasource\EntityInterface $entity The entity to check. * @param \Cake\ORM\Table $source The table to use schema from. * @return bool */ protected function _fieldsAreNull(EntityInterface $entity, Table $source): bool { $nulls = 0; $schema = $source->getSchema(); foreach ($this->_fields as $field) { if ($schema->getColumn($field) && $schema->isNullable($field) && $entity->get($field) === null) { $nulls++; } } return $nulls === count($this->_fields); } }