1 | <?php namespace Arcanedev\LaravelSitemap; |
||
15 | class SitemapManager implements SitemapManagerContract |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Properties |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** @var \Illuminate\Support\Collection */ |
||
23 | protected $sitemaps; |
||
24 | |||
25 | /* ----------------------------------------------------------------- |
||
26 | | Constructor |
||
27 | | ----------------------------------------------------------------- |
||
28 | */ |
||
29 | |||
30 | /** |
||
31 | * SitemapManager constructor. |
||
32 | */ |
||
33 | 26 | public function __construct() |
|
37 | |||
38 | /* ----------------------------------------------------------------- |
||
39 | | Main Methods |
||
40 | | ----------------------------------------------------------------- |
||
41 | */ |
||
42 | |||
43 | /** |
||
44 | * Create and add a sitemap to the collection. |
||
45 | * |
||
46 | * @param string $name |
||
47 | * @param callable $callback |
||
48 | * |
||
49 | * @return self |
||
50 | */ |
||
51 | 2 | public function create($name, callable $callback) |
|
55 | |||
56 | /** |
||
57 | * Add a sitemap to the collection. |
||
58 | * |
||
59 | * @param string $name |
||
60 | * @param \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap $sitemap |
||
61 | * |
||
62 | * @return self |
||
63 | */ |
||
64 | 20 | public function add($name, SitemapContract $sitemap) |
|
70 | |||
71 | /** |
||
72 | * Get the sitemaps collection. |
||
73 | * |
||
74 | * @return \Illuminate\Support\Collection |
||
75 | */ |
||
76 | 10 | public function all() |
|
80 | |||
81 | /** |
||
82 | * Get a sitemap instance. |
||
83 | * |
||
84 | * @param string $name |
||
85 | * @param mixed|null $default |
||
86 | * |
||
87 | * @return mixed |
||
88 | */ |
||
89 | 10 | public function get($name, $default = null) |
|
93 | |||
94 | /** |
||
95 | * Check if a sitemap exists. |
||
96 | * |
||
97 | * @param string $name |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | 8 | public function has($name) |
|
105 | |||
106 | /** |
||
107 | * Remove a sitemap from the collection by key. |
||
108 | * |
||
109 | * @param string|array $names |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | 2 | public function forget($names) |
|
119 | |||
120 | /** |
||
121 | * Get the sitemaps count. |
||
122 | * |
||
123 | * @return int |
||
124 | */ |
||
125 | 14 | public function count() |
|
129 | |||
130 | /** |
||
131 | * Render the sitemaps. |
||
132 | * |
||
133 | * @param string $name |
||
134 | * |
||
135 | * @return string|null |
||
136 | */ |
||
137 | 10 | public function render($name = null) |
|
152 | |||
153 | /** |
||
154 | * Render the sitemap index. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | 4 | public function renderIndex() |
|
162 | |||
163 | /** |
||
164 | * Save the sitemaps. |
||
165 | * |
||
166 | * @param string $path |
||
167 | * @param string|null $name |
||
168 | * |
||
169 | * @return self |
||
170 | */ |
||
171 | 6 | public function save($path, $name = null) |
|
179 | |||
180 | /** |
||
181 | * Get the collection of items as a plain array. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | 4 | public function toArray() |
|
189 | |||
190 | /** |
||
191 | * Get the collection of sitemaps as JSON. |
||
192 | * |
||
193 | * @param int $options |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | 2 | public function toJson($options = 0) |
|
201 | |||
202 | /** |
||
203 | * Convert the object into something JSON serializable. |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | 2 | public function jsonSerialize() |
|
211 | |||
212 | /* ----------------------------------------------------------------- |
||
213 | | Other Methods |
||
214 | | ----------------------------------------------------------------- |
||
215 | */ |
||
216 | |||
217 | /** |
||
218 | * Render the view. |
||
219 | * |
||
220 | * @param string $view |
||
221 | * @param array $data |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public function renderView($view, array $data) |
||
233 | } |
||
234 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.