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