Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Builds result sets in to the hierarchy graph using php arrays
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Hydrate
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.2
30
 * @version     $Revision$
31
 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
32
 */
33
class Doctrine_Hydrator_ArrayHierarchyDriver extends Doctrine_Hydrator_ArrayDriver
34
{
35
    public function hydrateResultSet($stmt)
36
    {
37
        $collection = parent::hydrateResultSet($stmt);
38
 
39
        $table = $this->getRootComponent();
40
 
41
        if ( ! $table->isTree() || ! $table->hasColumn('level')) {
42
            throw new Doctrine_Exception('Cannot hydrate model that does not implements Tree behavior with `level` column');
43
        }
44
 
45
        // Trees mapped
46
        $trees = array();
47
        $l = 0;
48
 
49
        if (count($collection) > 0) {
50
            // Node Stack. Used to help building the hierarchy
51
            $stack = array();
52
 
53
            foreach ($collection as $child) {
54
                $item = $child;
55
 
56
                $item['__children'] = array();
57
 
58
                // Number of stack items
59
                $l = count($stack);
60
 
61
                // Check if we're dealing with different levels
62
                while($l > 0 && $stack[$l - 1]['level'] >= $item['level']) {
63
                    array_pop($stack);
64
                    $l--;
65
                }
66
 
67
                // Stack is empty (we are inspecting the root)
68
                if ($l == 0) {
69
                    // Assigning the root child
70
                    $i = count($trees);
71
                    $trees[$i] = $item;
72
                    $stack[] = & $trees[$i];
73
                } else {
74
                    // Add child to parent
75
                    $i = count($stack[$l - 1]['__children']);
76
                    $stack[$l - 1]['__children'][$i] = $item;
77
                    $stack[] = & $stack[$l - 1]['__children'][$i];
78
                }
79
            }
80
        }
81
        return $trees;
82
    }
83
}