Test Failed
Push — master ( e3cf61...f26703 )
by Alain
01:54
created
src/View/View/AbstractView.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -167,14 +167,14 @@  discard block
 block discarded – undo
167 167
      * @param string $behavior Behavior to use for adapting the context.
168 168
      * @return View
169 169
      */
170
-    public function addToContext( string $key, $value, string $behavior ): View
170
+    public function addToContext(string $key, $value, string $behavior): View
171 171
     {
172 172
         switch ($behavior) {
173 173
             case View::REPLACE:
174 174
                 $this->_context_[$key] = $value;
175 175
                 return $this;
176 176
             case View::MERGE:
177
-                if(array_key_exists($key, $this->_context_)) {
177
+                if (array_key_exists($key, $this->_context_)) {
178 178
                     $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]);
179 179
                     return $this;
180 180
                 }
@@ -187,13 +187,13 @@  discard block
 block discarded – undo
187 187
                 $this->_context_[$key] = $value;
188 188
                 return $this;
189 189
             case View::REPLACE_ONLY:
190
-                if (! array_key_exists($key, $this->_context_)) {
190
+                if ( ! array_key_exists($key, $this->_context_)) {
191 191
                     return $this;
192 192
                 }
193 193
                 $this->_context_[$key] = $value;
194 194
                 return $this;
195 195
             case View::MERGE_ONLY:
196
-                if (! array_key_exists($key, $this->_context_)) {
196
+                if ( ! array_key_exists($key, $this->_context_)) {
197 197
                     return $this;
198 198
                 }
199 199
                 $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]);
Please login to merge, or discard this patch.
Indentation   +209 added lines, -209 removed lines patch added patch discarded remove patch
@@ -31,233 +31,233 @@
 block discarded – undo
31 31
 abstract class AbstractView implements View
32 32
 {
33 33
 
34
-    /**
35
-     * URI of the view.
36
-     *
37
-     * The underscores are used to prevent accidental use of these properties from within the rendering closure.
38
-     *
39
-     * @since 0.1.0
40
-     *
41
-     * @var string
42
-     */
43
-    protected $_uri_;
34
+	/**
35
+	 * URI of the view.
36
+	 *
37
+	 * The underscores are used to prevent accidental use of these properties from within the rendering closure.
38
+	 *
39
+	 * @since 0.1.0
40
+	 *
41
+	 * @var string
42
+	 */
43
+	protected $_uri_;
44 44
 
45
-    /**
46
-     * Engine to use for the view.
47
-     *
48
-     * The underscores are used to prevent accidental use of these properties from within the rendering closure.
49
-     *
50
-     * @since 0.1.0
51
-     *
52
-     * @var Engine
53
-     */
54
-    protected $_engine_;
45
+	/**
46
+	 * Engine to use for the view.
47
+	 *
48
+	 * The underscores are used to prevent accidental use of these properties from within the rendering closure.
49
+	 *
50
+	 * @since 0.1.0
51
+	 *
52
+	 * @var Engine
53
+	 */
54
+	protected $_engine_;
55 55
 
56
-    /**
57
-     * ViewBuilder instance.
58
-     *
59
-     * The underscores are used to prevent accidental use of these properties from within the rendering closure.
60
-     *
61
-     * @since 0.2.0
62
-     *
63
-     * @var ViewBuilder
64
-     */
65
-    protected $_builder_;
56
+	/**
57
+	 * ViewBuilder instance.
58
+	 *
59
+	 * The underscores are used to prevent accidental use of these properties from within the rendering closure.
60
+	 *
61
+	 * @since 0.2.0
62
+	 *
63
+	 * @var ViewBuilder
64
+	 */
65
+	protected $_builder_;
66 66
 
67
-    /**
68
-     * The context with which the view will be rendered.
69
-     *
70
-     * The underscores are used to prevent accidental use of these properties from within the rendering closure.
71
-     *
72
-     * @since 0.4.0
73
-     *
74
-     * @var array
75
-     */
76
-    protected $_context_;
67
+	/**
68
+	 * The context with which the view will be rendered.
69
+	 *
70
+	 * The underscores are used to prevent accidental use of these properties from within the rendering closure.
71
+	 *
72
+	 * @since 0.4.0
73
+	 *
74
+	 * @var array
75
+	 */
76
+	protected $_context_;
77 77
 
78
-    /**
79
-     * Instantiate an AbstractView object.
80
-     *
81
-     * @since 0.1.0
82
-     *
83
-     * @param string      $uri         URI for the view.
84
-     * @param Engine      $engine      Engine to use for the view.
85
-     * @param ViewBuilder $viewBuilder View builder instance to use.
86
-     * @param array       $context     Initial context to use.
87
-     */
88
-    public function __construct(string $uri, Engine $engine, ViewBuilder $viewBuilder = null, array $context = [])
89
-    {
90
-        $this->_uri_     = $uri;
91
-        $this->_engine_  = $engine;
92
-        $this->_builder_ = $viewBuilder ?? Views::getViewBuilder();
93
-        $this->_context_ = $context;
94
-    }
78
+	/**
79
+	 * Instantiate an AbstractView object.
80
+	 *
81
+	 * @since 0.1.0
82
+	 *
83
+	 * @param string      $uri         URI for the view.
84
+	 * @param Engine      $engine      Engine to use for the view.
85
+	 * @param ViewBuilder $viewBuilder View builder instance to use.
86
+	 * @param array       $context     Initial context to use.
87
+	 */
88
+	public function __construct(string $uri, Engine $engine, ViewBuilder $viewBuilder = null, array $context = [])
89
+	{
90
+		$this->_uri_     = $uri;
91
+		$this->_engine_  = $engine;
92
+		$this->_builder_ = $viewBuilder ?? Views::getViewBuilder();
93
+		$this->_context_ = $context;
94
+	}
95 95
 
96
-    /**
97
-     * Render the view.
98
-     *
99
-     * @since 0.1.0
100
-     *
101
-     * @param array $context Optional. The context in which to render the view.
102
-     * @param bool  $echo    Optional. Whether to echo the output immediately. Defaults to false.
103
-     *
104
-     * @return string Rendered HTML.
105
-     * @throws FailedToProcessConfigException If the Config could not be processed.
106
-     */
107
-    public function render(array $context = [], bool $echo = false): string
108
-    {
109
-        $this->assimilateContext($context);
96
+	/**
97
+	 * Render the view.
98
+	 *
99
+	 * @since 0.1.0
100
+	 *
101
+	 * @param array $context Optional. The context in which to render the view.
102
+	 * @param bool  $echo    Optional. Whether to echo the output immediately. Defaults to false.
103
+	 *
104
+	 * @return string Rendered HTML.
105
+	 * @throws FailedToProcessConfigException If the Config could not be processed.
106
+	 */
107
+	public function render(array $context = [], bool $echo = false): string
108
+	{
109
+		$this->assimilateContext($context);
110 110
 
111
-        $closure = Closure::bind(
112
-            $this->_engine_->getRenderCallback($this->_uri_, $context),
113
-            $this,
114
-            static::class
115
-        );
111
+		$closure = Closure::bind(
112
+			$this->_engine_->getRenderCallback($this->_uri_, $context),
113
+			$this,
114
+			static::class
115
+		);
116 116
 
117
-        $output = $closure();
117
+		$output = $closure();
118 118
 
119
-        if ($echo) {
120
-            echo $output;
121
-        }
119
+		if ($echo) {
120
+			echo $output;
121
+		}
122 122
 
123
-        return $output;
124
-    }
123
+		return $output;
124
+	}
125 125
 
126
-    /**
127
-     * Render a partial view (or section) for a given URI.
128
-     *
129
-     * @since 0.2.0
130
-     *
131
-     * @param string      $view    View identifier to create a view for.
132
-     * @param array       $context Optional. The context in which to render the view.
133
-     * @param string|null $type    Type of view to create.
134
-     *
135
-     * @return string Rendered HTML content.
136
-     * @throws FailedToProcessConfigException If the Config could not be processed.
137
-     * @throws FailedToInstantiateView If the View could not be instantiated.
138
-     */
139
-    public function section(string $view, array $context = null, $type = null): string
140
-    {
141
-        $context = (null === $context)
142
-            ? $this->_context_
143
-            : array_merge($this->_context_, $context);
126
+	/**
127
+	 * Render a partial view (or section) for a given URI.
128
+	 *
129
+	 * @since 0.2.0
130
+	 *
131
+	 * @param string      $view    View identifier to create a view for.
132
+	 * @param array       $context Optional. The context in which to render the view.
133
+	 * @param string|null $type    Type of view to create.
134
+	 *
135
+	 * @return string Rendered HTML content.
136
+	 * @throws FailedToProcessConfigException If the Config could not be processed.
137
+	 * @throws FailedToInstantiateView If the View could not be instantiated.
138
+	 */
139
+	public function section(string $view, array $context = null, $type = null): string
140
+	{
141
+		$context = (null === $context)
142
+			? $this->_context_
143
+			: array_merge($this->_context_, $context);
144 144
 
145
-        $viewObject = $this->_builder_->create($view, $type);
145
+		$viewObject = $this->_builder_->create($view, $type);
146 146
 
147
-        return $viewObject->render($context);
148
-    }
147
+		return $viewObject->render($context);
148
+	}
149 149
 
150
-    /**
151
-     * Get the entire array of contextual data.
152
-     *
153
-     * @since 0.4.0
154
-     *
155
-     * @return array Array of contextual data.
156
-     */
157
-    public function getContext(): array
158
-    {
159
-        return $this->_context_;
160
-    }
150
+	/**
151
+	 * Get the entire array of contextual data.
152
+	 *
153
+	 * @since 0.4.0
154
+	 *
155
+	 * @return array Array of contextual data.
156
+	 */
157
+	public function getContext(): array
158
+	{
159
+		return $this->_context_;
160
+	}
161 161
 
162
-    /**
163
-     * Add information to the context.
164
-     *
165
-     * @param string $key      Context key to add.
166
-     * @param mixed  $value    Value to add under the given key.
167
-     * @param string $behavior Behavior to use for adapting the context.
168
-     * @return View
169
-     */
170
-    public function addToContext( string $key, $value, string $behavior ): View
171
-    {
172
-        switch ($behavior) {
173
-            case View::REPLACE:
174
-                $this->_context_[$key] = $value;
175
-                return $this;
176
-            case View::MERGE:
177
-                if(array_key_exists($key, $this->_context_)) {
178
-                    $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]);
179
-                    return $this;
180
-                }
181
-                $this->_context_[$key] = $value;
182
-                return $this;
183
-            case View::ADD_ONLY:
184
-                if (array_key_exists($key, $this->_context_)) {
185
-                    return $this;
186
-                }
187
-                $this->_context_[$key] = $value;
188
-                return $this;
189
-            case View::REPLACE_ONLY:
190
-                if (! array_key_exists($key, $this->_context_)) {
191
-                    return $this;
192
-                }
193
-                $this->_context_[$key] = $value;
194
-                return $this;
195
-            case View::MERGE_ONLY:
196
-                if (! array_key_exists($key, $this->_context_)) {
197
-                    return $this;
198
-                }
199
-                $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]);
200
-                return $this;
201
-            default:
202
-                throw new InvalidContextAddingBehavior(
203
-                    sprintf(
204
-                        _('Invalid behavior "%s" for adding to the context of view "%s".'),
205
-                        $key,
206
-                        $this->_uri_
207
-                    )
208
-                );
209
-        }
210
-    }
162
+	/**
163
+	 * Add information to the context.
164
+	 *
165
+	 * @param string $key      Context key to add.
166
+	 * @param mixed  $value    Value to add under the given key.
167
+	 * @param string $behavior Behavior to use for adapting the context.
168
+	 * @return View
169
+	 */
170
+	public function addToContext( string $key, $value, string $behavior ): View
171
+	{
172
+		switch ($behavior) {
173
+			case View::REPLACE:
174
+				$this->_context_[$key] = $value;
175
+				return $this;
176
+			case View::MERGE:
177
+				if(array_key_exists($key, $this->_context_)) {
178
+					$this->_context_ = array_merge_recursive($this->_context_, [$key => $value]);
179
+					return $this;
180
+				}
181
+				$this->_context_[$key] = $value;
182
+				return $this;
183
+			case View::ADD_ONLY:
184
+				if (array_key_exists($key, $this->_context_)) {
185
+					return $this;
186
+				}
187
+				$this->_context_[$key] = $value;
188
+				return $this;
189
+			case View::REPLACE_ONLY:
190
+				if (! array_key_exists($key, $this->_context_)) {
191
+					return $this;
192
+				}
193
+				$this->_context_[$key] = $value;
194
+				return $this;
195
+			case View::MERGE_ONLY:
196
+				if (! array_key_exists($key, $this->_context_)) {
197
+					return $this;
198
+				}
199
+				$this->_context_ = array_merge_recursive($this->_context_, [$key => $value]);
200
+				return $this;
201
+			default:
202
+				throw new InvalidContextAddingBehavior(
203
+					sprintf(
204
+						_('Invalid behavior "%s" for adding to the context of view "%s".'),
205
+						$key,
206
+						$this->_uri_
207
+					)
208
+				);
209
+		}
210
+	}
211 211
 
