Completed
Pull Request — master (#114)
by Bart
07:39
created

Fields::getRecords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\base\Field;
8
use craft\models\FieldLayout;
9
10
/**
11
 * Schematic Fields Service.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class Fields extends Base
22
{
23
    //==============================================================================================================
24
    //================================================  EXPORT  ====================================================
25
    //==============================================================================================================
26
27
    /**
28
     * Get all field groups
29
     *
30
     * @return FieldGroup[]
31
     */
32
    protected function getRecords()
33
    {
34
        return Craft::$app->fields->getAllGroups();
35
    }
36
37
    /**
38
     * Get all field definitions per group
39
     *
40
     * @return array
41
     */
42
    public function export(array $records = null)
43
    {
44
        $fieldGroups = $records ?: $this->getRecords();
45
        $result = [];
46
        foreach ($fieldGroups as $group) {
47
            $fields = $group->getFields();
48
            if (count($fields) > 0) {
49
                $result[$group->name] = parent::export($group->getFields());
50
            }
51
        }
52
        return $result;
53
    }
54
55
    /**
56
     * Get section definition.
57
     *
58
     * @param Model $record
59
     *
60
     * @return array
61
     */
62
    protected function getRecordDefinition(Model $record)
63
    {
64
        $attributes = parent::getRecordDefinition($record);
65
        if ($record instanceof Field) {
66
            unset($attributes['groupId']);
67
            unset($attributes['layoutId']);
68
            unset($attributes['tabId']);
69
        }
70
71
        return $attributes;
72
    }
73
74
    //==============================================================================================================
75
    //================================================  IMPORT  ====================================================
76
    //==============================================================================================================
77
78
    /**
79
     * Attempt to import fields.
80
     *
81
     * @param array $groupDefinitions
82
     * @param bool  $force            if set to true items not in the import will be deleted
83
     *
84
     * @return Result
85
     */
86
    public function import($force = false, array $groupDefinitions = null)
87
    {
88
        Craft::info('Importing Fields', 'schematic');
89
90
        if (!empty($groupDefinitions)) {
91
            $this->setGlobalContext();
92
            $this->groups = Craft::$app->fields->getAllGroups('name');
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
            $this->fields = Craft::$app->fields->getAllFields('handle');
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
95
            foreach ($groupDefinitions as $name => $fieldDefinitions) {
96
                try {
97
                    $this->beginTransaction();
0 ignored issues
show
Documentation Bug introduced by
The method beginTransaction does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
98
99
                    $group = $this->createFieldGroupModel($name);
100
101
                    $this->importFields($fieldDefinitions, $group, $force);
102
103
                    $this->commitTransaction();
0 ignored issues
show
Documentation Bug introduced by
The method commitTransaction does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
104
                } catch (\Exception $e) {
105
                    $this->rollbackTransaction();
0 ignored issues
show
Documentation Bug introduced by
The method rollbackTransaction does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
106
107
                    $this->addError($e->getMessage());
0 ignored issues
show
Documentation Bug introduced by
The method addError does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
108
                }
109
110
                $this->unsetData($name, $fieldDefinitions);
111
            }
112
113
            if ($force) { // Remove not imported data
114
                $this->deleteFieldsAndGroups();
115
            }
116
        }
117
118
        return $this->getResultModel();
0 ignored issues
show
Documentation Bug introduced by
The method getResultModel does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
119
    }
120
121
    /**
122
     * Save field group.
123
     *
124
     * @param FieldGroupModel $group
125
     *
126
     * @throws Exception
127
     */
128
    private function saveFieldGroupModel(FieldGroupModel $group)
129
    {
130
        if (!Craft::$app->fields->saveGroup($group)) {
131
            $this->addErrors($group->getAllErrors());
0 ignored issues
show
Documentation Bug introduced by
The method addErrors does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
132
133
            throw new Exception('Failed to save group');
134
        }
135
    }
136
137
    /**
138
     * Save field.
139
     *
140
     * @param FieldModel $field
141
     *
142
     * @throws \Exception
143
     */
144
    private function saveFieldModel(FieldModel $field)
145
    {
146
        $this->validateFieldModel($field); // Validate field
147
        if ($field->context === 'global') {
148
            $this->setGlobalContext();
149
        }
150
        if (!Craft::$app->fields->saveField($field)) {
151
            $this->addErrors($field->getAllErrors());
0 ignored issues
show
Documentation Bug introduced by
The method addErrors does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
152
153
            throw new Exception('Failed to save field');
154
        }
155
    }
156
157
    /**
158
     * Removes fields that where not imported.
159
     */
160
    private function deleteFields()
161
    {
162
        $fieldsService = Craft::$app->fields;
163
        foreach ($this->fields as $field) {
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
164
            $fieldsService->deleteFieldById($field->id);
165
        }
166
    }
167
168
    /**
169
     * Removes groups that where not imported.
170
     */
171
    private function deleteGroups()
172
    {
173
        $fieldsService = Craft::$app->fields;
174
        foreach ($this->groups as $group) {
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
175
            $fieldsService->deleteGroupById($group->id);
176
        }
177
    }
178
179
    /**
180
     * Removes fields and groups that where not imported.
181
     */
182
    private function deleteFieldsAndGroups()
183
    {
184
        $this->deleteFields();
185
        $this->deleteGroups();
186
    }
187
188
    /**
189
     * Creates new or updates existing group model.
190
     *
191
     * @param string $group
192
     *
193
     * @return FieldGroupModel
194
     */
195
    private function createFieldGroupModel($group)
196
    {
197
        $groupModel = (array_key_exists($group, $this->groups) ? $this->groups[$group] : new FieldGroupModel());
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
198
        $groupModel->name = $group;
199
200
        $this->saveFieldGroupModel($groupModel);
201
202
        return $groupModel;
203
    }
204
205
    /**
206
     * @param string $field
207
     *
208
     * @return FieldModel
209
     */
210
    private function getFieldModel($field)
211
    {
212
        return array_key_exists($field, $this->fields) ? $this->fields[$field] : new FieldModel();
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
213
    }
214
215
    /**
216
     * Validates field type, throw error when it's incorrect.
217
     *
218
     * @param FieldModel $field
219
     *
220
     * @throws \Exception
221
     */
222
    private function validateFieldModel(FieldModel $field)
223
    {
224
        if (!$field->getFieldType()) {
225
            $fieldType = $field->type;
226
            ($fieldType == 'Matrix')
227
                ? $this->addError("One of the field's types does not exist. Are you missing a plugin?")
0 ignored issues
show
Documentation Bug introduced by
The method addError does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
228
                : $this->addError("Field type '$fieldType' does not exist. Are you missing a plugin?");
0 ignored issues
show
Documentation Bug introduced by
The method addError does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
229
230
            throw new Exception('Failed to save field');
231
        }
232
    }
233
234
    /**
235
     * Import field group fields.
236
     *
237
     * @param array           $fieldDefinitions
238
     * @param FieldGroupModel $group
239
     * @param bool            $force
240
     *
241
     * @throws \Exception
242
     */
243
    private function importFields(array $fieldDefinitions, FieldGroupModel $group, $force = false)
244
    {
245
        $fieldFactory = $this->getFieldFactory();
0 ignored issues
show
Documentation Bug introduced by
The method getFieldFactory does not exist on object<NerdsAndCompany\Schematic\Services\Fields>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
246
247
        foreach ($fieldDefinitions as $fieldHandle => $fieldDef) {
248
            $field = $this->getFieldModel($fieldHandle);
249
            $schematicFieldModel = $fieldFactory->build($fieldDef['type']);
250
251
            if ($schematicFieldModel->getDefinition($field, true) === $fieldDef) {
252
                Craft::info('Skipping `{name}`, no changes detected', ['name' => $field->name], 'schematic');
0 ignored issues
show
Documentation introduced by
array('name' => $field->name) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Craft::info() has too many arguments starting with 'schematic'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
253
                continue;
254
            }
255
256
            Craft::info('Importing `{name}`', ['name' => $fieldDef['name']], 'schematic');
0 ignored issues
show
Documentation introduced by
array('name' => $fieldDef['name']) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Craft::info() has too many arguments starting with 'schematic'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
257
258
            $schematicFieldModel->populate($fieldDef, $field, $fieldHandle, $group, $force);
259
            $this->saveFieldModel($field);
260
        }
261
    }
262
263
    /**
264
     * Unset group and field data else $force flag will delete it.
265
     *
266
     * @param string $name
267
     * @param array  $definitions
268
     */
269
    private function unsetData($name, array $definitions)
270
    {
271
        if (array_key_exists($name, $this->groups)) {
0 ignored issues
show
Documentation introduced by
The property groups does not exist on object<NerdsAndCompany\Schematic\Services\Fields>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
272
            unset($this->groups[$name]);
273
            foreach ($definitions as $handle => $definition) {
274
                unset($this->fields[$handle]);
275
            }
276
        }
277
    }
278
279
    /**
280
     * Set global field context.
281
     */
282
    private function setGlobalContext()
283
    {
284
        Craft::$app->content->fieldContext = 'global';
285
        Craft::$app->content->contentTable = 'content';
286
    }
287
}
288