Passed
Branch master (b2f909)
by Andreas
04:01
created

property   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 115
ccs 28
cts 31
cp 0.9032
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 17
    public function __construct(type $parent, string $name, string $type)
81
    {
82 17
        $this->mgdschematype = $parent;
83 17
        $this->name = $name;
84 17
        $this->field = $name;
85 17
        $this->type = $type;
86 17
        $this->dbtype = $type;
87
        // type guid always gets an index (but the guid field itself already has unique)
88 17
        $this->index = ($type == 'guid' && $name != 'guid');
89
    }
90
91
    public function get_parent() : type
92
    {
93
        return $this->mgdschematype;
94
    }
95
96 16
    public function set(string $name, $value)
97
    {
98 16
        switch ($name) {
99 16
            case 'unique':
100 16
            case 'index':
101 14
                $value = ($value === 'yes');
102 14
                break;
103 16
            case 'link':
104 16
                if (strpos($value, ':') < 1) {
105
                    throw new \InvalidArgumentException('link target "' . $value . '" in ' . $this->mgdschematype->name . '::' . $this->name . ' is invalid ');
106
                }
107 16
                $tmp = explode(':', $value);
108 16
                $value = [];
109 16
                $value['target'] = $tmp[0];
110 16
                $value['field'] = $tmp[1];
111 16
                if ($value['field'] !== 'id') {
112 14
                    $this->noidlink = $value;
113 14
                    $value = null;
114
                    // Doctrine can't figure this out automatically, so we do it here
115 14
                    $this->index = true;
116
                }
117 16
                break;
118
        }
119 16
        $this->$name = $value;
120
    }
121
122 14
    public function set_multiple(array $attributes)
123
    {
124 14
        foreach ($attributes as $name => $value) {
125 14
            $this->set($name, $value);
126
        }
127
    }
128
}
129