212
-    /**
213
-     * Associate a view builder with this view.
214
-     *
215
-     * @since 0.2.0
216
-     *
217
-     * @param ViewBuilder $builder
218
-     *
219
-     * @return View
220
-     */
221
-    public function setBuilder(ViewBuilder $builder): View
222
-    {
223
-        $this->_builder_ = $builder;
212
+	/**
213
+	 * Associate a view builder with this view.
214
+	 *
215
+	 * @since 0.2.0
216
+	 *
217
+	 * @param ViewBuilder $builder
218
+	 *
219
+	 * @return View
220
+	 */
221
+	public function setBuilder(ViewBuilder $builder): View
222
+	{
223
+		$this->_builder_ = $builder;
224 224
 
225
-        return $this;
226
-    }
225
+		return $this;
226
+	}
227 227
 
228
-    /**
229
-     * Assimilate the context to make it available as properties.
230
-     *
231
-     * @since 0.2.0
232
-     *
233
-     * @param array $context Context to assimilate.
234
-     */
235
-    protected function assimilateContext(array $context = [])
236
-    {
237
-        $this->_context_ = $context;
238
-        foreach ($context as $key => $value) {
239
-            $this->$key = $value;
240
-        }
241
-    }
228
+	/**
229
+	 * Assimilate the context to make it available as properties.
230
+	 *
231
+	 * @since 0.2.0
232
+	 *
233
+	 * @param array $context Context to assimilate.
234
+	 */
235
+	protected function assimilateContext(array $context = [])
236
+	{
237
+		$this->_context_ = $context;
238
+		foreach ($context as $key => $value) {
239
+			$this->$key = $value;
240
+		}
241
+	}
242 242
 
