FieldConfig   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 35
c 1
b 0
f 0
dl 0
loc 95
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getGeneratorConfig() 0 3 1
A toArray() 0 3 1
A __construct() 0 9 1
A getRelationshipKind() 0 7 1
A getPropertyName() 0 3 1
A getName() 0 3 1
A getHandle() 0 3 1
A getMethodName() 0 3 1
A __toString() 0 3 1
A fromArray() 0 3 1
A getRelationshipTo() 0 7 1
A getMetadata() 0 3 1
A getEntityEvents() 0 16 1
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\SectionField\ValueObject;
15
16
use Assert\Assertion;
17
use Tardigrades\Helper\ArrayConverter;
18
19
final class FieldConfig implements ConfigWithHandleInterface
20
{
21
    /** @var array */
22
    private $fieldConfig;
23
24
    private function __construct(array $fieldConfig)
25
    {
26
        Assertion::keyIsset($fieldConfig, 'field', 'Config is not a field config');
27
        Assertion::keyIsset($fieldConfig['field'], 'name', 'No name in config');
28
        Assertion::keyIsset($fieldConfig['field'], 'handle', 'Field needs a handle');
29
        Assertion::notEmpty($fieldConfig['field'], 'name', 'Field has no value');
30
        Assertion::notEmpty($fieldConfig['field'], 'handle', 'Field needs a handle');
31
32
        $this->fieldConfig = $fieldConfig;
33
    }
34
35
    public function toArray(): array
36
    {
37
        return $this->fieldConfig;
38
    }
39
40
    public function getName(): Name
41
    {
42
        return Name::fromString($this->fieldConfig['field']['name']);
43
    }
44
45
    public function getHandle(): Handle
46
    {
47
        return Handle::fromString($this->fieldConfig['field']['handle']);
48
    }
49
50
    public function getMethodName(): MethodName
51
    {
52
        return MethodName::fromString($this->fieldConfig['field']['handle']);
53
    }
54
55
    public function getRelationshipKind(): string
56
    {
57
        Assertion::keyIsset($this->fieldConfig['field'], 'kind', 'No relationship kind defined');
58
        Assertion::notEmpty($this->fieldConfig['field']['kind'], 'Relationship kind is empty');
59
        Assertion::string($this->fieldConfig['field']['kind'], 'Relationship kind must be defined as string');
60
61
        return $this->fieldConfig['field']['kind'];
62
    }
63
64
    public function getRelationshipTo(): string
65
    {
66
        Assertion::keyIsset($this->fieldConfig['field'], 'to', 'No relationship to defined');
67
        Assertion::notEmpty($this->fieldConfig['field']['to'], 'Relationship to is empty');
68
        Assertion::string($this->fieldConfig['field']['to'], 'Relationship to must be defined as string');
69
70
        return $this->fieldConfig['field']['to'];
71
    }
72
73
    public function getPropertyName(): PropertyName
74
    {
75
        return PropertyName::fromString($this->fieldConfig['field']['handle']);
76
    }
77
78
    public function getEntityEvents(): array
79
    {
80
        Assertion::keyExists($this->fieldConfig['field'], 'generator', 'No generator entity events found');
81
        Assertion::keyExists($this->fieldConfig['field']['generator'], 'entity', 'No generator entity events found');
82
        Assertion::keyExists(
83
            $this->fieldConfig['field']['generator']['entity'],
84
            'event',
85
            'No generator entity events found'
86
        );
87
88
        Assertion::isArray(
89
            $this->fieldConfig['field']['generator']['entity']['event'],
90
            'Entity events should be an array of events you want a generator to run for.'
91
        );
92
93
        return $this->fieldConfig['field']['generator']['entity']['event'];
94
    }
95
96
    public function getGeneratorConfig(): GeneratorConfig
97
    {
98
        return GeneratorConfig::fromArray($this->fieldConfig['field']);
99
    }
100
101
    public function getMetadata(): FieldMetadata
102
    {
103
        return FieldMetadata::fromArray($this->fieldConfig['metadata']);
104
    }
105
106
    public function __toString(): string
107
    {
108
        return ArrayConverter::recursive($this->fieldConfig['field']);
109
    }
110
111
    public static function fromArray(array $fieldConfig): self
112
    {
113
        return new self($fieldConfig);
114
    }
115
}
116