Passed
Push — master ( b33de3...cb7df5 )
by Sebastian
02:24
created

Request_URLComparer::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the class {@see \AppUtils\Request_URLComparer}.
4
 * 
5
 * @package Application Utils
6
 * @subpackage Request
7
 * @see \AppUtils\Request_URLComparer
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * URL comparison class: used to check if URLs match
16
 * independently of the order of parameters, or fragments
17
 * and the like. Allows specifying parameters that should
18
 * be excluded from the comparison as well.
19
 * 
20
 * @package Application Utils
21
 * @subpackage Request
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Request_URLComparer
25
{
26
   /**
27
    * @var Request
28
    */
29
    protected $request;
30
    
31
   /**
32
    * @var string
33
    */
34
    protected $sourceURL;
35
    
36
   /**
37
    * @var string
38
    */
39
    protected $targetURL;
40
    
41
   /**
42
    * @var array
43
    */
44
    protected $limitParams = array();
45
    
46
   /**
47
    * @var bool
48
    */
49
    protected $isMatch = false;
50
    
51
   /**
52
    * @var bool
53
    */
54
    protected $ignoreFragment = true;
55
56
   /**
57
    * @var URLInfo
58
    */
59
    protected $sourceInfo;
60
    
61
   /**
62
    * @var URLInfo
63
    */
64
    protected $targetInfo;
65
    
66
    public function __construct(Request $request, string $sourceURL, string $targetURL)
67
    {
68
        $this->request = $request;
69
        $this->sourceURL = $sourceURL;
70
        $this->targetURL = $targetURL;
71
    }
72
    
73
    public function isMatch() : bool
74
    {
75
        $this->init();
76
        
77
        return $this->isMatch;
78
    }
79
    
80
    public function addLimitParam(string $name) : Request_URLComparer
81
    {
82
        if(!in_array($name, $this->limitParams)) {
83
            $this->limitParams[] = $name;
84
        }
85
        
86
        return $this;
87
    }
88
    
89
    public function addLimitParams(array $names) : Request_URLComparer
90
    {
91
        foreach($names as $name) {
92
            $this->addLimitParam($name);
93
        }
94
        
95
        return $this;
96
    }
97
    
98
    public function setIgnoreFragment(bool $ignore=true) : Request_URLComparer
99
    {
100
        $this->ignoreFragment = $ignore;
101
        return $this;
102
    }
103
    
104
    protected function init()
105
    {
106
        // so they are always in the same order. 
107
        sort($this->limitParams);
108
        
109
        $this->isMatch = $this->compare();
110
    }
111
    
112
    protected function compare()
113
    {
114
        $this->sourceInfo = parseURL($this->sourceURL);
115
        $this->targetInfo = parseURL($this->targetURL);
116
        
117
        $keys = array(
118
            'scheme',
119
            'host',
120
            'path',
121
            'query' 
122
        );
123
        
124
        if(!$this->ignoreFragment) {
125
            $keys[] = 'fragment';
126
        }
127
        
128
        foreach($keys as $key)
129
        {
130
            if(!$this->compareKey($key)) {
131
                return false;
132
            }
133
        }
134
        
135
        return true;
136
    }
137
    
138
    protected function compareKey(string $key) : bool
139
    {
140
        $sVal = $this->sourceInfo[$key];
141
        $tVal = $this->targetInfo[$key];
142
        
143
        $filter = 'filter_'.$key;
144
        
145
        if(method_exists($this, $filter)) 
146
        {
147
            $sVal = $this->$filter($sVal);
148
            $tVal = $this->$filter($tVal);
149
        }
150
        
151
        return $sVal === $tVal;
152
    }
153
    
154
    protected function filter_path(string $path) : string
155
    {
156
        // fix double slashes in URLs
157
        while(stristr($path, '//')) {
158
            $path = str_replace('//', '/', $path);
159
        }
160
        
161
        return ltrim($path, '/');
162
    }
163
    
164
    protected function filter_query(string $query) : string
165
    {
166
        if(empty($query)) {
167
            return '';
168
        }
169
        
170
        $params = ConvertHelper::parseQueryString($query);
171
        
172
        $params = $this->limitParams($params);
173
        
174
        ksort($params);
175
        
176
        return serialize($params);
177
    }
178
    
179
    protected function limitParams(array $params) : array
180
    {
181
        if(empty($this->limitParams)) {
182
            return $params;
183
        }
184
        
185
        $keep = array();
186
        
187
        foreach($this->limitParams as $name)
188
        {
189
            if(isset($params[$name])) {
190
                $keep[$name] = $params[$name];
191
            }
192
        }
193
        
194
        return $keep;
195
    }
196
}
197