Completed
Push — master ( b5fb47...8bb9d4 )
by Nate
02:22
created

AbstractScope::childScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/transform/releases/latest
7
 * @license   https://github.com/flipbox/transform/blob/master/LICENSE
8
 */
9
10
namespace Flipbox\Transform\Scope;
11
12
use Flipbox\Transform\Transform\TransformInterface;
13
use Flipbox\Transform\Transformers\TransformerInterface;
14
use Flipbox\Transform\ParamBag;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
abstract class AbstractScope implements ScopeInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $scopeIdentifier;
26
27
    /**
28
     * @var TransformInterface
29
     */
30
    protected $transform;
31
32
    /**
33
     * @var array
34
     */
35
    protected $parentScopes = [];
36
37
    /**
38
     * Scope constructor.
39
     * @param TransformInterface $transform
40
     * @param string|null $scopeIdentifier
41
     * @param array $parentScopes
42
     */
43
    public function __construct(
44
        TransformInterface $transform,
45
        string $scopeIdentifier = null,
46
        array $parentScopes = []
47
    ) {
48
        $this->transform = $transform;
49
        $this->scopeIdentifier = $scopeIdentifier;
50
        $this->parentScopes = $parentScopes;
51
    }
52
53
    /**
54
     * @return TransformInterface
55
     */
56
    public function getTransform(): TransformInterface
57
    {
58
        return $this->transform;
59
    }
60
61
    /**
62
     * @param $key
63
     * @return ParamBag
64
     */
65
    public function getParams(string $key = null): ParamBag
66
    {
67
        return $this->getTransform()->getParams(
68
            $this->getIdentifier($key)
69
        );
70
    }
71
72
    /**
73
     * Get the current identifier.
74
     *
75
     * @return string|null
76
     */
77
    public function getScopeIdentifier()
78
    {
79
        return $this->scopeIdentifier;
80
    }
81
82
    /**
83
     * Get the unique identifier for this scope.
84
     *
85
     * @param string $appendIdentifier
86
     *
87
     * @return string
88
     */
89
    public function getIdentifier(string $appendIdentifier = null): string
90
    {
91
        return implode(
92
            '.',
93
            array_filter(array_merge(
94
                $this->parentScopes,
95
                [
96
                    $this->scopeIdentifier,
97
                    $appendIdentifier
98
                ]
99
            ))
100
        );
101
    }
102
103
    /**
104
     * Getter for parentScopes.
105
     *
106
     * @return array
107
     */
108
    public function getParentScopes(): array
109
    {
110
        return $this->parentScopes;
111
    }
112
113
    /**
114
     * @param TransformerInterface|callable $transformer
115
     * @param mixed $data
116
     * @return mixed
117
     */
118
    public function transform(callable $transformer, $data)
119
    {
120
        return $this->parseValue($transformer, $data);
121
    }
122
123
    /**
124
     * @param $val
125
     * @param $data
126
     * @param string|null $key
127
     * @return array|string|null
128
     */
129
    protected function parseValue($val, $data, string $key = null)
130
    {
131
        if (is_callable($val)) {
132
            return call_user_func_array($val, [$data, $this, $key]);
133
        }
134
135
        return $val;
136
    }
137
138
    /**
139
     * @param string $identifier
140
     * @return static
141
     */
142
    public function childScope(string $identifier)
143
    {
144
        $parentScopes = $this->getParentScopes();
145
        $parentScopes[] = $this->getScopeIdentifier();
146
147
        return new static(
148
            $this->getTransform(),
149
            $identifier,
150
            $parentScopes
151
        );
152
    }
153
154
    /**
155
     * Check, if this is the root scope.
156
     *
157
     * @return bool
158
     */
159
    protected function isRootScope(): bool
160
    {
161
        return empty($this->parentScopes);
162
    }
163
}
164