# Source for file Xml.php

Documentation is available at Xml.php

1. <?php
2. /*
3. * $Id: Xml.php 1838 2007-06-26 00:58:21Z nicobn $
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. */
22. /**
23. * class Doctrine_Import_Xml
24. *
25. * Different methods to import a XML schema. The logic behind using two different
26. * methods is simple. Some people will like the idea of producing Doctrine_Record
27. * objects directly, which is totally fine. But in fast and growing application,
28. * table definitions tend to be a little bit more volatile. importArr() can be used
29. * to output a table definition in a PHP file. This file can then be stored
30. * independantly from the object itself.
31. *
32. * @package Doctrine
33. * @category Object Relational Mapping
34. * @link www.phpdoctrine.com
35. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
36. * @version $Revision: 1838 $
37. * @author Nicolas Bérard-Nault <nicobn@gmail.com>
38. * @author Jonathan H. Wage <jonwage@gmail.com>
39. */
40. class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
41. {
42. /**
43. * parse
44. *
45. * @param string $schema
46. * @return void
47. */
48. public function parse($schema)
49. {
50. if (!is_readable($schema)) {
51. throw new Doctrine_Import_Exception('Could not read schema file '. $schema);
52. }
54. if (!($xmlString = file_get_contents($schema))) {
55. throw new Doctrine_Import_Exception('Schema file '. $schema . ' is empty');
56. }
58. return simplexml_load_string($xmlString);
59. }
61. /**
62. * parseSchema
63. *
64. * A method to parse a XML Schema and translate it into a property array.
65. * The function returns that property array.
66. *
67. * @param string $schema Path to the file containing the XML schema
68. * @return array
69. */
70. public function parseSchema($schema)
71. {
72. $xmlObj = $this->parse($schema);
74. // Go through all tables...
75. foreach ($xmlObj->table as $table) {
76. // Go through all columns...
77. foreach ($table->declaration->field as $field) {
78. $colDesc = array(
79. 'name' => (string) $field->name,
80. 'type' => (string) $field->type,
81. 'ptype' => (string) $field->type,
82. 'length' => (int) $field->length,
83. 'fixed' => (int) $field->fixed,
84. 'unsigned' => (bool) $field->unsigned,
85. 'primary' => (bool) (isset($field->primary) && $field->primary),
86. 'default' => (string) $field->default,
87. 'notnull' => (bool) (isset($field->notnull) && $field->notnull),
88. 'autoinc' => (bool) (isset($field->autoincrement) && $field->autoincrement),
89. );
91. $columns[(string) $field->name] = $colDesc;
92. }
94. $class = $table->class ? (string) $table->class:(string) $table->name;
96. $tables[(string) $table->name]['name'] = (string) $table->name;
97. $tables[(string) $table->name]['class'] = (string) $class;
99. $tables[(string) $table->name]['columns'] = $columns;
100. }
102. return $tables;
103. }
104. }