File indexing completed on 2024-12-22 05:36:31

0001 <?php
0002 /**
0003  * LICENSE
0004  *
0005  * This source file is subject to the new BSD license that is bundled
0006  * with this package in the file LICENSE.txt.
0007  * It is also available through the world-wide-web at this URL:
0008  * http://framework.zend.com/license/new-bsd
0009  * If you did not receive a copy of the license and are unable to
0010  * obtain it through the world-wide-web, please send an email
0011  * to license@zend.com so we can send you a copy immediately.
0012  *
0013  * @category   Zend
0014  * @package    Zend_Cloud
0015  * @subpackage DocumentService
0016  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0017  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0018  */
0019 
0020 // require_once 'Zend/Cloud/DocumentService/QueryAdapter.php';
0021 
0022 /**
0023  * Generic query object
0024  *
0025  * Aggregates operations in an array of clauses, where the first element
0026  * describes the clause type, and the next element describes the criteria.
0027  *
0028  * @category   Zend
0029  * @package    Zend_Cloud
0030  * @subpackage DocumentService
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Cloud_DocumentService_Query
0035     implements Zend_Cloud_DocumentService_QueryAdapter
0036 {
0037     /**
0038      * Known query types
0039      */
0040     const QUERY_SELECT  = 'select';
0041     const QUERY_FROM    = 'from';
0042     const QUERY_WHERE   = 'where';
0043     const QUERY_WHEREID = 'whereid'; // request element by ID
0044     const QUERY_LIMIT   = 'limit';
0045     const QUERY_ORDER   = 'order';
0046 
0047     /**
0048      * Clause list
0049      *
0050      * @var array
0051      */
0052     protected $_clauses = array();
0053 
0054     /**
0055      * Generic clause
0056      *
0057      * You can use any clause by doing $query->foo('bar')
0058      * but concrete adapters should be able to recognise it
0059      *
0060      * The call will be iterpreted as clause 'foo' with argument 'bar'
0061      *
0062      * @param  string $name Clause/method name
0063      * @param  mixed $args
0064      * @return Zend_Cloud_DocumentService_Query
0065      */
0066     public function __call($name, $args)
0067     {
0068         $this->_clauses[] = array(strtolower($name), $args);
0069         return $this;
0070     }
0071 
0072     /**
0073      * SELECT clause (fields to be selected)
0074      *
0075      * @param  null|string|array $select
0076      * @return Zend_Cloud_DocumentService_Query
0077      */
0078     public function select($select)
0079     {
0080         if (empty($select)) {
0081             return $this;
0082         }
0083         if (!is_string($select) && !is_array($select)) {
0084             // require_once 'Zend/Cloud/DocumentService/Exception.php';
0085             throw new Zend_Cloud_DocumentService_Exception("SELECT argument must be a string or an array of strings");
0086         }
0087         $this->_clauses[] = array(self::QUERY_SELECT, $select);
0088         return $this;
0089     }
0090 
0091     /**
0092      * FROM clause
0093      *
0094      * @param string $name Field names
0095      * @return Zend_Cloud_DocumentService_Query
0096      */
0097     public function from($name)
0098     {
0099         if(!is_string($name)) {
0100             // require_once 'Zend/Cloud/DocumentService/Exception.php';
0101             throw new Zend_Cloud_DocumentService_Exception("FROM argument must be a string");
0102         }
0103         $this->_clauses[] = array(self::QUERY_FROM, $name);
0104         return $this;
0105     }
0106 
0107     /**
0108      * WHERE query
0109      *
0110      * @param string $cond Condition
0111      * @param array $args Arguments to substitute instead of ?'s in condition
0112      * @param string $op relation to other clauses - and/or
0113      * @return Zend_Cloud_DocumentService_Query
0114      */
0115     public function where($cond, $value = null, $op = 'and')
0116     {
0117         if (!is_string($cond)) {
0118             // require_once 'Zend/Cloud/DocumentService/Exception.php';
0119             throw new Zend_Cloud_DocumentService_Exception("WHERE argument must be a string");
0120         }
0121         $this->_clauses[] = array(self::QUERY_WHERE, array($cond, $value, $op));
0122         return $this;
0123     }
0124 
0125     /**
0126      * Select record or fields by ID
0127      *
0128      * @param  string|int $value Identifier to select by
0129      * @return Zend_Cloud_DocumentService_Query
0130      */
0131     public function whereId($value)
0132     {
0133         if (!is_scalar($value)) {
0134             // require_once 'Zend/Cloud/DocumentService/Exception.php';
0135             throw new Zend_Cloud_DocumentService_Exception("WHEREID argument must be a scalar");
0136         }
0137         $this->_clauses[] = array(self::QUERY_WHEREID, $value);
0138         return $this;
0139     }
0140 
0141     /**
0142      * LIMIT clause (how many items to return)
0143      *
0144      * @param  int $limit
0145      * @return Zend_Cloud_DocumentService_Query
0146      */
0147     public function limit($limit)
0148     {
0149         if ($limit != (int) $limit) {
0150             // require_once 'Zend/Cloud/DocumentService/Exception.php';
0151             throw new Zend_Cloud_DocumentService_Exception("LIMIT argument must be an integer");
0152         }
0153         $this->_clauses[] = array(self::QUERY_LIMIT, $limit);
0154         return $this;
0155     }
0156 
0157     /**
0158      * ORDER clause; field or fields to sort by, and direction to sort
0159      *
0160      * @param  string|int|array $sort
0161      * @param  string $direction
0162      * @return Zend_Cloud_DocumentService_Query
0163      */
0164     public function order($sort, $direction = 'asc')
0165     {
0166         $this->_clauses[] = array(self::QUERY_ORDER, array($sort, $direction));
0167         return $this;
0168     }
0169 
0170     /**
0171      * "Assemble" the query
0172      *
0173      * Simply returns the clauses present.
0174      *
0175      * @return array
0176      */
0177     public function assemble()
0178     {
0179         return $this->getClauses();
0180     }
0181 
0182     /**
0183      * Return query clauses as an array
0184      *
0185      * @return array Clauses in the query
0186      */
0187     public function getClauses()
0188     {
0189          return $this->_clauses;
0190     }
0191 }