1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
4
|
|
|
|
5
|
|
|
use Faker\Generator; |
6
|
|
|
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck; |
7
|
|
|
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
8
|
|
|
use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
9
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
10
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
11
|
|
|
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; |
12
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
13
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck; |
14
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ArrayWeGo |
19
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
20
|
|
|
* @author Michael Woodward <[email protected]> |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
class ArrayWeGo extends AbstractExercise implements ExerciseInterface, FunctionRequirementsExerciseCheck, CliExercise |
23
|
|
|
{ |
24
|
|
|
use TemporaryDirectoryTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Filesystem |
28
|
|
|
*/ |
29
|
|
|
private $filesystem; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Generator |
33
|
|
|
*/ |
34
|
|
|
private $faker; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Filesystem $filesystem |
38
|
|
|
* @param Generator $faker |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Filesystem $filesystem, Generator $faker) |
41
|
|
|
{ |
42
|
|
|
$this->filesystem = $filesystem; |
43
|
|
|
$this->faker = $faker; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getName() |
50
|
|
|
{ |
51
|
|
|
return 'Array We Go!'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getDescription() |
58
|
|
|
{ |
59
|
|
|
return 'Filter an array of file paths and map to SplFile objects'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function getArgs() |
66
|
|
|
{ |
67
|
|
|
$this->filesystem->mkdir($this->getTemporaryPath()); |
68
|
|
|
|
69
|
|
|
$fileCount = rand(2, 10); |
70
|
|
|
$realFiles = rand(1, $fileCount - 1); |
71
|
|
|
|
72
|
|
|
$files = []; |
73
|
|
|
foreach (range(1, $fileCount) as $index) { |
74
|
|
|
$file = sprintf('%s/%s.txt', $this->getTemporaryPath(), $this->faker->uuid); |
75
|
|
|
if ($index <= $realFiles) { |
76
|
|
|
$this->filesystem->touch($file); |
77
|
|
|
} |
78
|
|
|
$files[] = $file; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $files; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return null |
86
|
|
|
*/ |
87
|
|
|
public function tearDown() |
88
|
|
|
{ |
89
|
|
|
$this->filesystem->remove($this->getTemporaryPath()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string[] |
94
|
|
|
*/ |
95
|
|
|
public function getRequiredFunctions() |
96
|
|
|
{ |
97
|
|
|
return ['array_shift', 'array_filter', 'array_map']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string[] |
102
|
|
|
*/ |
103
|
|
|
public function getBannedFunctions() |
104
|
|
|
{ |
105
|
|
|
return ['basename']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return ExerciseType |
110
|
|
|
*/ |
111
|
|
|
public function getType() |
112
|
|
|
{ |
113
|
|
|
return ExerciseType::CLI(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param ExerciseDispatcher $dispatcher |
118
|
|
|
*/ |
119
|
|
|
public function configure(ExerciseDispatcher $dispatcher) |
120
|
|
|
{ |
121
|
|
|
$dispatcher->requireCheck(FunctionRequirementsCheck::class, $dispatcher::CHECK_AFTER); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|