Issues (134)

src/mgdschema/property.php (1 issue)

Labels
Severity
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
    /**
16
     * For backward compatibility only
17
     *
18
     * @var string
19
     */
20
    public $reverse;
21
22
    public $noidlink;
23
24
    /**
25
     * DB column name (defaults to $this->name)
26
     */
27
    public string $field;
28
29
    /**
30
     * Does this field point to a parent
31
     *
32
     * @var string
33
     */
34
    public $parentfield;
35
36
    /**
37
     * Field name for MdgSchema object
38
     */
39
    public string $name;
40
41
    /**
42
     * Helpttext
43
     *
44
     * @var string
45
     */
46
    public $description;
47
48
    /**
49
     * Should an index be created for the column
50
     */
51
    public bool $index = false;
52
53
    /**
54
     * Field type as written in XML
55
     */
56
    public string $type;
57
58
    /**
59
     * DB field type (defaults to $this->type)
60
     */
61
    public string $dbtype;
62
63
    /**
64
     * Are values unique?
65
     */
66
    public bool $unique = false;
67
68
    /**
69
     * The field's default value
70
     *
71
     * @var mixed
72
     */
73
    public $default;
74
75
    /**
76
     * Parent type
77
     */
78
    private readonly type $mgdschematype;
79
80 15
    public function __construct(type $parent, string $name, string $type)
81
    {
82 15
        $this->mgdschematype = $parent;
0 ignored issues
show
The property mgdschematype is declared read-only in midgard\portable\mgdschema\property.
Loading history...
83 15
        $this->name = $name;
84 15
        $this->field = $name;
85 15
        $this->type = $type;
86 15
        $this->dbtype = $type;
87
        // type guid always gets an index (but the guid field itself already has unique)
88 15
        $this->index = ($type == 'guid' && $name != 'guid');
89
    }
90
91
    public function get_parent() : type
92
    {
93
        return $this->mgdschematype;
94
    }
95
96 14
    public function set(string $name, $value)
97
    {
98
        switch ($name) {
99 14
            case 'unique':
100 14
            case 'index':
101 12
                $value = ($value === 'yes');
102 12
                break;
103 14
            case 'link':
104 14
                if (strpos($value, ':') < 1) {
105
                    throw new \InvalidArgumentException('link target "' . $value . '" in ' . $this->mgdschematype->name . '::' . $this->name . ' is invalid ');
106
                }
107 14
                $tmp = explode(':', $value);
108 14
                $value = [];
109 14
                $value['target'] = $tmp[0];
110 14
                $value['field'] = $tmp[1];
111 14
                if ($value['field'] !== 'id') {
112 12
                    $this->noidlink = $value;
113 12
                    $value = null;
114
                    // Doctrine can't figure this out automatically, so we do it here
115 12
                    $this->index = true;
116
                }
117 14
                break;
118
        }
119 14
        $this->$name = $value;
120
    }
121
122 12
    public function set_multiple(array $attributes)
123
    {
124 12
        foreach ($attributes as $name => $value) {
125 12
            $this->set($name, $value);
126
        }
127
    }
128
}
129