Test Failed
Push — master ( 90a3be...261c05 )
by Alain
06:34
created
src/Views.php 1 patch
Indentation   +85 added lines, -85 removed lines patch added patch discarded remove patch
@@ -28,97 +28,97 @@
 block discarded – undo
28 28
 class Views
29 29
 {
30 30
 
31
-    /**
32
-     * ViewBuilder Instance.
33
-     *
34
-     * @since 0.1.0
35
-     *
36
-     * @var ViewBuilder
37
-     */
38
-    protected static $viewBuilder;
31
+	/**
32
+	 * ViewBuilder Instance.
33
+	 *
34
+	 * @since 0.1.0
35
+	 *
36
+	 * @var ViewBuilder
37
+	 */
38
+	protected static $viewBuilder;
39 39
 
40
-    /**
41
-     * Add a location to the ViewBuilder.
42
-     *
43
-     * @since 0.1.0
44
-     *
45
-     * @param Location $location Location to add.
46
-     *
47
-     * @throws FailedToProcessConfigException If the Config could not be processed.
48
-     */
49
-    public static function addLocation(Location $location)
50
-    {
51
-        $viewBuilder = static::getViewBuilder();
52
-        $viewBuilder->addLocation($location);
53
-    }
40
+	/**
41
+	 * Add a location to the ViewBuilder.
42
+	 *
43
+	 * @since 0.1.0
44
+	 *
45
+	 * @param Location $location Location to add.
46
+	 *
47
+	 * @throws FailedToProcessConfigException If the Config could not be processed.
48
+	 */
49
+	public static function addLocation(Location $location)
50
+	{
51
+		$viewBuilder = static::getViewBuilder();
52
+		$viewBuilder->addLocation($location);
53
+	}
54 54
 
55
-    /**
56
-     * Get the ViewBuilder instance.
57
-     *
58
-     * @since 0.1.0
59
-     *
60
-     * @return ViewBuilder
61
-     * @throws FailedToProcessConfigException If the Config could not be processed.
62
-     */
63
-    public static function getViewBuilder()
64
-    {
65
-        if (null === static::$viewBuilder) {
66
-            static::$viewBuilder = static::instantiateViewBuilder();
67
-        }
55
+	/**
56
+	 * Get the ViewBuilder instance.
57
+	 *
58
+	 * @since 0.1.0
59
+	 *
60
+	 * @return ViewBuilder
61
+	 * @throws FailedToProcessConfigException If the Config could not be processed.
62
+	 */
63
+	public static function getViewBuilder()
64
+	{
65
+		if (null === static::$viewBuilder) {
66
+			static::$viewBuilder = static::instantiateViewBuilder();
67
+		}
68 68
 
69
-        return static::$viewBuilder;
70
-    }
69
+		return static::$viewBuilder;
70
+	}
71 71
 
72
-    /**
73
-     * Instantiate the ViewBuilder.
74
-     *
75
-     * @since 0.1.0
76
-     *
77
-     * @param ConfigInterface|null $config Optional. Configuration to pass into the ViewBuilder.
78
-     *
79
-     * @return ViewBuilder Instance of the ViewBuilder.
80
-     * @throws FailedToProcessConfigException If the Config could not be processed.
81
-     */
82
-    public static function instantiateViewBuilder(ConfigInterface $config = null)
83
-    {
84
-        return static::$viewBuilder = new ViewBuilder($config);
85
-    }
72
+	/**
73
+	 * Instantiate the ViewBuilder.
74
+	 *
75
+	 * @since 0.1.0
76
+	 *
77
+	 * @param ConfigInterface|null $config Optional. Configuration to pass into the ViewBuilder.
78
+	 *
79
+	 * @return ViewBuilder Instance of the ViewBuilder.
80
+	 * @throws FailedToProcessConfigException If the Config could not be processed.
81
+	 */
82
+	public static function instantiateViewBuilder(ConfigInterface $config = null)
83
+	{
84
+		return static::$viewBuilder = new ViewBuilder($config);
85
+	}
86 86
 
87
-    /**
88
-     * Create a new view for a given URI.
89
-     *
90
-     * @since 0.1.0
91
-     *
92
-     * @param string      $view View identifier to create a view for.
93
-     * @param string|null $type Type of view to create.
94
-     *
95
-     * @return View Instance of the requested view.
96
-     * @throws FailedToProcessConfigException If the Config could not be processed.
97
-     */
98
-    public static function create($view, $type = null)
99
-    {
100
-        $viewBuilder = static::getViewBuilder();
87
+	/**
88
+	 * Create a new view for a given URI.
89
+	 *
90
+	 * @since 0.1.0
91
+	 *
92
+	 * @param string      $view View identifier to create a view for.
93
+	 * @param string|null $type Type of view to create.
94
+	 *
95
+	 * @return View Instance of the requested view.
96
+	 * @throws FailedToProcessConfigException If the Config could not be processed.
97
+	 */
98
+	public static function create($view, $type = null)
99
+	{
100
+		$viewBuilder = static::getViewBuilder();
101 101
 
102
-        return $viewBuilder->create($view, $type);
103
-    }
102
+		return $viewBuilder->create($view, $type);
103
+	}
104 104
 
105
-    /**
106
-     * Render a view for a given URI.
107
-     *
108
-     * @since 0.1.0
109
-     *
110
-     * @param string      $view    View identifier to create a view for.
111
-     * @param array       $context Optional. The context in which to render the view.
112
-     * @param string|null $type    Type of view to create.
113
-     *
114
-     * @return string Rendered HTML content.
115
-     * @throws FailedToProcessConfigException If the Config could not be processed.
116
-     */
117
-    public static function render($view, array $context = [], $type = null)
118
-    {
119
-        $viewBuilder = static::getViewBuilder();
120
-        $viewObject  = $viewBuilder->create($view, $type);
105
+	/**
106
+	 * Render a view for a given URI.
107
+	 *
108
+	 * @since 0.1.0
109
+	 *
110
+	 * @param string      $view    View identifier to create a view for.
111
+	 * @param array       $context Optional. The context in which to render the view.
112
+	 * @param string|null $type    Type of view to create.
113
+	 *
114
+	 * @return string Rendered HTML content.
115
+	 * @throws FailedToProcessConfigException If the Config could not be processed.
116
+	 */
117
+	public static function render($view, array $context = [], $type = null)
118
+	{
119
+		$viewBuilder = static::getViewBuilder();
120
+		$viewObject  = $viewBuilder->create($view, $type);
121 121
 
122
-        return $viewObject->render($context);
123
-    }
122
+		return $viewObject->render($context);
123
+	}
124 124
 }
