1<?php
2/*
3 *  $Id: Hydrate.php 3192 2007-11-19 17:55:23Z romanb $
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information, see
19 * <http://www.doctrine-project.org>.
20 */
21
22/**
23 * Its purpose is to populate object graphs.
24 *
25 *
26 * @package     Doctrine
27 * @subpackage  Hydrate
28 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29 * @link        www.doctrine-project.org
30 * @since       1.0
31 * @version     $Revision: 3192 $
32 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
33 * @author      Jonathan H. Wage <jonwage@gmail.com>
34 */
35class Doctrine_Hydrator
36{
37    protected static
38        $_totalHydrationTime = 0;
39
40    protected
41        $_hydrators,
42        $_rootAlias = null,
43        $_hydrationMode = Doctrine_Core::HYDRATE_RECORD,
44        $_queryComponents = array();
45
46    public function __construct()
47    {
48        $this->_hydrators = Doctrine_Manager::getInstance()->getHydrators();
49    }
50
51    /**
52     * Set the hydration mode
53     *
54     * @param mixed $hydrationMode  One of the Doctrine_Core::HYDRATE_* constants or
55     *                              a string representing the name of the hydration mode or
56     *                              or an instance of the hydration class
57     */
58    public function setHydrationMode($hydrationMode)
59    {
60        $this->_hydrationMode = $hydrationMode;
61    }
62
63    /**
64     * Get the hydration mode
65     *
66     * @return mixed $hydrationMode One of the Doctrine_Core::HYDRATE_* constants
67     */
68    public function getHydrationMode()
69    {
70        return $this->_hydrationMode;
71    }
72
73    /**
74     * Set the array of query components
75     *
76     * @param array $queryComponents
77     */
78    public function setQueryComponents(array $queryComponents)
79    {
80        $this->_queryComponents = $queryComponents;
81    }
82
83    /**
84     * Get the array of query components
85     *
86     * @return array $queryComponents
87     */
88    public function getQueryComponents()
89    {
90        return $this->_queryComponents;
91    }
92
93    /**
94     * Get the name of the driver class for the passed hydration mode
95     *
96     * @param string $mode
97     * @return string $className
98     */
99    public function getHydratorDriverClassName($mode = null)
100    {
101        if ($mode === null) {
102            $mode = $this->_hydrationMode;
103        }
104
105        if ( ! isset($this->_hydrators[$mode])) {
106            throw new Doctrine_Hydrator_Exception('Invalid hydration mode specified: '.$this->_hydrationMode);
107        }
108
109        return $this->_hydrators[$mode];
110    }
111
112    /**
113     * Get an instance of the hydration driver for the passed hydration mode
114     *
115     * @param string $mode
116     * @param array $tableAliases
117     * @return object Doctrine_Hydrator_Abstract
118     */
119    public function getHydratorDriver($mode, $tableAliases)
120    {
121        $driverClass = $this->getHydratorDriverClassName($mode);
122        if (is_object($driverClass)) {
123            if (!$driverClass instanceOf Doctrine_Hydrator_Abstract) {
124                throw new Doctrine_Hydrator_Exception('Invalid hydration class specified: '.get_class($driverClass));
125            }
126            $driver = $driverClass;
127            $driver->setQueryComponents($this->_queryComponents);
128            $driver->setTableAliases($tableAliases);
129            $driver->setHydrationMode($mode);
130        } else {
131            $driver = new $driverClass($this->_queryComponents, $tableAliases, $mode);
132        }
133
134        return $driver;
135    }
136
137    /**
138     * Hydrate the query statement in to its final data structure by one of the
139     * hydration drivers.
140     *
141     * @param object $stmt
142     * @param array $tableAliases
143     * @return mixed $result
144     */
145    public function hydrateResultSet($stmt, $tableAliases)
146    {
147        $driver = $this->getHydratorDriver($this->_hydrationMode, $tableAliases);
148        $result = $driver->hydrateResultSet($stmt);
149
150        return $result;
151    }
152}