Passed
Push — master ( abee1f...edc958 )
by Kirill
03:08
created

Delete::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Config\Patch;
13
14
use Spiral\Config\Exception\DotNotFoundException;
15
use Spiral\Config\Patch\Traits\DotTrait;
16
use Spiral\Config\PatchInterface;
17
18
final class Delete implements PatchInterface
19
{
20
    use DotTrait;
0 ignored issues
show
Bug introduced by
The trait Spiral\Config\Patch\Traits\DotTrait requires the property $prefix which is not provided by Spiral\Config\Patch\Delete.
Loading history...
21
22
    /** @var string */
23
    private $position;
24
25
    /** @var null|string */
26
    private $key;
27
28
    /** @var mixed */
29
    private $value;
30
31
    /**
32
     * @param string      $position
33
     * @param null|string $key
34
     * @param mixed       $value
35
     */
36
    public function __construct(string $position, ?string $key, $value = null)
37
    {
38
        $this->position = $position === '.' ? '' : $position;
39
        $this->key = $key;
40
        $this->value = $value;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function patch(array $config): array
47
    {
48
        try {
49
            $target = &$this->dotGet($config, $this->position);
50
51
            if ($this->key !== null) {
52
                unset($target[$this->key]);
53
            } else {
54
                foreach ($target as $key => $value) {
55
                    if ($value === $this->value) {
56
                        unset($target[$key]);
57
                        break;
58
                    }
59
                }
60
            }
61
        } catch (DotNotFoundException $e) {
62
            // doing nothing when section not found
63
        }
64
65
        return $config;
66
    }
67
}
68