Please login to merge, or discard this patch.
src/View/Location/URIs.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -25,19 +25,19 @@
 block discarded – undo
25 25
 class URIs extends ArrayCollection
26 26
 {
27 27
 
28
-    /**
29
-     * Create a new URIs from a Symfony Finder instance.
30
-     *
31
-     * @since 0.1.3
32
-     *
33
-     * @param Finder $finder The Finder instance to create the URI collection from.
34
-     *
35
-     * @return URIs New URIs instance.
36
-     */
37
-    public static function fromFinder(Finder $finder): URIs
38
-    {
39
-        $elements = array_keys(iterator_to_array($finder));
28
+	/**
29
+	 * Create a new URIs from a Symfony Finder instance.
30
+	 *
31
+	 * @since 0.1.3
32
+	 *
33
+	 * @param Finder $finder The Finder instance to create the URI collection from.
34
+	 *
35
+	 * @return URIs New URIs instance.
36
+	 */
37
+	public static function fromFinder(Finder $finder): URIs
38
+	{
39
+		$elements = array_keys(iterator_to_array($finder));
40 40
 
41
-        return new static($elements);
42
-    }
41
+		return new static($elements);
42
+	}
43 43
 }
Please login to merge, or discard this patch.
src/View/Location/Location.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -22,25 +22,25 @@
 block discarded – undo
