~etenil/datajar/0.01

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
30
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
<?php
/**
 * @file StorableCollection.php
 *
 * @brief A object that contains DatajarBase-derived objects. It is mostly used
 * to contain children of DatajarBase objects within themselves.
 *
 * This is also a class factory that can instanciate new objects bound to the
 * original.
 *
 * Copyright © 2012 Guillaume Pasquet
 *
 * This file is part of Datajar.
 *
 * Datajar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * Datajar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Datajar.  If not, see <http://www.gnu.org/licenses/>.
 */

class DatajarCollection
{
    protected $objects = array();
    protected $mother;

    public function __construct(&$mother)
    {
        $this->mother = $mother;
    }

    public function load($id)
    {
        $children = DatajarSchema::get_relations(get_class($this->mother));

        foreach($children as $relation) {
            $this->objects[$rel['class']] =
                $child_class::objects(array($rel['var'] => $id));
        }
    }

    public function add(&$obj)
    {
        $children = DatajarSchema::get_child_classes(get_class($this->mother));

        if(in_array(get_class($obj), $children)) {
            $this->objects[get_class($obj)][] = &$obj;
        }

        /*$children = DatajarSchema::get_relations(get_class($this->mother));

        $var = "";
        foreach($children as $rel) {
            if($rel['class'] == get_class($obj)) {
                $var = $rel['var'];
                break;
            }
        }

        if($var != "") {
            $obj->$var = $this->mother->id;
            }*/

        return $obj;
    }

    protected function walk_objects($func, array $args)
    {
        $outp = array();
        foreach($this->objects as $objects) {
            foreach($objects as $object) {
                $outp[] = call_user_func_array(array($object, $func), $args);
            }
        }

        return $outp;
    }

    public function save()
    {
        return $this->walk_objects('save', func_get_args());
    }

    public function delete()
    {
        return $this->walk_objects('delete', func_get_args());
    }

    public function drop()
    {
        return $this->walk_objects('drop', func_get_args());
    }
}

?>