Passed
Push — master ( de30cb...30b342 )
by Korotkov
08:04 queued 13s
created

Interpreter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author  : Jagepard <[email protected]>
7
 * @license https://mit-license.org/ MIT
8
 */
9
10
namespace Behavioral\Interpreter;
11
12
class Interpreter implements InterpreterInterface
13
{
14
    private array $registryData;
15
16
    public function __construct(RegistryInterface $registry)
17
    {
18
        $this->registryData = $registry->getData();
19
    }
20
21
    /**
22
     * @param string $input
23
     * @return array
24
     */
25
    public function interpret(string $input): array
26
    {
27
        $input = explode(' ', $input);
28
29
        foreach ($input as $value) {
30
            if (is_numeric($value)) {
31
                return $this->getDataFromRegistry($input, $this->registryData[$value - 1]);
32
            }
33
        }
34
35
        throw new \InvalidArgumentException("No id specified");
36
    }
37
38
    /**
39
     * @param array $input
40
     * @param AlbumInterface $item
41
     * @return array
42
     */
43
    private function getDataFromRegistry(array $input, AlbumInterface $item): array
44
    {
45
        $dataArray = [];
46
47
        foreach ($input as $value) {
48
            if ($value === "author") {
49
                $dataArray["author"] = $item->getAuthor();
50
            }
51
52
            if ($value === "album") {
53
                $dataArray["album"] = $item->getAlbum();
54
            }
55
        }
56
57
        return $dataArray;
58
    }
59
}
60