1 | <?php |
||
24 | class Route extends Component |
||
25 | { |
||
26 | /** |
||
27 | * The CSRF claim identifier |
||
28 | */ |
||
29 | const CLAIM_ROUTE = 'route'; |
||
30 | |||
31 | /** |
||
32 | * Issue an authorization JWT token on behalf of a user. |
||
33 | * |
||
34 | * An $action may come in the form of: |
||
35 | * |
||
36 | * STRING - Route to a controller action |
||
37 | * 'action/path' |
||
38 | * |
||
39 | * ARRAY - Route to a template |
||
40 | * ['templates/render', ['template' => 'template/path']] |
||
41 | * |
||
42 | * ARRAY w/ PARAMS - Route to a controller action with params |
||
43 | * ['action/path', [ |
||
44 | * 'foo' => 'bar' |
||
45 | * ]] |
||
46 | * |
||
47 | * @param string|array $action |
||
48 | * @param string|int|IdentityInterface $user |
||
49 | * @param string|null $audience |
||
50 | * @param int|null $expiration |
||
51 | * @return Token|null |
||
52 | * @throws \craft\errors\SiteNotFoundException |
||
53 | * @throws \yii\base\InvalidConfigException |
||
54 | */ |
||
55 | public function issue( |
||
79 | |||
80 | /** |
||
81 | * @param string $token |
||
82 | * @param bool $assumeIdentity |
||
83 | * @return string|array |
||
84 | * @throws \craft\errors\SiteNotFoundException |
||
85 | */ |
||
86 | public function claim(string $token, bool $assumeIdentity = true) |
||
99 | |||
100 | /** |
||
101 | * @param Token $token |
||
102 | * @return null|IdentityInterface |
||
103 | */ |
||
104 | private function tokenIdentity(Token $token) |
||
112 | |||
113 | /** |
||
114 | * @param $token |
||
115 | * @param bool $validate |
||
116 | * @param bool $verify |
||
117 | * @return Token|null |
||
118 | * @throws \craft\errors\SiteNotFoundException |
||
119 | */ |
||
120 | public function parse(string $token, bool $validate = true, bool $verify = true) |
||
132 | |||
133 | /** |
||
134 | * @param Token $token |
||
135 | * @return bool |
||
136 | * @throws \craft\errors\SiteNotFoundException |
||
137 | */ |
||
138 | public function verifyToken(Token $token): bool |
||
150 | |||
151 | /** |
||
152 | * @param string|null $audience |
||
153 | * @return string |
||
154 | * @throws \craft\errors\SiteNotFoundException |
||
155 | */ |
||
156 | private function resolveAudience(string $audience = null): string |
||
164 | |||
165 | /** |
||
166 | * @param int|null $expiration |
||
167 | * @return int |
||
168 | * @throws \yii\base\InvalidConfigException |
||
169 | */ |
||
170 | private function resolveTokenExpiration(int $expiration = null): int |
||
178 | } |
||
179 |
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.