|
1
|
|
|
<?php namespace Arcanedev\LaravelSitemap\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\ChangeFrequency as ChangeFrequencyContract; |
|
4
|
|
|
use Illuminate\Support\Collection; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class ChangeFrequency |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanedev\LaravelSitemap\Entities |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class ChangeFrequency implements ChangeFrequencyContract |
|
13
|
|
|
{ |
|
14
|
|
|
/* ----------------------------------------------------------------- |
|
15
|
|
|
| Main Methods |
|
16
|
|
|
| ----------------------------------------------------------------- |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get all the valid frequency keys. |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Illuminate\Support\Collection |
|
23
|
|
|
*/ |
|
24
|
10 |
|
public static function keys() |
|
25
|
|
|
{ |
|
26
|
10 |
|
return new Collection([ |
|
|
|
|
|
|
27
|
10 |
|
static::ALWAYS, |
|
28
|
10 |
|
static::HOURLY, |
|
29
|
10 |
|
static::DAILY, |
|
30
|
10 |
|
static::WEEKLY, |
|
31
|
10 |
|
static::MONTHLY, |
|
32
|
10 |
|
static::YEARLY, |
|
33
|
10 |
|
static::NEVER, |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get all the valid frequency values. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string|null $locale |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Support\Collection |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function all($locale = null) |
|
45
|
|
|
{ |
|
46
|
6 |
|
return static::keys()->mapWithKeys(function ($key) use ($locale) { |
|
|
|
|
|
|
47
|
6 |
|
return [$key => trans("sitemap::frequencies.$key", [], $locale)]; |
|
48
|
6 |
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the translated frequency name. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $key |
|
55
|
|
|
* @param string|null $default |
|
56
|
|
|
* @param string|null $locale |
|
57
|
|
|
* |
|
58
|
|
|
* @return string|null |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public static function get($key, $default = null, $locale = null) |
|
61
|
|
|
{ |
|
62
|
2 |
|
return static::all($locale)->get($key, $default); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Check if the given frequency exists. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public static function has($key) |
|
73
|
|
|
{ |
|
74
|
2 |
|
return static::keys()->flip()->has($key); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.