Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
1 www 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
    {
23
        /*
24
         * Check for an adapter being defined. If not, throw an exception.
25
         */
26
        if (null === $this->adapter) {
27
            throw new \Exception('No database adapter present');
28
        }
29
 
30
        $select          = new Select();
31
        $select->from($this->getTable())->columns(['total' => new Expression('COUNT(*)')]);
32
        $select->where->in($this->getField(), $value);
33
 
34
 
35
        $options = $this->getOptions();
36
        $custom_fields = isset($options['custom_fields']) ? $options['custom_fields'] : null;
37
        if(is_array($custom_fields)) {
38
            foreach($custom_fields as $key_custom => $value_custom)
39
            {
40
                if(!is_null($value_custom)) {
41
                    $select->where->equalTo($key_custom, $value_custom);
42
                }
43
            }
44
        }
45
 
46
        if ($this->exclude !== null)
47
        {
48
        	if (is_array($this->exclude))
49
        	{
50
        		$select->where->notEqualTo(
51
        				$this->exclude['field'],
52
        				$this->exclude['value']
53
        		);
54
        	}
55
        	else
56
        	{
57
        		$select->where($this->exclude);
58
        	}
59
        }
60
 
61
 
62
        $valid = true;
63
 
64
 
65
 
66
 
67
 
68
        $sql = new Sql($this->getAdapter());
69
        $statement = $sql->prepareStatementForSqlObject($select);
5021 efrain 70
 
71
 
5022 efrain 72
 
5021 efrain 73
 
5022 efrain 74
 
1 www 75
        $result = $statement->execute();
76
 
77
        if($result) {
78
            $current = $result->current();
5022 efrain 79
 
1 www 80
            $valid = $current['total'] == count($value);
81
            if(!$valid) {
82
                $this->error(self::ERROR_NO_RECORD_FOUND);
83
            }
84
        }
85
 
86
        return $valid;
87
    }
88
}