Completed
Push — master ( 958707...b33915 )
by Hong
02:14
created

ReferenceTrait::deReference()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 13
rs 9.2
cc 4
eloc 9
nc 3
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
        $matched = [];
107
        while ($this->hasReference($subject, $matched)) {
108
            $val = $this->resolveReference($matched[2]);
109
            if (is_string($val) && $val !== $subject) {
110
                $subject = str_replace($matched[1], $val, $subject);
111
            } else {
112
                return $val;
113
            }
114
        }
115
        return $subject;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function deReferenceArray(array &$dataArray)
122
    {
123
        if (!is_array($dataArray)) {
124
            return;
125
        }
126
127
        foreach ($dataArray as &$data) {
128
            if (is_string($data)) {
129
                $data = $this->deReference($data);
130
            }
131
            $this->dereferenceArray($data);
132
        }
133
    }
134
135
    /**
136
     * Resolve the reference $name
137
     *
138
     * @param  string $name
139
     * @return mixed
140
     * @access protected
141
     */
142
    protected function resolveReference(/*# string */ $name)
143
    {
144
        // unresolved found
145
        if (isset($this->unresolved[$name])) {
146
            return $this->unresolved[$name];
147
        }
148
149
        // get the referenced value
150
        $val = $this->getReference($name);
151
152
        // not found
153
        if (is_null($val)) {
154
            $this->unresolved[$name] = $this->resolveUnknown($name);
155
            return $this->unresolved[$name];
156
157
        // found it
158
        } else {
159
            return $val;
160
        }
161
    }
162
163
    /**
164
     * For unknown reference $name
165
     *
166
     * @param  string $name
167
     * @return mixed
168
     * @access protected
169
     */
170
    abstract protected function resolveUnknown(/*# string */ $name);
171
172
    /**
173
     * The real resolving method. return NULL for unknown reference
174
     *
175
     * @param  string $name
176
     * @return mixed
177
     * @access protected
178
     */
179
    abstract protected function getReference(/*# string */ $name);
180
}
181