22 22
 interface Location
23 23
 {
24 24
 
25
-    /**
26
-     * Get the first URI that matches the given criteria.
27
-     *
28
-     * @since 0.1.0
29
-     *
30
-     * @param array $criteria Criteria to match.
31
-     *
32
-     * @return string|false URI that matches the criteria or false if none found.
33
-     */
34
-    public function getURI(array $criteria);
25
+	/**
26
+	 * Get the first URI that matches the given criteria.
27
+	 *
28
+	 * @since 0.1.0
29
+	 *
30
+	 * @param array $criteria Criteria to match.
31
+	 *
32
+	 * @return string|false URI that matches the criteria or false if none found.
33
+	 */
34
+	public function getURI(array $criteria);
35 35
 
36
-    /**
37
-     * Get all URIs that match the given criteria.
38
-     *
39
-     * @since 0.1.1
40
-     *
41
-     * @param array $criteria Criteria to match.
42
-     *
43
-     * @return URIs Collection of URIs that matches the criteria or an empty collection if none found.
44
-     */
45
-    public function getURIs(array $criteria): URIs;
36
+	/**
37
+	 * Get all URIs that match the given criteria.
38
+	 *
39
+	 * @since 0.1.1
40
+	 *
41
+	 * @param array $criteria Criteria to match.
42
+	 *
43
+	 * @return URIs Collection of URIs that matches the criteria or an empty collection if none found.
44
+	 */
45
+	public function getURIs(array $criteria): URIs;
46 46
 }
Please login to merge, or discard this patch.
src/View/Location/Locations.php 1 patch
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -24,37 +24,37 @@
 block discarded – undo
24 24
 class Locations extends ArrayCollection
25 25
 {
26 26
 
27
-    /**
28
-     * Adds a location at the end of the collection if it does not already exist.
29
-     *
30
-     * @param mixed $location The location to add.
31
-     *
32
-     * @return bool Whether the location was added or not.
33
-     */
34
-    public function add($location): bool
35
-    {
36
-        if ($this->hasLocation($location)) {
37
-            return false;
38
-        }
27
+	/**
28
+	 * Adds a location at the end of the collection if it does not already exist.
29
+	 *
30
+	 * @param mixed $location The location to add.
31
+	 *
32
+	 * @return bool Whether the location was added or not.
33
+	 */
34
+	public function add($location): bool
35
+	{
36
+		if ($this->hasLocation($location)) {
37
+			return false;
38
+		}
39 39
 
40
-        return parent::add($location);
41
-    }
40
+		return parent::add($location);
41
+	}
42 42
 
43
-    /**
44
-     * Check whether a given location is already registered.
45
-     *
46
-     * For two locations to be equal, both their path and their extensions must be the same.
47
-     *
48
-     * @since 0.1.1
49
-     *
50
-     * @param Location $location Location to check the existence of.
51
-     *
52
-     * @return bool Whether the location is already registered or not.
53
-     */
54
-    public function hasLocation(Location $location): bool
55
-    {
56
-        return $this->exists(function ($key, $element) use ($location) {
57
-            return $location == $element;
58
-        });
59
-    }
43
+	/**
44
+	 * Check whether a given location is already registered.
45
+	 *
46
+	 * For two locations to be equal, both their path and their extensions must be the same.
47
+	 *
48
+	 * @since 0.1.1
49
+	 *
50
+	 * @param Location $location Location to check the existence of.
51
+	 *
52
+	 * @return bool Whether the location is already registered or not.
53
+	 */
54
+	public function hasLocation(Location $location): bool
55
+	{
56
+		return $this->exists(function ($key, $element) use ($location) {
57
+			return $location == $element;
58
+		});
59
+	}
60 60
 }
Please login to merge, or discard this patch.
src/View/Support/Findable.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -22,14 +22,14 @@
 block discarded – undo
22 22
 interface Findable
