1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
4
|
|
|
|
5
|
|
|
use Faker\Generator; |
6
|
|
|
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
7
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
8
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\CgiOutputExerciseCheck; |
9
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck; |
10
|
|
|
use PhpSchool\PhpWorkshop\Solution\DirectorySolution; |
11
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
12
|
|
|
use Zend\Diactoros\Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DependencyHeaven |
16
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
17
|
|
|
* @author Michael Woodward <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class DependencyHeaven extends AbstractExercise implements |
20
|
|
|
ExerciseInterface, |
21
|
|
|
CgiOutputExerciseCheck, |
22
|
|
|
ComposerExerciseCheck |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Generator |
26
|
|
|
*/ |
27
|
|
|
private $faker; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Generator $faker |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Generator $faker) |
33
|
|
|
{ |
34
|
|
|
$this->faker = $faker; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getName() |
41
|
|
|
{ |
42
|
|
|
return 'Dependency Heaven'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getDescription() |
49
|
|
|
{ |
50
|
|
|
return 'An introduction to Composer dependency management'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return SolutionInterface |
55
|
|
|
*/ |
56
|
|
|
public function getSolution() |
57
|
|
|
{ |
58
|
|
|
return DirectorySolution::fromDirectory(__DIR__ . '/../../exercises/dependency-heaven/solution'); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return RequestInterface[] |
63
|
|
|
*/ |
64
|
|
|
public function getRequests() |
65
|
|
|
{ |
66
|
|
|
$requests = []; |
67
|
|
|
|
68
|
|
|
for ($i = 0; $i < rand(2, 5); $i++) { |
69
|
|
|
$requests[] = $this->newApiRequest('/reverse'); |
70
|
|
|
$requests[] = $this->newApiRequest('/swapcase'); |
71
|
|
|
$requests[] = $this->newApiRequest('/titleize'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $requests; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $endpoint |
79
|
|
|
* @return RequestInterface |
80
|
|
|
*/ |
81
|
|
|
private function newApiRequest($endpoint) |
82
|
|
|
{ |
83
|
|
|
$request = (new Request($endpoint)) |
84
|
|
|
->withMethod('POST') |
85
|
|
|
->withHeader('Content-Type', 'application/x-www-form-urlencoded'); |
86
|
|
|
|
87
|
|
|
$request->getBody()->write( |
88
|
|
|
http_build_query(['data' => $this->faker->sentence(rand(3, 6))]) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return $request; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getRequiredPackages() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
'klein/klein', |
101
|
|
|
'danielstjules/stringy' |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.