Passed
Push — master ( bdb0df...101e6d )
by Andreas
04:53 queued 01:05
created

property   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 115
ccs 32
cts 33
cp 0.9697
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_parent() 0 3 1
A __construct() 0 9 2
A set_multiple() 0 4 2
A set() 0 24 6
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
namespace midgard\portable\mgdschema;
9
10
11
class property implements node
12
{
13
    public $link;
14
15
    public $noidlink;
16
17
    /**
18
     * DB column name (defaults to $this->name)
19
     *
20
     * @var string
21
     */
22
    public $field;
23
24
    /**
25
     * Does this field point to a parent
26
     *
27
     * @var string
28
     */
29
    public $parentfield;
30
31
    /**
32
     * Field name for MdgSchema object
33
     *
34
     * @var string
35
     */
36
    public $name;
37
38
    /**
39
     * Helpttext
40
     *
41
     * @var string
42
     */
43
    public $description;
44
45
    /**
46
     * Should an index be created for the column
47
     *
48
     * @var boolean
49
     */
50
    public $index = false;
51
52
    /**
53
     * Field type as written in XML
54
     *
55
     * @var string
56
     */
57
    public $type;
58
59
    /**
60
     * DB field type (defaults to $this->type)
61
     *
62
     * @var string
63
     */
64
    public $dbtype;
65
66
    /**
67
     * Are values unique?
68
     *
69
     * @var boolean
70
     */
71
    public $unique = false;
72
73
    /**
74
     * Parent type
75
     *
76
     * @var type
77
     */
78
    private $mgdschematype;
79
80 18
    public function __construct(type $parent, $name, $type)
81
    {
82 18
        $this->mgdschematype = $parent;
83 18
        $this->name = $name;
84 18
        $this->field = $name;
85 18
        $this->type = $type;
86 18
        $this->dbtype = $type;
87
        // type guid always gets an index (but the guid field itself already has unique)
88 18
        $this->index = ($type == 'guid' && $name != 'guid');
89 18
    }
90
91 5
    public function get_parent()
92
    {
93 5
        return $this->mgdschematype;
94
    }
95
96 17
    public function set($name, $value)
97
    {
98
        switch ($name) {
99 17
            case 'unique':
100 17
            case 'index':
101 15
                $value = ($value === 'yes');
102 15
                break;
103 17
            case 'link':
104 17
                if (strpos($value, ':') < 1) {
105
                    throw new \InvalidArgumentException('link target "' . $value . '" in ' . $this->mgdschematype->name . '::' . $this->name . ' is invalid ');
106
                }
107 17
                $tmp = explode(':', $value);
108 17
                $value = [];
109 17
                $value['target'] = $tmp[0];
110 17
                $value['field'] = $tmp[1];
111 17
                if ($value['field'] !== 'id') {
112 15
                    $this->noidlink = $value;
113 15
                    $value = null;
114
                    // Doctrine can't figure this out automatically, so we do it here
115 15
                    $this->index = true;
116 15
                }
117 17
                break;
118
        }
119 17
        $this->$name = $value;
120 17
    }
121
122 15
    public function set_multiple(array $attributes)
123
    {
124 15
        foreach ($attributes as $name => $value) {
125 15
            $this->set($name, $value);
126 15
        }
127 15
    }
128
}
129