23 23
 {
24 24
 
25
-    /**
26
-     * Check whether the Findable can handle an individual criterion.
27
-     *
28
-     * @since 0.1.0
29
-     *
30
-     * @param mixed $criterion Criterion to check.
31
-     *
32
-     * @return bool Whether the Findable can handle the criterion.
33
-     */
34
-    public function canHandle($criterion): bool;
25
+	/**
26
+	 * Check whether the Findable can handle an individual criterion.
27
+	 *
28
+	 * @since 0.1.0
29
+	 *
30
+	 * @param mixed $criterion Criterion to check.
31
+	 *
32
+	 * @return bool Whether the Findable can handle the criterion.
33
+	 */
34
+	public function canHandle($criterion): bool;
35 35
 }
Please login to merge, or discard this patch.
src/View/Support/Finder.php 1 patch
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -22,23 +22,23 @@
 block discarded – undo
22 22
 interface Finder
23 23
 {
24 24
 
25
-    /**
26
-     * Find a result based on a specific criteria.
27
-     *
28
-     * @since 0.1.0
29
-     *
30
-     * @param array $criteria Criteria to search for.
31
-     *
32
-     * @return mixed Result of the search.
33
-     */
34
-    public function find(array $criteria);
25
+	/**
26
+	 * Find a result based on a specific criteria.
27
+	 *
28
+	 * @since 0.1.0
29
+	 *
30
+	 * @param array $criteria Criteria to search for.
31
+	 *
32
+	 * @return mixed Result of the search.
33
+	 */
34
+	public function find(array $criteria);
35 35
 
36
-    /**
37
-     * Get the NullObject.
38
-     *
39
-     * @since 0.1.1
40
-     *
41
-     * @return NullFindable NullObject for the current Finder.
42
-     */
43
-    public function getNullObject(): NullFindable;
36
+	/**
37
+	 * Get the NullObject.
38
+	 *
39
+	 * @since 0.1.1
40
+	 *
41
+	 * @return NullFindable NullObject for the current Finder.
42
+	 */
43
+	public function getNullObject(): NullFindable;
44 44
 }
Please login to merge, or discard this patch.
src/View/Support/URIHelper.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -22,38 +22,38 @@
 block discarded – undo
22 22
 class URIHelper
23 23
 {
24 24
 
25
-    /**
26
-     * Check whether a given URI has a specific extension.
27
-     *
28
-     * @since 0.1.3
29
-     *
30
-     * @param string $uri       URI to check the extension of.
31
-     * @param string $extension Extension to check for.
32
-     *
33
-     * @return bool
34
-     */
35
-    public static function hasExtension(string $uri, string $extension): bool
36
-    {
37
-        $uriLength       = mb_strlen($uri);
38
-        $extensionLength = mb_strlen($extension);
39
-        if ($extensionLength > $uriLength) {
40
-            return false;
41
-        }
25
+	/**
26
+	 * Check whether a given URI has a specific extension.
27
+	 *
28
+	 * @since 0.1.3
29
+	 *
30
+	 * @param string $uri       URI to check the extension of.
31
+	 * @param string $extension Extension to check for.
32
+	 *
33
+	 * @return bool
34
+	 */
35
+	public static function hasExtension(string $uri, string $extension): bool
36
+	{
37
+		$uriLength       = mb_strlen($uri);
38
+		$extensionLength = mb_strlen($extension);
39
+		if ($extensionLength > $uriLength) {
40
+			return false;
41
+		}
42 42
 
43
-        return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
44
-    }
43
+		return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
44
+	}
45 45
 
46
-    /**
47
-     * Get the filename for an URI.
48
-     *
49
-     * @since 0.1.3
50
-     *
51
-     * @param string $uri URI to get the filename from.
52
-     *
53
-     * @return string Filename without path.
54
-     */
55
-    public static function getFilename(string $uri): String
56
-    {
57
-        return basename($uri);
58
-    }
46
+	/**
47
+	 * Get the filename for an URI.
48
+	 *
49
+	 * @since 0.1.3
50
+	 *
51
+	 * @param string $uri URI to get the filename from.
52
+	 *
53
+	 * @return string Filename without path.
54
+	 */
55
+	public static function getFilename(string $uri): String
56
+	{
57
+		return basename($uri);
58
+	}
59 59
 }
