summaryrefslogtreecommitdiff
path: root/framework/Data/SqlMap/Statements/TSqlMapObjectCollectionTree.php
blob: fa2bf1cdb91ce054e9ec87ec12a09ebaaf854d68 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
<?php
/**
 * TMappedStatement and related classes.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package Prado\Data\SqlMap\Statements
 */

namespace Prado\Data\SqlMap\Statements;
use Prado\Collections\TList;
use Prado\Data\SqlMap\DataMapper\TPropertyAccess;
use Prado\Data\SqlMap\DataMapper\TSqlMapExecutionException;

/**
 * TSQLMapObjectCollectionTree class.
 *
 * Maps object collection graphs as trees. Nodes in the collection can
 * be {@link add} using parent relationships. The object collections can be
 * build using the {@link collect} method.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package Prado\Data\SqlMap\Statements
 * @since 3.1
 */
class TSqlMapObjectCollectionTree extends \Prado\TComponent
{
	/**
	 * @var array object graph as tree
	 */
	private $_tree = array();
	/**
	 * @var array tree node values
	 */
	private $_entries = array();
	/**
	 * @var array resulting object collection
	 */
	private $_list = array();

	/**
	 * @return boolean true if the graph is empty
	 */
	public function isEmpty()
	{
		return count($this->_entries) == 0;
	}

	/**
	 * Add a new node to the object tree graph.
	 * @param string parent node id
	 * @param string new node id
	 * @param mixed node value
	 */
	public function add($parent, $node, $object='')
	{
		if(isset($this->_entries[$parent]) && ($this->_entries[$parent]!==null)
			&& isset($this->_entries[$node]) && ($this->_entries[$node]!==null))
		{
			$this->_entries[$node] = $object;
			return;
		}
		$this->_entries[$node] = $object;
		if(empty($parent))
		{
			if(isset($this->_entries[$node]))
				return;
			$this->_tree[$node] = array();
		}
		$found = $this->addNode($this->_tree, $parent, $node);
		if(!$found && !empty($parent))
		{
			$this->_tree[$parent] = array();
			if(!isset($this->_entries[$parent]) || $object !== '')
				$this->_entries[$parent] = $object;
			$this->addNode($this->_tree, $parent, $node);
		}
	}

	/**
	 * Find the parent node and add the new node as its child.
	 * @param array list of nodes to check
	 * @param string parent node id
	 * @param string new node id
	 * @return boolean true if parent node is found.
	 */
	protected function addNode(&$childs, $parent, $node)
	{
		$found = false;
		reset($childs);
		for($i = 0, $k = count($childs); $i < $k; $i++)
		{
			$key = key($childs);
			next($childs);
			if($key == $parent)
			{
				$found = true;
				$childs[$key][$node] = array();
			}
			else
			{
				$found = $found || $this->addNode($childs[$key], $parent, $node);
			}
		}
		return $found;
	}

	/**
	 * @return array object collection
	 */
	public function collect()
	{
		while(count($this->_tree) > 0)
			$this->collectChildren(null, $this->_tree);
		return $this->getCollection();
	}

	/**
	 * @param array list of nodes to check
	 * @return boolean true if all nodes are leaf nodes, false otherwise
	 */
	protected function hasChildren(&$nodes)
	{
		$hasChildren = false;
		foreach($nodes as $node)
			if(count($node) != 0)
				return true;
		return $hasChildren;
	}

	/**
	 * Visit all the child nodes and collect them by removing.
	 * @param string parent node id
	 * @param array list of child nodes.
	 */
	protected function collectChildren($parent, &$nodes)
	{
		$noChildren = !$this->hasChildren($nodes);
		$childs = array();
		for(reset($nodes); $key = key($nodes);)
		{
			next($nodes);
			if($noChildren)
			{
				$childs[] = $key;
				unset($nodes[$key]);
			}
			else
				$this->collectChildren($key, $nodes[$key]);
		}
		if(count($childs) > 0)
			$this->onChildNodesVisited($parent, $childs);
	}

	/**
	 * Set the object properties for all the child nodes visited.
	 * @param string parent node id
	 * @param array list of child nodes visited.
	 */
	protected function onChildNodesVisited($parent, $nodes)
	{
		if(empty($parent) || empty($this->_entries[$parent]))
			return;

		$parentObject = $this->_entries[$parent]['object'];
		$property = $this->_entries[$nodes[0]]['property'];

		$list = TPropertyAccess::get($parentObject, $property);

		foreach($nodes as $node)
		{
			if($list instanceof TList)
				$parentObject->{$property}[] = $this->_entries[$node]['object'];
			else if(is_array($list))
				$list[] = $this->_entries[$node]['object'];
			else
				throw new TSqlMapExecutionException(
					'sqlmap_property_must_be_list');
		}

		if(is_array($list))
			TPropertyAccess::set($parentObject, $property, $list);

		if($this->_entries[$parent]['property'] === null)
			$this->_list[] = $parentObject;
	}

	/**
	 * @return array object collection.
	 */
	protected function getCollection()
	{
		return $this->_list;
	}

	public function __sleep()
	{
		$exprops = array(); $cn = __CLASS__;
		if (!count($this->_tree)) $exprops[] = "\0$cn\0_tree";
		if (!count($this->_entries)) $exprops[] = "\0$cn\0_entries";
		if (!count($this->_list)) $exprops[] = "\0$cn\0_list";
		return array_diff(parent::__sleep(),$exprops);
	}
}