set explicit lenght for index
[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                     
37                     if (isset($val->length)) {
38                         $this->fieldLength[(string) $val->name] = (int) $val->length;
39                     }
40                 } else {
41                     $this->field   = (string) $val;
42                 }
43             
44             // reduce complexity of storage of foreign keys 
45             } else if ($key == 'reference') {
46                 $this->referenceTable    = (string) $val->table;
47                 $this->referenceField    = (string) $val->field;
48                 $this->referenceOnUpdate = (string) $val->onupdate;
49                 $this->referenceOnDelete = (string) $val->ondelete;
50                 $this->field             = $this->field[0];
51             }
52         }
53
54         if (empty($this->name)) {
55             $this->setName(join('-', (array)$this->field));
56         }
57     }
58 }