Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Abstract.php « Db « Validate « Zend « libs - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5103ff4edf159092c4771c1f2206c06b8937e91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php  
  
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Validate
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Abstract.php 17160 2009-07-26 19:46:24Z bittarman $
 */  
  
/**
 * @see Zend_Validate_Abstract
 */  
require_once 'Zend/Validate/Abstract.php';  
  
/**
 * Class for Database record validation
 * 
 * @category   Zend
 * @package    Zend_Validate
 * @uses       Zend_Validate_Abstract
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */  
abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract    
{   
    /**
     * Error constants
     */ 
    const ERROR_NO_RECORD_FOUND = 'noRecordFound';  
    const ERROR_RECORD_FOUND    = 'recordFound';  
   
    /**
     * @var array Message templates
     */    
    protected $_messageTemplates = array(self::ERROR_NO_RECORD_FOUND => 'No record matching %value% was found', 
                                         self::ERROR_RECORD_FOUND    => 'A record matching %value% was found');     

    /**
     * @var string
     */ 
    protected $_schema = null;
     
    /**
     * @var string
     */    
    protected $_table = '';    
    
    /**
     * @var string
     */    
    protected $_field = '';    
    
    /**
     * @var mixed
     */   
    protected $_exclude = null;   
       
    /**
     * Database adapter to use. If null isValid() will use Zend_Db::getInstance instead
     *
     * @var unknown_type
     */    
    protected $_adapter = null;    
   
    /**
     * Provides basic configuration for use with Zend_Validate_Db Validators 
     * Setting $exclude allows a single record to be excluded from matching.
     * Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
     * to define the where clause added to the sql.  
     * A database adapter may optionally be supplied to avoid using the registered default adapter. 
     * 
     * @param string||array $table The database table to validate against, or array with table and schema keys
     * @param string $field The field to check for a match
     * @param string||array $exclude An optional where clause or field/value pair to exclude from the query
     * @param Zend_Db_Adapter_Abstract $adapter An optional database adapter to use.
     */   
    public function __construct($table, $field, $exclude = null, Zend_Db_Adapter_Abstract $adapter = null)    
    {    
        if ($adapter !== null) { 
            $this->_adapter = $adapter;    
        }   
        $this->_exclude = $exclude;   
        $this->_field   = (string) $field;   
        
        if (is_array($table)) {
            $this->_table  = (isset($table['table'])) ? $table['table'] : '';
            $this->_schema = (isset($table['schema'])) ? $table['schema'] : null;                
        } else {
            $this->_table = (string) $table;            
        }

    }  
     
    /**
     * Run query and returns matches, or null if no matches are found.
     *
     * @param  String $value
     * @return Array when matches are found.
     */ 
    protected function _query($value) 
    { 
        /**
         * Check for an adapter being defined. if not, fetch the default adapter.
         */ 
        if ($this->_adapter === null) {
            $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
            if (null === $this->_adapter) {
                require_once 'Zend/Validate/Exception.php';
                throw new Zend_Validate_Exception('No database adapter present');
            }
        }

        /**
         * Build select object
         */ 
        $select = new Zend_Db_Select($this->_adapter);
        $select->from($this->_table, array($this->_field), $this->_schema)
               ->where($this->_adapter->quoteIdentifier($this->_field).' = ?', $value); 
        if ($this->_exclude !== null) { 
            if (is_array($this->_exclude)) { 
                $select->where($this->_adapter->quoteIdentifier($this->_exclude['field']).' != ?', $this->_exclude['value']); 
            } else { 
                $select->where($this->_exclude); 
            } 
        } 
        $select->limit(1); 
                 
        /**
         * Run query
         */ 
        $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC); 
         
        return $result; 
    } 
}