# Source for file Lib.php

Documentation is available at Lib.php

1. <?php
2. /*
3. * $Id: Lib.php 1916 2007-07-01 10:47:44Z meus $
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.com>.
20. */
21. /**
22. * Doctrine_Lib has not commonly used static functions, mostly for debugging purposes
23. *
24. * @package Doctrine
25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
26. * @category Object Relational Mapping
27. * @link www.phpdoctrine.com
28. * @since 1.0
29. * @version $Revision: 1916 $
30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
31. */
32. class Doctrine_Lib
33. {
34. /**
35. * @param integer $state the state of record
36. * @see Doctrine_Record::STATE_* constants
37. * @return string string representation of given state
38. */
39. public static function getRecordStateAsString($state)
40. {
41. switch ($state) {
42. case Doctrine_Record::STATE_PROXY:
43. return "proxy";
44. break;
45. case Doctrine_Record::STATE_CLEAN:
46. return "persistent clean";
47. break;
48. case Doctrine_Record::STATE_DIRTY:
49. return "persistent dirty";
50. break;
51. case Doctrine_Record::STATE_TDIRTY:
52. return "transient dirty";
53. break;
54. case Doctrine_Record::STATE_TCLEAN:
55. return "transient clean";
56. break;
57. }
58. }
59. /**
60. * returns a string representation of Doctrine_Record object
61. * @param Doctrine_Record $record
62. * @return string
63. */
64. public static function getRecordAsString(Doctrine_Record $record)
65. {
66. $r[] = '<pre>';
67. $r[] = 'Component : ' . $record->getTable()->getComponentName();
68. $r[] = 'ID : ' . $record->obtainIdentifier();
69. $r[] = 'References : ' . count($record->getReferences());
70. $r[] = 'State : ' . Doctrine_Lib::getRecordStateAsString($record->getState());
71. $r[] = 'OID : ' . $record->getOID();
72. $r[] = 'data : ' . Doctrine::dump($record->getData(), false);
73. $r[] = '</pre>';
74. return implode("\n",$r)."<br />";
75. }
76. /**
77. * Return an collection of records as XML.
78. *
79. * @see getRecordAsXml for options to set in the record class to control this.
80. *
81. * @param Doctrine_Collection $collection
82. * @param SimpleXMLElement $xml
83. * @return string Xml as string
84. */
86. public static function getCollectionAsXml(Doctrine_Collection $collection, SimpleXMLElement $incomming_xml = null){
88. $collectionName = Doctrine_Lib::plurelize($collection->getTable()->tableName);
89. if ( $collection->count != 0) {
90. $record = $collection[0];
91. $xml_options = $record->option("xml");
92. if ( isset($xml_options["collection_name"])) {
93. $collectionName = $xml_options["collection_name"];
94. }
95. }
97. if ( ! isset($incomming_xml)) {
98. $new_xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><" . $collectionName . "></" . $collectionName . ">";
99. $xml = new SimpleXMLElement($new_xml_string);
100. } else {
101. $xml = $incomming_xml->addChild($collectionName);
102. }
103. foreach ($collection as $key => $record) {
104. Doctrine_Lib::getRecordAsXml($record, $xml);
105. }
106. return $xml->asXML();
107. }
109. public static function plurelize($string){
110. return $string . "s";
111. }
113. /**
114. * Return a recrd as XML.
115. *
116. * In order to control how this is done set the "xml" option in a record.
117. * This option is an array that has the keys "ignore_fields" and "include_relations". Both of these are arrays that list the name of fields/relations to include/process.
118. *
119. * If you want to insert this xml as a part inside another xml send a
120. * SimpleXMLElement to the function. Because of the nature of SimpleXML the
121. * content you add to this element will be avilable after the function is
122. * complete.
123. *
124. * @param Doctrine_Record $record
125. * @param SimpleXMLElement $xml
126. * @return string Xml as string
127. */
128. public static function getRecordAsXml(Doctrine_Record $record, SimpleXMlElement $incomming_xml = NULL)
129. {
130. $recordname = $record->getTable()->tableName;
131. if ( !isset($incomming_xml)) {
132. $new_xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><" . $recordname . "></" . $recordname . ">";
133. $xml = new SimpleXMLElement($new_xml_string);
134. }else{
135. $xml = $incomming_xml->addChild($recordname);
136. }
137. foreach($record->obtainIdentifier() as $pk_field => $pk_value){
138. $xml->addChild($pk_field,$pk_value);
139. }
140. $xml_options = $record->option("xml");
141. if ( isset($xml_options["record_name"])) {
142. $recordname = $xml_options["record_name"];
143. }
144. foreach ($record->getData() as $field => $value) {
145. if ((isset($xml_options["ignore_fields"]) && !in_array($field, $xml_options["ignore_fields"])) || !isset($xml_options["ignore_fields"])) {
146. if ($value instanceOf Doctrine_Null) {
147. $xml->addChild($field);
148. } else {
149. $xml->addChild($field, $value);
150. }
151. }
152. }
153. if (!isset($xml_options["include_relations"])) {
154. return $xml->asXML();
155. }
156. $relations = $record->getTable()->getRelations();
157. foreach ($relations as $name => $relation) {
158. if (in_array($name, $xml_options["include_relations"])) {
159. $relation_type = $relation->getType();
160. $related_records = $record->get($name);
161. if ($relation_type == Doctrine_Relation::ONE && $related_records instanceOf Doctrine_Record) {
162. Doctrine_Lib::getRecordAsXml($related_records, $xml);
163. } else {
164. Doctrine_Lib::getCollectionAsXml($related_records, $xml);
165. }
166. }
167. }
168. return $xml->asXML();
169. }
172. /**
173. * getStateAsString
174. * returns a given connection state as string
175. * @param integer $state connection state
176. */
177. public static function getConnectionStateAsString($state)
178. {
179. switch ($state) {
180. case Doctrine_Transaction::STATE_SLEEP:
181. return "open";
182. break;
183. case Doctrine_Transaction::STATE_BUSY:
184. return "busy";
185. break;
186. case Doctrine_Transaction::STATE_ACTIVE:
187. return "active";
188. break;
189. }
190. }
191. /**
192. * returns a string representation of Doctrine_Connection object
193. * @param Doctrine_Connection $connection
194. * @return string
195. */
196. public static function getConnectionAsString(Doctrine_Connection $connection)
197. {
198. $r[] = '<pre>';
199. $r[] = 'Doctrine_Connection object';
200. $r[] = 'State : ' . Doctrine_Lib::getConnectionStateAsString($connection->transaction->getState());
201. $r[] = 'Open Transactions : ' . $connection->transaction->getTransactionLevel();
202. $r[] = 'Table in memory : ' . $connection->count();
203. $r[] = 'Driver name : ' . $connection->getAttribute(Doctrine::ATTR_DRIVER_NAME);
205. $r[] = "</pre>";
206. return implode("\n",$r)."<br>";
207. }
208. /**
209. * returns a string representation of Doctrine_Table object
210. * @param Doctrine_Table $table
211. * @return string
212. */
213. public static function getTableAsString(Doctrine_Table $table)
214. {
215. $r[] = "<pre>";
216. $r[] = "Component : ".$table->getComponentName();
217. $r[] = "Table : ".$table->getTableName();
218. $r[] = "</pre>";
219. return implode("\n",$r)."<br>";
220. }
221. /**
222. * @return string
223. */
224. public static function formatSql($sql)
225. {
226. $e = explode("\n",$sql);
227. $color = "367FAC";
228. $l = $sql;
229. $l = str_replace("SELECT ", "<font color='$color'><b>SELECT </b></font><br \> ",$l);
230. $l = str_replace("FROM ", "<font color='$color'><b>FROM </b></font><br \>",$l);
231. $l = str_replace(" LEFT JOIN ", "<br \><font color='$color'><b> LEFT JOIN </b></font>",$l);
232. $l = str_replace(" INNER JOIN ", "<br \><font color='$color'><b> INNER JOIN </b></font>",$l);
233. $l = str_replace(" WHERE ", "<br \><font color='$color'><b> WHERE </b></font>",$l);
234. $l = str_replace(" GROUP BY ", "<br \><font color='$color'><b> GROUP BY </b></font>",$l);
235. $l = str_replace(" HAVING ", "<br \><font color='$color'><b> HAVING </b></font>",$l);
236. $l = str_replace(" AS ", "<font color='$color'><b> AS </b></font><br \> ",$l);
237. $l = str_replace(" ON ", "<font color='$color'><b> ON </b></font>",$l);
238. $l = str_replace(" ORDER BY ", "<font color='$color'><b> ORDER BY </b></font><br \>",$l);
239. $l = str_replace(" LIMIT ", "<font color='$color'><b> LIMIT </b></font><br \>",$l);
240. $l = str_replace(" OFFSET ", "<font color='$color'><b> OFFSET </b></font><br \>",$l);
241. $l = str_replace(" ", "<dd>",$l);
243. return $l;
244. }
245. /**
246. * returns a string representation of Doctrine_Collection object
247. * @param Doctrine_Collection $collection
248. * @return string
249. */
250. public static function getCollectionAsString(Doctrine_Collection $collection)
251. {
252. $r[] = "<pre>";
253. $r[] = get_class($collection);
254. $r[] = 'data : ' . Doctrine::dump($collection->getData(), false);
255. //$r[] = 'snapshot : ' . Doctrine::dump($collection->getSnapshot());
257. $r[] = "</pre>";
258. return implode("\n",$r);
259. }
260. }