243
-    /**
244
-     * Turn invokable objects as properties into methods of the view.
245
-     *
246
-     * @param string $method    Method that was called on the view.
247
-     * @param array  $arguments Array of arguments that were used.
248
-     * @return mixed Return value of the invokable object.
249
-     */
250
-    public function __call($method, $arguments) {
251
-        if ( ! property_exists($this, $method)
252
-             || ! is_callable($this->$method)) {
253
-            trigger_error(
254
-                "Call to undefined method {$method} on a view.",
255
-                E_USER_ERROR
256
-            );
257
-        }
243
+	/**
244
+	 * Turn invokable objects as properties into methods of the view.
245
+	 *
246
+	 * @param string $method    Method that was called on the view.
247
+	 * @param array  $arguments Array of arguments that were used.
248
+	 * @return mixed Return value of the invokable object.
249
+	 */
250
+	public function __call($method, $arguments) {
251
+		if ( ! property_exists($this, $method)
252
+			 || ! is_callable($this->$method)) {
253
+			trigger_error(
254
+				"Call to undefined method {$method} on a view.",
255
+				E_USER_ERROR
256
+			);
257
+		}
258 258
 
259
-        $callable = $this->$method;
259
+		$callable = $this->$method;
260 260
 
261
-        return $callable(...$arguments);
262
-    }
261
+		return $callable(...$arguments);
262
+	}
263 263
 }
