|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\Testing; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
|
|
10
|
|
|
use function array_key_exists; |
|
11
|
|
|
use function array_replace_recursive; |
|
12
|
|
|
use function explode; |
|
13
|
|
|
use function str_starts_with; |
|
14
|
|
|
|
|
15
|
|
|
class Attributes implements Arrayable |
|
16
|
|
|
{ |
|
17
|
|
|
private const RELATION_MARKER = 'relations:'; |
|
18
|
|
|
private array $params; |
|
|
|
|
|
|
19
|
|
|
private array $attributes = []; |
|
20
|
|
|
/** @var self[] */ |
|
21
|
|
|
private array $relations = []; |
|
22
|
|
|
private string $relationName; |
|
23
|
|
|
private int $count; |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct(array $params, string $relationName = 'root', int $count = 1) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->params = $params; |
|
28
|
1 |
|
$this->relationName = $relationName; |
|
29
|
1 |
|
$this->count = $count; |
|
30
|
1 |
|
$this->parseParameters($params); |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function getAttributes(array $extraAttrs = []): array |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
1 |
|
return array_replace_recursive($this->attributes, $extraAttrs); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function hasAttribute(string $name): bool |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
return array_key_exists($name, $this->attributes); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getAttribute(string $name, $default = null) |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
return $this->attributes[$name] ?? $default; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setAttribute(string $name, $value): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->attributes[$name] = $value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function getCount(): int |
|
60
|
|
|
{ |
|
61
|
|
|
|
|
62
|
1 |
|
return $this->count; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getRelationName(): string |
|
66
|
|
|
{ |
|
67
|
|
|
|
|
68
|
|
|
return $this->relationName; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function getRelations(): array |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this->relations; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function hasRelation(string $name): bool |
|
77
|
|
|
{ |
|
78
|
1 |
|
return array_key_exists($name, $this->getRelations()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function getRelation(string $name): self |
|
82
|
|
|
{ |
|
83
|
1 |
|
if (! $this->hasRelation($name)) { |
|
84
|
|
|
throw new InvalidArgumentException('Relation "' . $name . '" does not found'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $this->getRelations()[$name]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function addRelation(string $name, int $count = 1, array $data = []): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->params = array_replace_recursive($this->params, [self::RELATION_MARKER . $name . ':' . $count => $data]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function toArray(array $extraParams = []): array |
|
96
|
|
|
{ |
|
97
|
|
|
return array_replace_recursive($this->params, $extraParams); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
private function parseParameters(array $params): void |
|
101
|
|
|
{ |
|
102
|
1 |
|
$attributes = []; |
|
103
|
1 |
|
$relations = []; |
|
104
|
1 |
|
foreach ($params as $key => $data) { |
|
105
|
1 |
|
if (str_starts_with($key, self::RELATION_MARKER)) { |
|
106
|
1 |
|
$ex = explode(':', $key); |
|
107
|
1 |
|
if (! isset($ex[1])) { |
|
108
|
|
|
throw new InvalidArgumentException('Relation is empty'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$relName = $ex[1]; |
|
112
|
1 |
|
$count = 1; |
|
113
|
1 |
|
if (isset($ex[2])) { |
|
114
|
1 |
|
$count = (int) $ex[2]; |
|
115
|
|
|
} |
|
116
|
1 |
|
$attrs = $params[$key]; |
|
117
|
|
|
|
|
118
|
1 |
|
$relations[$relName] = new self($attrs, $relName, $count); |
|
119
|
|
|
} else { |
|
120
|
1 |
|
$attributes[$key] = $data; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
$this->attributes = $attributes; |
|
125
|
1 |
|
$this->relations = $relations; |
|
126
|
1 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|