AbstractResult.php 7.85 KB
Newer Older
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
<?php

/*
 *  $Id: Cache.php 3938 2008-03-06 19:36:50Z romanb $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.org>.
 */

/**
 * Doctrine_Query_AbstractResult
 *
 * @package     Doctrine
 * @subpackage  Query
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
30
 * @since       2.0
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
 * @version     $Revision: 1393 $
 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
abstract class Doctrine_Query_AbstractResult
{
    /**
     * @var mixed $_data The actual data to be stored. Can be an array, a string or an integer.
     */
    protected $_data;

    /**
     * @var array $_queryComponents
     *
     * Two dimensional array containing the map for query aliases. Main keys are component aliases.
     *
     * table    Table object associated with given alias.
     * relation Relation object owned by the parent.
     * parent   Alias of the parent.
     * agg      Aggregates of this component.
     * map      Name of the column / aggregate value this component is mapped to a collection.
     */
    protected $_queryComponents;

    /**
     * @var array Table alias map. Keys are SQL aliases and values DQL aliases.
     */
    protected $_tableAliasMap;

    /**
     * @var array Enum params.
     */
    protected $_enumParams;


    /**
     * Cannot be called directly, factory methods handle this job.
     *
     * @param mixed $data Data to be stored.
     * @param array $queryComponents Query components.
     * @param array $tableAliasMap Table aliases.
     * @param array $enumParams Enum params.
     * @return Doctrine_Query_CacheHandler
     */
    public function __construct($data = '', $queryComponents = array(), $tableAliasMap = array(), $enumParams = array())
    {
        $this->_data = $data;
        $this->_queryComponents = $queryComponents;
        $this->_tableAliasMap = $tableAliasMap;
        $this->_enumParams = $enumParams;
    }


    /**
     * Defines the mapping components.
     *
     * @param array $queryComponents Query components.
     */
    public function setQueryComponents(array $queryComponents)
    {
        $this->_queryComponents = $queryComponents;
    }


    /**
     * Sets the declaration for given component alias.
     *
     * @param string $componentAlias The component alias to set the declaration to.
     * @param string $queryComponent Alias declaration.
     */
    public function setQueryComponent($componentAlias, array $queryComponent)
    {
        $this->_queryComponents[$componentAlias] = $queryComponent;
    }


    /**
     * Gets the mapping components.
     *
     * @return array Query components.
     */
    public function getQueryComponents()
    {
        return $this->_queryComponents;
    }


    /**
     * Get the declaration for given component alias.
     *
     * @param string $componentAlias The component alias the retrieve the declaration from.
     * @return array Alias declaration.
     */
    public function getQueryComponent($componentAlias)
    {
126
        if ( ! array_key_exists($componentAlias, $this->_queryComponents)) {
127 128 129 130 131 132 133
            throw new Doctrine_Query_Exception('Unknown query component ' . $componentAlias);
        }

        return $this->_queryComponents[$componentAlias];
    }


134 135 136 137 138 139 140 141 142 143 144 145
    /**
     * Get the component alias for a given query component
     *
     * @param array $queryComponent The query component
     * @param string Component alias
     */
    public function getComponentAlias($queryComponent)
    {
        return array_search($queryComponent, $this->_queryComponents);;
    }


146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    /**
     * Whether or not this object has a declaration for given component alias.
     *
     * @param string $componentAlias Component alias the retrieve the declaration from.
     * @return boolean True if this object has given alias, otherwise false.
     */
    public function hasQueryComponent($componentAlias)
    {
        return isset($this->_queryComponents[$componentAlias]);
    }


    /**
     * Defines the table aliases.
     *
     * @param array $tableAliasMap Table aliases.
     */
    public function setTableAliasMap(array $tableAliasMap)
    {
        $this->_tableAliasMap = $tableAliasMap;
    }


    /**
     * Adds an SQL table alias and associates it a component alias
     *
     * @param string $tableAlias Table alias to be added.
     * @param string $componentAlias Alias for the query component associated with given tableAlias.
     */
    public function setTableAlias($tableAlias, $componentAlias)
    {
        $this->_tableAliasMap[$tableAlias] = $componentAlias;
    }


    /**
     * Returns all table aliases.
     *
     * @return array Table aliases as an array.
     */
    public function getTableAliasMap()
    {
        return $this->_tableAliasMap;
    }


    /**
     * Get component alias associated with given table alias.
     *
     * @param string $tableAlias SQL table alias that identifies the component alias
     * @return string Component alias
     */
    public function getTableAlias($tableAlias)
    {
        if ( ! isset($this->_tableAliasMap[$tableAlias])) {
            throw new Doctrine_Query_Exception('Unknown table alias ' . $tableAlias);
        }

        return $this->_tableAliasMap[$tableAlias];
    }


    /**
     * Get table alias associated with given component alias.
     *
     * @param string $componentAlias Component alias that identifies the table alias
     * @return string Component alias
     */
    public function getTableAliasFromComponentAlias($componentAlias)
    {
        return array_search($componentAlias, $this->_tableAliasMap);
    }


    /**
     * Whether or not this object has given tableAlias.
     *
     * @param string $tableAlias Table alias to be checked.
     * @return boolean True if this object has given alias, otherwise false.
     */
    public function hasTableAlias($tableAlias)
    {
        return (isset($this->_tableAliasMap[$tableAlias]));
    }


    /**
     * Returns the enum parameters.
     *
     * @return mixed Enum parameters.
     */
    public function getEnumParams()
    {
        return $this->_enumParams;
    }


    /**
     * Sets input parameter as an enumerated parameter
     *
     * @param string $key The key of the input parameter
     * @return Doctrine_Query_AbstractResult
     */
    public function addEnumParam($key, $table = null, $column = null)
    {
        $array = (isset($table) || isset($column)) ? array($table, $column) : array();

        if ($key === '?') {
            $this->_enumParams[] = $array;
        } else {
            $this->_enumParams[$key] = $array;
        }

        return $this;
    }


    /**
     * Returns this object in serialized format, revertable using fromCached*.
     *
     * @return string Serialized cached item.
     */
    public function toCachedForm()
    {
        return serialize(array(
            $this->_data,
            $this->getQueryComponents(),
            $this->getTableAliasMap(),
            $this->getEnumParams()
        ));
    }

}