Please login to merge, or discard this patch.
src/View/View/NullView.php 2 patches
Indentation   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -26,86 +26,86 @@
 block discarded – undo
26 26
 class NullView implements View, NullFindable
27 27
 {
28 28
 
29
-    /**
30
-     * Check whether the Findable can handle an individual criterion.
31
-     *
32
-     * @since 0.1.0
33
-     *
34
-     * @param mixed $criterion Criterion to check.
35
-     *
36
-     * @return bool Whether the Findable can handle the criterion.
37
-     */
38
-    public function canHandle($criterion): bool
39
-    {
40
-        return true;
41
-    }
29
+	/**
30
+	 * Check whether the Findable can handle an individual criterion.
31
+	 *
32
+	 * @since 0.1.0
33
+	 *
34
+	 * @param mixed $criterion Criterion to check.
35
+	 *
36
+	 * @return bool Whether the Findable can handle the criterion.
37
+	 */
38
+	public function canHandle($criterion): bool
39
+	{
40
+		return true;
41
+	}
42 42
 
43
-    /**
44
-     * Render the view.
45
-     *
46
-     * @since 0.1.0
47
-     *
48
-     * @param array $context Optional. The context in which to render the view.
49
-     * @param bool  $echo    Optional. Whether to echo the output immediately. Defaults to false.
50
-     *
51
-     * @return string Rendered HTML.
52
-     */
53
-    public function render(array $context = [], bool $echo = false): string
54
-    {
55
-        return '';
56
-    }
43
+	/**
44
+	 * Render the view.
45
+	 *
46
+	 * @since 0.1.0
47
+	 *
48
+	 * @param array $context Optional. The context in which to render the view.
49
+	 * @param bool  $echo    Optional. Whether to echo the output immediately. Defaults to false.
50
+	 *
51
+	 * @return string Rendered HTML.
52
+	 */
53
+	public function render(array $context = [], bool $echo = false): string
54
+	{
55
+		return '';
56
+	}
57 57
 
58
-    /**
59
-     * Render a partial view (or section) for a given URI.
60
-     *
61
-     * @since 0.2.0
62
-     *
63
-     * @param string      $view    View identifier to create a view for.
64
-     * @param array       $context Optional. The context in which to render the view.
65
-     * @param string|null $type    Type of view to create.
66
-     *
67
-     * @return string Rendered HTML content.
68
-     */
69
-    public function section(string $view, array $context = [], $type = null): string
70
-    {
71
-        return '';
72
-    }
58
+	/**
59
+	 * Render a partial view (or section) for a given URI.
60
+	 *
61
+	 * @since 0.2.0
62
+	 *
63
+	 * @param string      $view    View identifier to create a view for.
64
+	 * @param array       $context Optional. The context in which to render the view.
65
+	 * @param string|null $type    Type of view to create.
66
+	 *
67
+	 * @return string Rendered HTML content.
68
+	 */
69
+	public function section(string $view, array $context = [], $type = null): string
70
+	{
71
+		return '';
72
+	}
73 73
 
74
-    /**
75
-     * Get the entire array of contextual data.
76
-     *
77
-     * @since 0.4.0
78
-     *
79
-     * @return array Array of contextual data.
80
-     */
81
-    public function getContext(): array
82
-    {
83
-        return [];
84
-    }
74
+	/**
75
+	 * Get the entire array of contextual data.
76
+	 *
77
+	 * @since 0.4.0
78
+	 *
79
+	 * @return array Array of contextual data.
80
+	 */
81
+	public function getContext(): array
82
+	{
83
+		return [];
84
+	}
85 85
 
86
-    /**
87
-     * Add information to the context.
88
-     *
89
-     * @param string $key      Context key to add.
90
-     * @param mixed  $value    Value to add under the given key.
91
-     * @param string $behavior Behavior to use for adapting the context.
92
-     * @return View
93
-     */
94
-    public function addToContext( string $key, $value, string $behavior ): View {
95
-        return $this;
96
-    }
86
+	/**
87
+	 * Add information to the context.
88
+	 *
89
+	 * @param string $key      Context key to add.
90
+	 * @param mixed  $value    Value to add under the given key.
91
+	 * @param string $behavior Behavior to use for adapting the context.
92
+	 * @return View
93
+	 */
94
+	public function addToContext( string $key, $value, string $behavior ): View {
95
+		return $this;
96
+	}
97 97
 
98
-    /**
99
-     * Associate a view builder with this view.
100
-     *
101
-     * @since 0.2.0
102
-     *
103
-     * @param ViewBuilder $builder
104
-     *
105
-     * @return View
106
-     */
107
-    public function setBuilder(ViewBuilder $builder): View
108
-    {
109
-        return $this;
110
-    }
98
+	/**
99
+	 * Associate a view builder with this view.
100
+	 *
101
+	 * @since 0.2.0
102
+	 *
103
+	 * @param ViewBuilder $builder
104
+	 *
105
+	 * @return View
106
+	 */
107
+	public function setBuilder(ViewBuilder $builder): View
108
+	{
109
+		return $this;
110
+	}
111 111
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -91,7 +91,7 @@
 block discarded – undo
91 91
      * @param string $behavior Behavior to use for adapting the context.
92 92
      * @return View
93 93
      */
94
-    public function addToContext( string $key, $value, string $behavior ): View {
94
+    public function addToContext(string $key, $value, string $behavior): View {
95 95
         return $this;
96 96
     }
97 97
 
Please login to merge, or discard this patch.
src/View/Location/FilesystemLocation.php 2 patches
Indentation   +180 added lines, -180 removed lines patch added patch discarded remove patch
@@ -28,184 +28,184 @@
 block discarded – undo
28 28
 class FilesystemLocation implements Location
29 29
 {
30 30
 
31
-    /**
32
-     * Path that this location points to.
33
-     *
34
-     * @since 0.1.0
35
-     *
36
-     * @var string
37
-     */
38
-    protected $path;
39
-
40
-    /**
41
-     * Extensions that this location can accept.
42
-     *
43
-     * @since 0.1.0
44
-     *
45
-     * @var Extensions
46
-     */
47
-    protected $extensions;
48
-
49
-    /**
50
-     * Instantiate a FilesystemLocation object.
51
-     *
52
-     * @since 0.1.0
53
-     *
54
-     * @param string                       $path       Path that this location points to.
55
-     * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept.
56
-     */
57
-    public function __construct(string $path, $extensions = null)
58
-    {
59
-        $this->path       = $path;
60
-        $this->extensions = $this->validateExtensions($extensions);
61
-    }
62
-
63
-    /**
64
-     * Get the first URI that matches the given criteria.
65
-     *
66
-     * @since 0.1.0
67
-     *
68
-     * @param array $criteria Criteria to match.
69
-     *
70
-     * @return string|false URI that matches the criteria or false if none found.
71
-     */
72
-    public function getURI(array $criteria)
73
-    {
74
-        $uris = $this->getURIs($criteria);
75
-
76
-        return $uris->count() > 0
77
-            ? $uris->first()
78
-            : false;
79
-    }
80
-
81
-    /**
82
-     * Get all URIs that match the given criteria.
83
-     *
84
-     * @since 0.1.1
85
-     *
86
-     * @param array $criteria Criteria to match.
87
-     *
88
-     * @return URIs URIs that match the criteria or an empty collection if none found.
89
-     */
90
-    public function getURIs(array $criteria): URIs
91
-    {
92
-        $uris = new URIs();
93
-
94
-        foreach ($this->extensions as $extension) {
95
-            $finder = new Finder();
96
-
97
-            try {
98
-                $finder->files()
99
-                    ->name($this->getNamePattern($criteria, $extension))
100
-                    ->in($this->getPathPattern($this->getRelativePath($criteria)));
101
-                foreach ($finder as $file) {
102
-                    /** @var SplFileInfo $file */
103
-                    $uris->add($file->getPathname());
104
-                }
105
-            } catch (Exception $exception) {
106
-                // Fail silently;
107
-            }
108
-        }
109
-
110
-        return $uris;
111
-    }
112
-
113
-    /**
114
-     * Get the name pattern to pass to the file finder.
115
-     *
116
-     * @since 0.1.3
117
-     *
118
-     * @param array  $criteria  Criteria to match.
119
-     * @param string $extension Extension to match.
120
-     *
121
-     * @return string Name pattern to pass to the file finder.
122
-     */
123
-    protected function getNamePattern(array $criteria, string $extension): string
124
-    {
125
-        $names = [];
126
-
127
-        $names[] = array_map(function ($criterion) use ($extension) {
128
-            $uriExtension = URIHelper::containsExtension($criterion);
129
-            if (! empty($extension)) {
130
-                $extension = ltrim($extension, '.');
131
-
132
-                if ($uriExtension === $extension) {
133
-                    $criterion = substr($criterion,0,-strlen(".{$extension}"));
134
-                }
135
-            } else {
136
-                $extension = URIHelper::containsExtension($criterion);
137
-                if (!empty($extension)) {
138
-                    $criterion = substr($criterion,0,-strlen(".{$extension}"));
139
-                }
140
-            }
141
-
142
-            $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1));
143
-
144
-            return (empty($extension) || URIHelper::hasExtension($criterion, $extension))
145
-                ? "{$criterion}(?:\..*?)$"
146
-                : "{$criterion}\.{$extension}$";
147
-        }, $criteria)[0];
148
-
149
-        return chr(1) . implode('|', array_unique($names)) . chr(1);
150
-    }
151
-
152
-    /**
153
-     * Get the path pattern to pass to the file finder.
154
-     *
155
-     * @since 0.4.7
156
-     *
157
-     * @param string $relativePath Relative path that was included in the
158
-     *                             criterion.
159
-     * @return string Path pattern to pass to the file finder.
160
-     */
161
-    protected function getPathPattern(string $relativePath): string
162
-    {
163
-        if (empty($relativePath)) {
164
-            return $this->path;
165
-        }
166
-
167
-        return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/');
168
-    }
169
-
170
-    /**
171
-     * Get the relative path that is included in the criterion.
172
-     *
173
-     * @since 0.4.7
174
-     *
175
-     * @param array $criteria Criteria to extract the relative path from.
176
-     * @return string Relative path included in the criterion.
177
-     */
178
-    protected function getRelativePath($criteria): string
179
-    {
180
-        $criterion  = current($criteria);
181
-        $components = explode('/', $criterion);
182
-
183
-        if (count($components) < 2) {
184
-            return '';
185
-        }
186
-
187
-        return implode('/', array_slice($components, 0, -1));
188
-    }
189
-
190
-    /**
191
-     * Validate the extensions and return a collection.
192
-     *
193
-     * @since 0.1.1
194
-     *
195
-     * @param Extensions|array|string|null $extensions Extensions to validate.
196
-     *
197
-     * @return Extensions Validated extensions collection.
198
-     */
199
-    protected function validateExtensions($extensions): Extensions
200
-    {
201
-        if (empty($extensions)) {
202
-            $extensions = new Extensions(['']);
203
-        }
204
-
205
-        if (! $extensions instanceof Extensions) {
206
-            $extensions = new Extensions((array)$extensions);
207
-        }
208
-
209
-        return $extensions;
210
-    }
31
+	/**
32
+	 * Path that this location points to.
33
+	 *
34
+	 * @since 0.1.0
35
+	 *
36
+	 * @var string
37
+	 */
38
+	protected $path;
39
+
40
+	/**
41
+	 * Extensions that this location can accept.
42
+	 *
43
+	 * @since 0.1.0
44
+	 *
45
+	 * @var Extensions
46
+	 */
47
+	protected $extensions;
48
+
49
+	/**
50
+	 * Instantiate a FilesystemLocation object.
51
+	 *
52
+	 * @since 0.1.0
53
+	 *
54
+	 * @param string                       $path       Path that this location points to.
55
+	 * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept.
56
+	 */
57
+	public function __construct(string $path, $extensions = null)
58
+	{
59
+		$this->path       = $path;
60
+		$this->extensions = $this->validateExtensions($extensions);
61
+	}
62
+
63
+	/**
64
+	 * Get the first URI that matches the given criteria.
65
+	 *
66
+	 * @since 0.1.0
67
+	 *
68
+	 * @param array $criteria Criteria to match.
69
+	 *
70
+	 * @return string|false URI that matches the criteria or false if none found.
71
+	 */
72
+	public function getURI(array $criteria)
73
+	{
74
+		$uris = $this->getURIs($criteria);
75
+
76
+		return $uris->count() > 0
77
+			? $uris->first()
78
+			: false;
79
+	}
80
+
81
+	/**
82
+	 * Get all URIs that match the given criteria.
83
+	 *
84
+	 * @since 0.1.1
85
+	 *
86
+	 * @param array $criteria Criteria to match.
87
+	 *
88
+	 * @return URIs URIs that match the criteria or an empty collection if none found.
89
+	 */
90
+	public function getURIs(array $criteria): URIs
91
+	{
92
+		$uris = new URIs();
93
+
94
+		foreach ($this->extensions as $extension) {
95
+			$finder = new Finder();
96
+
97
+			try {
98
+				$finder->files()
99
+					->name($this->getNamePattern($criteria, $extension))
100
+					->in($this->getPathPattern($this->getRelativePath($criteria)));
101
+				foreach ($finder as $file) {
102
+					/** @var SplFileInfo $file */
103
+					$uris->add($file->getPathname());
104
+				}
105
+			} catch (Exception $exception) {
106
+				// Fail silently;
107
+			}
108
+		}
109
+
110
+		return $uris;
111
+	}
112
+
113
+	/**
114
+	 * Get the name pattern to pass to the file finder.
115
+	 *
116
+	 * @since 0.1.3
117
+	 *
118
+	 * @param array  $criteria  Criteria to match.
119
+	 * @param string $extension Extension to match.
120
+	 *
121
+	 * @return string Name pattern to pass to the file finder.
122
+	 */
123
+	protected function getNamePattern(array $criteria, string $extension): string
124
+	{
125
+		$names = [];
126
+
127
+		$names[] = array_map(function ($criterion) use ($extension) {
128
+			$uriExtension = URIHelper::containsExtension($criterion);
129
+			if (! empty($extension)) {
130
+				$extension = ltrim($extension, '.');
131
+
132
+				if ($uriExtension === $extension) {
133
+					$criterion = substr($criterion,0,-strlen(".{$extension}"));
134
+				}
135
+			} else {
136
+				$extension = URIHelper::containsExtension($criterion);
137
+				if (!empty($extension)) {
138
+					$criterion = substr($criterion,0,-strlen(".{$extension}"));
139
+				}
140
+			}
141
+
142
+			$criterion = preg_quote(URIHelper::getFilename($criterion), chr(1));
143
+
144
+			return (empty($extension) || URIHelper::hasExtension($criterion, $extension))
145
+				? "{$criterion}(?:\..*?)$"
146
+				: "{$criterion}\.{$extension}$";
147
+		}, $criteria)[0];
148
+
149
+		return chr(1) . implode('|', array_unique($names)) . chr(1);
150
+	}
151
+
152
+	/**
153
+	 * Get the path pattern to pass to the file finder.
154
+	 *
155
+	 * @since 0.4.7
156
+	 *
157
+	 * @param string $relativePath Relative path that was included in the
158
+	 *                             criterion.
159
+	 * @return string Path pattern to pass to the file finder.
160
+	 */
161
+	protected function getPathPattern(string $relativePath): string
162
+	{
163
+		if (empty($relativePath)) {
164
+			return $this->path;
165
+		}
166
+
167
+		return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/');
168
+	}
169
+
170
+	/**
171
+	 * Get the relative path that is included in the criterion.
172
+	 *
173
+	 * @since 0.4.7
174
+	 *
175
+	 * @param array $criteria Criteria to extract the relative path from.
176
+	 * @return string Relative path included in the criterion.
177
+	 */
178
+	protected function getRelativePath($criteria): string
179
+	{
180
+		$criterion  = current($criteria);
181
+		$components = explode('/', $criterion);
182
+
183
+		if (count($components) < 2) {
184
+			return '';
185
+		}
186
+
187
+		return implode('/', array_slice($components, 0, -1));
188
+	}
189
+
190
+	/**
191
+	 * Validate the extensions and return a collection.
192
+	 *
193
+	 * @since 0.1.1
194
+	 *
195
+	 * @param Extensions|array|string|null $extensions Extensions to validate.
196
+	 *
197
+	 * @return Extensions Validated extensions collection.
198
+	 */
199
+	protected function validateExtensions($extensions): Extensions
200
+	{
201
+		if (empty($extensions)) {
202
+			$extensions = new Extensions(['']);
203
+		}
204
+
205
+		if (! $extensions instanceof Extensions) {
206
+			$extensions = new Extensions((array)$extensions);
207
+		}
208
+
209
+		return $extensions;
210
+	}
211 211
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -124,18 +124,18 @@  discard block
 block discarded – undo
124 124
     {
125 125
         $names = [];
126 126
 
127
-        $names[] = array_map(function ($criterion) use ($extension) {
127
+        $names[] = array_map(function($criterion) use ($extension) {
128 128
             $uriExtension = URIHelper::containsExtension($criterion);
129
-            if (! empty($extension)) {
129
+            if ( ! empty($extension)) {
130 130
                 $extension = ltrim($extension, '.');
131 131
 
132 132
                 if ($uriExtension === $extension) {
133
-                    $criterion = substr($criterion,0,-strlen(".{$extension}"));
133
+                    $criterion = substr($criterion, 0, -strlen(".{$extension}"));
134 134
                 }
135 135
             } else {
136 136
                 $extension = URIHelper::containsExtension($criterion);
137
-                if (!empty($extension)) {
138
-                    $criterion = substr($criterion,0,-strlen(".{$extension}"));
137
+                if ( ! empty($extension)) {
138
+                    $criterion = substr($criterion, 0, -strlen(".{$extension}"));
139 139
                 }
140 140
             }
141 141
 
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
                 : "{$criterion}\.{$extension}$";
147 147
         }, $criteria)[0];
148 148
 
149
-        return chr(1) . implode('|', array_unique($names)) . chr(1);
149
+        return chr(1).implode('|', array_unique($names)).chr(1);
150 150
     }
151 151
 
152 152
     /**
@@ -164,7 +164,7 @@  discard block
 block discarded – undo
164 164
             return $this->path;
165 165
         }
166 166
 
167
-        return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/');
167
+        return rtrim($this->path, '/').'/'.ltrim($relativePath, '/');
168 168
     }
169 169
 
170 170
     /**
@@ -202,8 +202,8 @@  discard block
 block discarded – undo
202 202
             $extensions = new Extensions(['']);
203 203
         }
204 204
 
205
-        if (! $extensions instanceof Extensions) {
206
-            $extensions = new Extensions((array)$extensions);
205
+        if ( ! $extensions instanceof Extensions) {
206
+            $extensions = new Extensions((array) $extensions);
207 207
         }
208 208
 
209 209
         return $extensions;
Please login to merge, or discard this patch.