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