Passed
Push — master ( 0d88a1...fded30 )
by Andreas
14:17
created

property::get_parent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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