eb7f8ddc7941eb4c2639749045f147ccd89f4584
[tine20] / tine20 / Setup / Backend / Schema / Index / Xml.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Setup
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL3
7  * @copyright   Copyright (c) 2008 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Matthias Greiling <m.greiling@metaways.de>
9  */
10
11
12 class Setup_Backend_Schema_Index_Xml extends Setup_Backend_Schema_Index_Abstract
13 {
14     /**
15      * constructor of this class
16      *
17      * @param string|SimpleXMLElement $_declaration the xml definition of the index
18      */
19     public function __construct($_declaration)
20     {
21         $declaration = $_declaration instanceof SimpleXMLElement ? $_declaration : new SimpleXMLElement($_declaration);
22         
23         $this->_setIndex($declaration);
24     }
25  
26     protected function _setIndex($_declaration)
27     {
28         foreach ($_declaration as $key => $val) {
29             if ($key != 'field' && $key != 'reference') {
30                 $this->$key = (string) $val;
31                 
32             // field definition is stored in SimpleXMLElement in quite different ways, depending on quantity
33             } else if ($key == 'field') {
34                 if ($val instanceof SimpleXMLElement) {
35                     $this->field[] = (string) $val->name;
36                 } else {
37                     $this->field   = (string) $val;
38                 }
39             
40             // reduce complexity of storage of foreign keys 
41             } else if ($key == 'reference') {
42                 $this->referenceTable    = (string) $val->table;
43                 $this->referenceField    = (string) $val->field;
44                 $this->referenceOnUpdate = (string) $val->onupdate;
45                 $this->referenceOnDelete = (string) $val->ondelete;
46                 $this->field             = $this->field[0];
47             }
48         }
49
50         if (empty($this->name)) {
51             $this->setName(join('-', (array)$this->field));
52         }
53     }
54 }