AttributeSet::hasKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 *
5
 * @author    Alex Kuperwood <[email protected]>
6
 * @copyright 2025 Alex Kuperwood
7
 * @license   https://opensource.org/license/mit  The MIT License
8
 */
9
declare(strict_types=1);
10
11
namespace Kuperwood\Eav;
12
13
use Kuperwood\Eav\Traits\ContainerTrait;
14
use Kuperwood\Eav\Traits\SingletonsTrait;
15
16
class AttributeSet
17
{
18
    use SingletonsTrait;
19
    use ContainerTrait;
20
21
    private $key;
22
    private string $name;
23
24
    /** @var AttributeContainer[] */
25
    private array $containers = [];
26
    private Entity $entity;
27
28 2
    public function getKey()
29
    {
30 2
        return $this->key;
31
    }
32
33 2
    public function setKey($key): self
34
    {
35 2
        $this->key = $key;
36
37 2
        return $this;
38
    }
39
40 2
    public function hasKey(): bool
41
    {
42 2
        return isset($this->key) && 0 !== $this->key;
43
    }
44
45 1
    public function getName(): string
46
    {
47 1
        return $this->name;
48
    }
49
50 1
    public function setName(string $name): self
51
    {
52 1
        $this->name = $name;
53
54 1
        return $this;
55
    }
56
57 1
    public function getEntity(): Entity
58
    {
59 1
        return $this->entity;
60
    }
61
62 1
    public function setEntity(Entity $entity): self
63
    {
64 1
        $this->entity = $entity;
65
66 1
        return $this;
67
    }
68
69 4
    public function fetchContainers(bool $force = false): self
70
    {
71 4
        if (!$this->hasKey()) {
72 1
            return $this;
73
        }
74 3
        if (!$force && $this->hasContainers()) {
75 1
            return $this;
76
        }
77 2
        $model = $this->makeAttributeSetModel();
78 2
        foreach ($model->findAttributes($this->getKey()) as $attribute) {
79 1
            $container = $this->makeAttributeContainer();
80 1
            $container->setAttributeSet($this);
81 1
            $container->makeAttributeSetAction();
82 1
            $action = $container->getAttributeSetAction();
83 1
            $action->initialize($attribute);
84 1
            $this->pushContainer($container);
85
        }
86
87 2
        return $this;
88
    }
89
90 1
    public function pushContainer(AttributeContainer $container): self
91
    {
92 1
        $this->containers[$container->getAttribute()->getName()] = $container;
93
94 1
        return $this;
95
    }
96
97 1
    public function resetContainers(): self
98
    {
99 1
        $this->containers = [];
100
101 1
        return $this;
102
    }
103
104 1
    public function getContainers(): array
105
    {
106 1
        return $this->containers;
107
    }
108
109 2
    public function getContainer(string $name): ?AttributeContainer
110
    {
111 2
        if (!$this->hasContainer($name)) {
112 1
            return null;
113
        }
114
115 1
        return $this->containers[$name];
116
    }
117
118 1
    public function hasContainer(string $name): bool
119
    {
120 1
        return array_key_exists($name, $this->containers);
121
    }
122
123 1
    public function hasContainers(): bool
124
    {
125 1
        return !empty($this->containers);
126
    }
127
}
128