Please login to merge, or discard this patch.
src/View/Support/Extensions.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -24,24 +24,24 @@
 block discarded – undo
24 24
 class Extensions extends ArrayCollection
25 25
 {
26 26
 
27
-    /**
28
-     * Check whether a given URI has a specific extension.
29
-     *
30
-     * @since 0.1.0
31
-     *
32
-     * @param string $uri       URI to check the extension of.
33
-     * @param string $extension Extension to check for.
34
-     *
35
-     * @return bool
36
-     */
37
-    public static function hasExtension(string $uri, string $extension): bool
38
-    {
39
-        $uriLength       = mb_strlen($uri);
40
-        $extensionLength = mb_strlen($extension);
41
-        if ($extensionLength > $uriLength) {
42
-            return false;
43
-        }
27
+	/**
28
+	 * Check whether a given URI has a specific extension.
29
+	 *
30
+	 * @since 0.1.0
31
+	 *
32
+	 * @param string $uri       URI to check the extension of.
33
+	 * @param string $extension Extension to check for.
34
+	 *
35
+	 * @return bool
36
+	 */
37
+	public static function hasExtension(string $uri, string $extension): bool
38
+	{
39
+		$uriLength       = mb_strlen($uri);
40
+		$extensionLength = mb_strlen($extension);
41
+		if ($extensionLength > $uriLength) {
42
+			return false;
43
+		}
44 44
 
45
-        return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
46
-    }
45
+		return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0;
46
+	}
47 47
 }
Please login to merge, or discard this patch.
src/View/Support/AbstractFinder.php 1 patch
Indentation   +211 added lines, -211 removed lines patch added patch discarded remove patch
@@ -27,215 +27,215 @@
 block discarded – undo
27 27
 abstract class AbstractFinder implements Finder
