Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Validator;
6
 
7
use Laminas\Validator\Db\RecordExists;
8
use Laminas\Db\Sql\Select;
9
use Laminas\Db\Sql\Sql;
10
use Laminas\Db\Sql\TableIdentifier;
11
use Laminas\Db\Sql\Expression;
12
 
13
class RecordExistsMultiValues extends RecordExists
14
{
15
 
16
 	public function __construct($options = null)
17
    {
18
        parent::__construct($options);
19
	}
20
 
21
    public function isValid($value)
22
    {
308 www 23
 
24
 
1 efrain 25
        /*
26
         * Check for an adapter being defined. If not, throw an exception.
27
         */
28
        if (null === $this->adapter) {
29
            throw new \Exception('No database adapter present');
30
        }
31
 
32
        $select          = new Select();
33
        $select->from($this->getTable())->columns(['total' => new Expression('COUNT(*)')]);
34
        $select->where->in($this->getField(), $value);
35
 
36
 
37
        $options = $this->getOptions();
38
        $custom_fields = isset($options['custom_fields']) ? $options['custom_fields'] : null;
39
        if(is_array($custom_fields)) {
40
            foreach($custom_fields as $key_custom => $value_custom)
41
            {
42
                if(!is_null($value_custom)) {
43
                    $select->where->equalTo($key_custom, $value_custom);
44
                }
45
            }
46
        }
47
 
48
        if ($this->exclude !== null)
49
        {
50
        	if (is_array($this->exclude))
51
        	{
52
        		$select->where->notEqualTo(
53
        				$this->exclude['field'],
54
        				$this->exclude['value']
55
        		);
56
        	}
57
        	else
58
        	{
59
        		$select->where($this->exclude);
60
        	}
61
        }
62
 
63
 
64
        $valid = true;
65
 
66
 
67
 
68
 
69
 
70
        $sql = new Sql($this->getAdapter());
71
        $statement = $sql->prepareStatementForSqlObject($select);
72
        $result = $statement->execute();
73
 
74
        if($result) {
75
            $current = $result->current();
76
            $valid = $current['total'] == count($value);
77
            if(!$valid) {
78
                $this->error(self::ERROR_NO_RECORD_FOUND);
79
            }
80
        }
81
 
82
        return $valid;
83
    }
84
}