Rev 5023 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Validator;
use Laminas\Validator\Db\RecordExists;
use Laminas\Db\Sql\Select;
use Laminas\Db\Sql\Sql;
use Laminas\Db\Sql\TableIdentifier;
use Laminas\Db\Sql\Expression;
class RecordExistsMultiValues extends RecordExists
{
public function __construct($options = null)
{
parent::__construct($options);
}
public function isValid($value)
{
/*
* Check for an adapter being defined. If not, throw an exception.
*/
if (null === $this->adapter) {
throw new \Exception('No database adapter present');
}
$select = new Select();
$select->from($this->getTable())->columns(['total' => new Expression('COUNT(*)')]);
$select->where->in($this->getField(), $value);
$options = $this->getOptions();
$custom_fields = isset($options['custom_fields']) ? $options['custom_fields'] : null;
if(is_array($custom_fields)) {
foreach($custom_fields as $key_custom => $value_custom)
{
if(!is_null($value_custom)) {
$select->where->equalTo($key_custom, $value_custom);
}
}
}
if ($this->exclude !== null)
{
if (is_array($this->exclude))
{
$select->where->notEqualTo(
$this->exclude['field'],
$this->exclude['value']
);
}
else
{
$select->where($this->exclude);
}
}
$valid = true;
$sql = new Sql($this->getAdapter());
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
if($result) {
$current = $result->current();
$valid = $current['total'] == count($value);
if(!$valid) {
$this->error(self::ERROR_NO_RECORD_FOUND);
}
}
return $valid;
}
}