Coverage for Doctrine_Hydrator_RecordDriver

Back to coverage report

1 <?php
2 /*
3  *  $Id$
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.phpdoctrine.org>.
20  */
21
22 /**
23  * Doctrine_Hydrate_RecordDriver
24  * Hydration strategy used for creating collections of entity objects.
25  *
26  * @package     Doctrine
27  * @subpackage  Hydrate
28  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29  * @link        www.phpdoctrine.org
30  * @since       1.0
31  * @version     $Revision$
32  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
33  * @author      Roman Borschel <roman@code-factory.org>
34  */
35 class Doctrine_Hydrator_RecordDriver extends Doctrine_Locator_Injectable
36 {
37     protected $_collections = array();
38     protected $_tables = array();
39
40     public function getElementCollection($component)
41     {
42         $coll = new Doctrine_Collection($component);
43         $this->_collections[] = $coll;
44
45         return $coll;
46     }
47
48     public function getLastKey($coll) 
49     {
50         $coll->end();
51         
52         return $coll->key();
53     }
54     
55     public function initRelated($record, $name)
56     {
57         return true;
58         /*
59         if ( ! is_array($record)) {
60             $record[$name];
61             return true;
62         }
63         return false;
64         */
65     }
66     
67     public function registerCollection(Doctrine_Collection $coll)
68     {
69         $this->_collections[] = $coll;
70     }
71
72     /**
73      * isIdentifiable
74      * returns whether or not a given data row is identifiable (it contains
75      * all primary key fields specified in the second argument)
76      *
77      * @param array $row
78      * @param Doctrine_Table $table
79      * @return boolean
80      */
81     /*public function isIdentifiable(array $row, Doctrine_Table $table)
82     {
83         $primaryKeys = $table->getIdentifierColumnNames();
84
85         if (is_array($primaryKeys)) {
86             foreach ($primaryKeys as $id) {
87                 if ( ! isset($row[$id])) {
88                     return false;
89                 }
90             }
91         } else {
92             if ( ! isset($row[$primaryKeys])) {
93                 return false;
94             }
95         }
96         return true;
97     }*/
98     
99     public function getNullPointer() 
100     {
101         return self::$_null;
102     }
103     
104     public function getElement(array $data, $component)
105     {
106         $component = $this->_getClassNameToReturn($data, $component);
107         if ( ! isset($this->_tables[$component])) {
108             $this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
109             $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
110         }
111
112         $this->_tables[$component]->setData($data);
113         $record = $this->_tables[$component]->getRecord();
114
115         return $record;
116     }
117     
118     public function flush()
119     {
120         // take snapshots from all initialized collections
121         foreach ($this->_collections as $key => $coll) {
122             $coll->takeSnapshot();
123         }
124         foreach ($this->_tables as $table) {
125             $table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
126         }
127     }
128     
129     /**
130      * Get the classname to return. Most often this is just the options['name']
131      *
132      * Check the subclasses option and the inheritanceMap for each subclass to see
133      * if all the maps in a subclass is met. If this is the case return that
134      * subclass name. If no subclasses match or if there are no subclasses defined
135      * return the name of the class for this tables record.
136      *
137      * @todo this function could use reflection to check the first time it runs
138      * if the subclassing option is not set.
139      *
140      * @return string The name of the class to create
141      *
142      */
143     protected function _getClassnameToReturn(array &$data, $component)
144     {
145         if ( ! isset($this->_tables[$component])) {
146             $this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
147             $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
148         }
149         
150         if ( ! ($subclasses = $this->_tables[$component]->getOption('subclasses'))) {
151             return $component;
152         }
153         
154         foreach ($subclasses as $subclass) {
155             $table = Doctrine_Manager::getInstance()->getTable($subclass);
156             $inheritanceMap = $table->getOption('inheritanceMap');
157             list($key, $value) = each($inheritanceMap);
158             if ( ! isset($data[$key]) || $data[$key] != $value) {
159                 continue;
160             } else {
161                 return $table->getComponentName();
162             }
163         }
164         return $component;
165     }
166 }