ApplicationConfig::getHandle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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 ApplicationConfig implements ConfigWithHandleInterface
20
{
21
    /** @var array */
22
    private $applicationConfig;
23
24
    private function __construct(array $applicationConfig)
25
    {
26
        Assertion::keyIsset($applicationConfig, 'application', 'Config is not a application config');
27
        Assertion::keyIsset($applicationConfig['application'], 'name', 'The name for the application is required.');
28
        Assertion::keyIsset($applicationConfig['application'], 'handle', 'The handle for the application is required.');
29
        Assertion::keyIsset($applicationConfig['application'], 'languages', 'At least define one language in an array');
30
        Assertion::isArray($applicationConfig['application']['languages'], 'Languages should contain an array');
31
32
        $this->applicationConfig = $applicationConfig;
33
    }
34
35
    public function getHandle(): Handle
36
    {
37
        return Handle::fromString($this->applicationConfig['application']['handle']);
38
    }
39
40
    public function toArray(): array
41
    {
42
        return $this->applicationConfig;
43
    }
44
45
    public function __toString(): string
46
    {
47
        return ArrayConverter::recursive($this->applicationConfig);
48
    }
49
50
    public static function fromArray(array $applicationConfig): self
51
    {
52
        return new self($applicationConfig);
53
    }
54
}
55