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(); |
|
|
|
|
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(); |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: