FieldCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Tacone\Bees\Collection;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Tacone\Bees\Base\CompositeTrait;
7
use Tacone\Bees\Base\DelegatedArrayTrait;
8
use Tacone\Bees\Helper\ArrayHelper;
9
10
class FieldCollection implements \Countable, \IteratorAggregate, \ArrayAccess, Arrayable
11
{
12
    use DelegatedArrayTrait;
13
    use CompositeTrait;
14
15
    public function __construct()
16
    {
17
        $this->storage = new \ArrayObject();
0 ignored issues
show
Bug introduced by
The property storage does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
    }
19
20
    protected function compositeTraitGetChildren()
21
    {
22
        $children = [];
23
        foreach ($this as $name => $field) {
24
            $children[$name] = $field;
25
        }
26
27
        return $children;
28
    }
29
30
    public function add($object)
31
    {
32
        return $this->storage[$object->name()] = $object;
33
    }
34
35
    /**
36
     * @param string $name
37
     */
38
    public function get($name)
39
    {
40
        return $this->storage[$name];
41
    }
42
43
    public function contains($name)
44
    {
45
        if (is_object($name)) {
46
            $name = $name->name();
47
        }
48
49
        return $this->storage->offsetExists($name);
50
    }
51
52
    protected function getDelegatedStorage()
53
    {
54
        return $this->storage;
55
    }
56
57
    /**
58
     * Get the fields value as an associative array.
59
     * By default a nested array is returned.
60
     * Passing true as the first parameter, a flat
61
     * array will be returned, with dotted offsets
62
     * as the keys.
63
     *
64
     * @param bool $flat
65
     *
66
     * @return array
67
     */
68
    public function toArray($flat = false)
69
    {
70
        $array = $this->cast();
0 ignored issues
show
Documentation Bug introduced by
The method cast does not exist on object<Tacone\Bees\Collection\FieldCollection>? 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...
71
        if ($flat) {
72
            return $array;
73
        }
74
75
        return ArrayHelper::undot($array);
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    public function validate()
82
    {
83
        $validator = \Validator::make(
84
            $this->toArray(true),
85
            $this->rules()
0 ignored issues
show
Documentation Bug introduced by
The method rules does not exist on object<Tacone\Bees\Collection\FieldCollection>? 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...
86
        );
87
        $names = array();
88
        foreach ($this as $field) {
89
            $names[$field->name()] = '"'.$field->name().'"';
90
        }
91
        $validator->setAttributeNames($names);
92
        foreach ($validator->errors()->getMessages() as $name => $messages) {
93
            $this[$name]->errors($messages);
94
        }
95
96
        return !$validator->fails();
97
    }
98
99
    /**
100
     * @param \Tacone\DataSource\AbstractDataSource $source
101
     */
102
    public function from($source)
103
    {
104
        foreach ($this as $name => $field) {
105
            if (isset($source[$name])) {
106
                $field->value($source[$name]);
107
            }
108
        }
109
    }
110
}
111