28 28
 {
29 29
 
30
-    use ConfigTrait;
31
-
32
-    /**
33
-     * Findable collection that the Finder can iterate through to find a match.
34
-     *
35
-     * @since 0.1.0
36
-     *
37
-     * @var Findables
38
-     */
39
-    protected $findables;
40
-
41
-    /**
42
-     * NullObject that is returned if the Finder could not find a match.
43
-     *
44
-     * @since 0.1.0
45
-     *
46
-     * @var NullFindable
47
-     */
48
-    protected $nullObject;
49
-
50
-    /**
51
-     * Instantiate an AbstractFinder object.
52
-     *
53
-     * @since 0.1.0
54
-     *
55
-     * @param ConfigInterface $config Configuration of the AbstractFinder.
56
-     *
57
-     * @throws FailedToProcessConfigException If the config could not be processed.
58
-     */
59
-    public function __construct(ConfigInterface $config)
60
-    {
61
-        $this->processConfig($config);
62
-        $this->findables = new Findables();
63
-        $this->registerFindables($this->config);
64
-        $this->registerNullObject($this->config);
65
-    }
66
-
67
-    /**
68
-     * Register the Findables defined in the given configuration.
69
-     *
70
-     * @since 0.1.0
71
-     *
72
-     * @param ConfigInterface $config Configuration to register the Findables from.
73
-     */
74
-    public function registerFindables(ConfigInterface $config)
75
-    {
76
-        $findables = (array) $config->getKey($this->getFindablesConfigKey());
77
-        foreach ($findables as $findableKey => $findableObject) {
78
-            $this->findables->set($findableKey, $findableObject);
79
-        }
80
-    }
81
-
82
-    /**
83
-     * Register the NullObject defined in the given configuration.
84
-     *
85
-     * @since 0.1.0
86
-     *
87
-     * @param ConfigInterface $config Configuration to register the NullObject from.
88
-     */
89
-    public function registerNullObject(ConfigInterface $config)
90
-    {
91
-        $this->nullObject = $config->getKey($this->getNullObjectConfigKey());
92
-    }
93
-
94
-    /**
95
-     * Get the NullObject.
96
-     *
97
-     * @since 0.1.1
98
-     *
99
-     * @return NullFindable NullObject for the current Finder.
100
-     * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
101
-     */
102
-    public function getNullObject(): NullFindable
103
-    {
104
-        $this->initializeNullObject();
105
-
106
-        return $this->nullObject;
107
-    }
108
-
109
-    /**
110
-     * Get the config key for the Findables definitions.
111
-     *
112
-     * @since 0.1.0
113
-     *
114
-     * @return string Config key use to define the Findables.
115
-     */
116
-    protected function getFindablesConfigKey(): string
117
-    {
118
-        return 'Findables';
119
-    }
120
-
121
-    /**
122
-     * Get the config key for the NullObject definitions.
123
-     *
124
-     * @since 0.1.0
125
-     *
126
-     * @return string Config key use to define the NullObject.
127
-     */
128
-    protected function getNullObjectConfigKey(): string
129
-    {
130
-        return 'NullObject';
131
-    }
132
-
133
-    /**
134
-     * Initialize the NullObject.
135
-     *
136
-     * @since 0.1.1
137
-     *
138
-     * @param mixed $arguments Optional. Arguments to use.
139
-     *
140
-     * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
141
-     */
142
-    protected function initializeNullObject($arguments = null)
143
-    {
144
-        $this->nullObject = $this->maybeInstantiateFindable($this->nullObject, $arguments);
145
-    }
146
-
147
-    /**
148
-     * Initialize the Findables that can be iterated.
149
-     *
150
-     * @param mixed $arguments Optional. Arguments to use.
151
-     *
152
-     * @since 0.1.0
153
-     *
154
-     * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
155
-     */
156
-    protected function initializeFindables($arguments = null)
157
-    {
158
-        $this->findables = $this->findables->map(function ($findable) use ($arguments) {
159
-            return $this->initializeFindable($findable, $arguments);
160
-        });
161
-    }
162
-
163
-    /**
164
-     * Initialize a single findable by instantiating class name strings and calling closures.
165
-     *
166
-     * @since 0.1.0
167
-     *
168
-     * @param mixed $findable  Findable to instantiate.
169
-     * @param mixed $arguments Optional. Arguments to use.
170
-     *
171
-     * @return Findable Instantiated findable.
172
-     * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
173
-     */
174
-    protected function initializeFindable($findable, $arguments = null): Findable
175
-    {
176
-        return $this->maybeInstantiateFindable($findable, $arguments);
177
-    }
178
-
179
-    /**
180
-     * Maybe instantiate a Findable if it is not yet an object.
181
-     *
182
-     * @since 0.1.1
183
-     *
184
-     * @param mixed $findable  Findable to instantiate.
185
-     * @param mixed $arguments Optional. Arguments to use.
186
-     *
187
-     * @return Findable Instantiated findable.
188
-     * @throws FailedToInstantiateFindable If the findable could not be instantiated.
189
-     */
190
-    protected function maybeInstantiateFindable($findable, $arguments = null): Findable
191
-    {
192
-        if (is_string($findable)) {
193
-            $findable = $this->instantiateFindableFromString($findable, $arguments);
194
-        }
195
-
196
-        if (is_callable($findable)) {
197
-            $findable = $this->instantiateFindableFromCallable($findable, $arguments);
198
-        }
199
-
200
-        if (! $findable instanceof Findable) {
201
-            throw new FailedToInstantiateFindable(
202
-                sprintf(
203
-                    _('Could not instantiate Findable "%s".'),
204
-                    serialize($findable)
205
-                )
206
-            );
207
-        }
208
-
209
-        return $findable;
210
-    }
211
-
212
-    /**
213
-     * Instantiate a Findable from a string.
214
-     *
215
-     * @since 0.1.1
216
-     *
217
-     * @param string $string    String to use for instantiation.
218
-     * @param mixed  $arguments Optional. Arguments to use for instantiation.
219
-     *
220
-     * @return Findable Instantiated Findable.
221
-     */
222
-    protected function instantiateFindableFromString(string $string, $arguments = []): Findable
223
-    {
224
-        return new $string(...(array)$arguments);
225
-    }
226
-
227
-    /**
228
-     * Instantiate a Findable from a callable.
229
-     *
230
-     * @since 0.1.1
231
-     *
232
-     * @param callable $callable  Callable to use for instantiation.
233
-     * @param mixed    $arguments Optional. Arguments to use for instantiation.
234
-     *
235
-     * @return Findable Instantiated Findable.
236
-     */
237
-    protected function instantiateFindableFromCallable(callable $callable, $arguments = []): Findable
238
-    {
239
-        return $callable(...(array)$arguments);
240
-    }
30
+	use ConfigTrait;
31
+
32
+	/**
33
+	 * Findable collection that the Finder can iterate through to find a match.
34
+	 *
35
+	 * @since 0.1.0
36
+	 *
37
+	 * @var Findables
38
+	 */
39
+	protected $findables;
40
+
41
+	/**
42
+	 * NullObject that is returned if the Finder could not find a match.
43
+	 *
44
+	 * @since 0.1.0
45
+	 *
46
+	 * @var NullFindable
47
+	 */
48
+	protected $nullObject;
49
+
50
+	/**
51
+	 * Instantiate an AbstractFinder object.
52
+	 *
53
+	 * @since 0.1.0
54
+	 *
55
+	 * @param ConfigInterface $config Configuration of the AbstractFinder.
56
+	 *
57
+	 * @throws FailedToProcessConfigException If the config could not be processed.
58
+	 */
59
+	public function __construct(ConfigInterface $config)
60
+	{
61
+		$this->processConfig($config);
62
+		$this->findables = new Findables();
63
+		$this->registerFindables($this->config);
64
+		$this->registerNullObject($this->config);
65
+	}
66
+
67
+	/**
68
+	 * Register the Findables defined in the given configuration.
69
+	 *
70
+	 * @since 0.1.0
71
+	 *
72
+	 * @param ConfigInterface $config Configuration to register the Findables from.
73
+	 */
74
+	public function registerFindables(ConfigInterface $config)
75
+	{
76
+		$findables = (array) $config->getKey($this->getFindablesConfigKey());
77
+		foreach ($findables as $findableKey => $findableObject) {
78
+			$this->findables->set($findableKey, $findableObject);
79
+		}
80
+	}
81
+
82
+	/**
83
+	 * Register the NullObject defined in the given configuration.
84
+	 *
85
+	 * @since 0.1.0
86
+	 *
87
+	 * @param ConfigInterface $config Configuration to register the NullObject from.
88
+	 */
89
+	public function registerNullObject(ConfigInterface $config)
90
+	{
91
+		$this->nullObject = $config->getKey($this->getNullObjectConfigKey());
92
+	}
93
+
94
+	/**
95
+	 * Get the NullObject.
96
+	 *
97
+	 * @since 0.1.1
98
+	 *
99
+	 * @return NullFindable NullObject for the current Finder.
100
+	 * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
101
+	 */
102
+	public function getNullObject(): NullFindable
103
+	{
104
+		$this->initializeNullObject();
105
+
106
+		return $this->nullObject;
107
+	}
108
+
109
+	/**
110
+	 * Get the config key for the Findables definitions.
111
+	 *
112
+	 * @since 0.1.0
113
+	 *
114
+	 * @return string Config key use to define the Findables.
115
+	 */
116
+	protected function getFindablesConfigKey(): string
117
+	{
118
+		return 'Findables';
119
+	}
120
+
121
+	/**
122
+	 * Get the config key for the NullObject definitions.
123
+	 *
124
+	 * @since 0.1.0
125
+	 *
126
+	 * @return string Config key use to define the NullObject.
127
+	 */
128
+	protected function getNullObjectConfigKey(): string
129
+	{
130
+		return 'NullObject';
131
+	}
132
+
133
+	/**
134
+	 * Initialize the NullObject.
135
+	 *
136
+	 * @since 0.1.1
137
+	 *
138
+	 * @param mixed $arguments Optional. Arguments to use.
139
+	 *
140
+	 * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
141
+	 */
142
+	protected function initializeNullObject($arguments = null)
143
+	{
144
+		$this->nullObject = $this->maybeInstantiateFindable($this->nullObject, $arguments);
145
+	}
146
+
147
+	/**
148
+	 * Initialize the Findables that can be iterated.
149
+	 *
150
+	 * @param mixed $arguments Optional. Arguments to use.
151
+	 *
152
+	 * @since 0.1.0
153
+	 *
154
+	 * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
155
+	 */
156
+	protected function initializeFindables($arguments = null)
157
+	{
158
+		$this->findables = $this->findables->map(function ($findable) use ($arguments) {
159
+			return $this->initializeFindable($findable, $arguments);
160
+		});
161
+	}
162
+
163
+	/**
164
+	 * Initialize a single findable by instantiating class name strings and calling closures.
165
+	 *
166
+	 * @since 0.1.0
167
+	 *
168
+	 * @param mixed $findable  Findable to instantiate.
169
+	 * @param mixed $arguments Optional. Arguments to use.
170
+	 *
171
+	 * @return Findable Instantiated findable.
172
+	 * @throws FailedToInstantiateFindable If the Findable could not be instantiated.
173
+	 */
174
+	protected function initializeFindable($findable, $arguments = null): Findable
175
+	{
176
+		return $this->maybeInstantiateFindable($findable, $arguments);
177
+	}
178
+
179
+	/**
180
+	 * Maybe instantiate a Findable if it is not yet an object.
181
+	 *
182
+	 * @since 0.1.1
183
+	 *
184
+	 * @param mixed $findable  Findable to instantiate.
185
+	 * @param mixed $arguments Optional. Arguments to use.
186
+	 *
187
+	 * @return Findable Instantiated findable.
188
+	 * @throws FailedToInstantiateFindable If the findable could not be instantiated.
189
+	 */
190
+	protected function maybeInstantiateFindable($findable, $arguments = null): Findable
191
+	{
192
+		if (is_string($findable)) {
193
+			$findable = $this->instantiateFindableFromString($findable, $arguments);
194
+		}
195
+
196
+		if (is_callable($findable)) {
197
+			$findable = $this->instantiateFindableFromCallable($findable, $arguments);
198
+		}
199
+
200
+		if (! $findable instanceof Findable) {
201
+			throw new FailedToInstantiateFindable(
202
+				sprintf(
203
+					_('Could not instantiate Findable "%s".'),
204
+					serialize($findable)
205
+				)
206
+			);
207
+		}
208
+
209
+		return $findable;
210
+	}
211
+
212
+	/**
213
+	 * Instantiate a Findable from a string.
214
+	 *
215
+	 * @since 0.1.1
216
+	 *
217
+	 * @param string $string    String to use for instantiation.
218
+	 * @param mixed  $arguments Optional. Arguments to use for instantiation.
219
+	 *
220
+	 * @return Findable Instantiated Findable.
221
+	 */
222
+	protected function instantiateFindableFromString(string $string, $arguments = []): Findable
223
+	{
224
+		return new $string(...(array)$arguments);
225
+	}
226
+
227
+	/**
228
+	 * Instantiate a Findable from a callable.
229
+	 *
230
+	 * @since 0.1.1
231
+	 *
232
+	 * @param callable $callable  Callable to use for instantiation.
233
+	 * @param mixed    $arguments Optional. Arguments to use for instantiation.
234
+	 *
235
+	 * @return Findable Instantiated Findable.
236
+	 */
237
+	protected function instantiateFindableFromCallable(callable $callable, $arguments = []): Findable
238
+	{
239
+		return $callable(...(array)$arguments);
240
+	}
241 241
 }
Please login to merge, or discard this patch.