Completed
Push — master ( cc06f5...b0a397 )
by Korotkov
02:50 queued 01:19
created

Interpreter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 14
dl 0
loc 40
rs 10
c 7
b 0
f 1
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A interpret() 0 22 6
A addItem() 0 3 1
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
    /**
15
     * @var array
16
     */
17
    private $depository;
18
19
    /**
20
     * @param ItemInterface $item
21
     */
22
    public function addItem(ItemInterface $item): void
23
    {
24
        $this->depository[] = $item;
25
    }
26
27
    /**
28
     * @param string $input
29
     */
30
    public function interpret(string $input): void
31
    {
32
        $item  = null;
33
        $input = explode(" ", $input);
34
35
        foreach ($input as $value) {
36
            if (is_numeric($value)) {
37
                $item = $this->depository[$value - 1];
38
            }
39
        }
40
41
        foreach ($input as $value) {
42
            if ($value == "album") {
43
                printf("%s ", $item->getName());
44
            }
45
46
            if ($value == "author") {
47
                printf("%s", $item->getAuthor());
48
            }
49
        }
50
51
        printf("%s", PHP_EOL);
52
    }
53
}
54