1 | <?php namespace Arcanedev\Color\Rules; |
||
11 | abstract class AbstractColorRule implements Rule |
||
12 | { |
||
13 | /* ----------------------------------------------------------------- |
||
14 | | Properties |
||
15 | | ----------------------------------------------------------------- |
||
16 | */ |
||
17 | |||
18 | /** @var string */ |
||
19 | protected $key = ''; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | * |
||
24 | * @link http://stackoverflow.com/questions/12385500/regex-pattern-for-rgb-rgba-hsl-hsla-color-coding |
||
25 | */ |
||
26 | protected $pattern = ''; |
||
27 | |||
28 | /* ----------------------------------------------------------------- |
||
29 | | Main Methods |
||
30 | | ----------------------------------------------------------------- |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * Determine if the validation rule passes. |
||
35 | * |
||
36 | * @param string $attribute |
||
37 | * @param mixed $value |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | 45 | public function passes($attribute, $value) |
|
45 | |||
46 | /** |
||
47 | * Get the validation error message. |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 30 | public function message() |
|
55 | |||
56 | /* ----------------------------------------------------------------- |
||
57 | | Other Methods |
||
58 | | ----------------------------------------------------------------- |
||
59 | */ |
||
60 | |||
61 | /** |
||
62 | * Check if match all. |
||
63 | * |
||
64 | * @param string $pattern |
||
65 | * @param string $value |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | 45 | protected function matchAll($pattern, $value) |
|
73 | } |
||
74 |
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.