Completed
Push — master ( 7ac994...11aa35 )
by Hong
03:09
created

ReferenceTrait::deReferenceArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Reference;
16
17
use Phossa2\Shared\Message\Message;
18
use Phossa2\Shared\Exception\RuntimeException;
19
20
/**
21
 * ReferenceTrait
22
 *
23
 * Provides reference & dereference methods
24
 *
25
 * @package Phossa2\Shared
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ReferenceInterface
28
 * @version 2.0.4
29
 * @since   2.0.4 added
30
 */
31
trait ReferenceTrait
32
{
33
    /**
34
     * refernece start chars
35
     *
36
     * @var    string
37
     * @access protected
38
     */
39
    protected $ref_start = '${';
40
41
    /**
42
     * reference ending chars
43
     *
44
     * @var    string
45
     * @access protected
46
     */
47
    protected $ref_end = '}';
48
49
    /**
50
     * cached pattern to match
51
     *
52
     * @var    string
53
     * @access protected
54
     */
55
    protected $ref_pattern;
56
57
    /**
58
     * unresolved reference
59
     *
60
     * @var    array
61
     * @access protected
62
     */
63
    protected $unresolved = [];
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function setReference(
69
        /*# string */ $start,
70
        /*# string */ $end
71
    ) {
72
        $this->ref_start = $start;
73
        $this->ref_end = $end;
74
75
        // build pattern
76
        $s = preg_quote($start);
77
        $e = preg_quote($end);
78
        $this->ref_pattern = sprintf(
79
            "~(%s((?:(?!%s|%s).)+?)%s)~", $s, $s, $e, $e
80
        );
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function hasReference(
89
        /*# string */ $subject,
90
        array &$matched
91
    )/*# : bool */ {
92
        if (is_string($subject) &&
93
            false !== strpos($subject, $this->ref_start) &&
94
            preg_match($this->ref_pattern, $subject, $matched)
95
        ) {
96
            return true;
97
        }
98
        return false;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function deReference(/*# string */ $subject)
105
    {
106
        $loop = 0;
107
        $matched = [];
108
        while ($loop++ < 8 && $this->hasReference($subject, $matched)) {
109
            // resolving
110
            $val = $this->resolveReference($matched[2]);
111
112
            // full match
113
            if ($matched[1] === $subject) {
114
                return $val;
115
116
            // partial matched
117
            } elseif (is_string($val)) {
118
                $subject = str_replace($matched[1], $val, $subject);
119
120
            // malformed
121
            } else {
122
                throw new RuntimeException(
123
                    Message::get(Message::MSG_REF_MALFORMED, $subject),
124
                    Message::MSG_REF_MALFORMED
125
                );
126
            }
127
        }
128
        return $subject;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function deReferenceArray(array &$dataArray)
135
    {
136
        if (!is_array($dataArray)) {
137
            return;
138
        }
139
140
        foreach ($dataArray as &$data) {
141
            if (is_string($data)) {
142
                $data = $this->deReference($data);
143
            }
144
            $this->dereferenceArray($data);
145
        }
146
    }
147
148
    /**
149
     * Resolve the reference $name
150
     *
151
     * @param  string $name
152
     * @return mixed
153
     * @access protected
154
     */
155
    protected function resolveReference(/*# string */ $name)
156
    {
157
        // unresolved found
158
        if (isset($this->unresolved[$name])) {
159
            return $this->unresolved[$name];
160
        }
161
162
        // get the referenced value
163
        $val = $this->getReference($name);
164
165
        // not found
166
        if (is_null($val)) {
167
            $this->unresolved[$name] = $this->resolveUnknown($name);
168
            return $this->unresolved[$name];
169
170
        // found it
171
        } else {
172
            return $val;
173
        }
174
    }
175
176
    /**
177
     * For unknown reference $name
178
     *
179
     * @param  string $name
180
     * @return mixed
181
     * @access protected
182
     */
183
    abstract protected function resolveUnknown(/*# string */ $name);
184
185
    /**
186
     * The real resolving method. return NULL for unknown reference
187
     *
188
     * @param  string $name
189
     * @return mixed
190
     * @access protected
191
     */
192
    abstract protected function getReference(/*# string */ $name);
193
}
194