Skip to content

Commit

Permalink
Merge pull request #17 from php-school/separation
Browse files Browse the repository at this point in the history
Concerned About Separation excercise + update format for new workshop…
  • Loading branch information
AydinHassan committed Dec 30, 2015
2 parents 6d39a62 + fe63ffd commit 93f6e0a
Show file tree
Hide file tree
Showing 37 changed files with 444 additions and 63 deletions.
2 changes: 2 additions & 0 deletions app/bootstrap.php
Expand Up @@ -19,6 +19,7 @@
throw new RuntimeException('Unable to locate Composer autoloader; please run "composer install".');
}

use PhpSchool\LearnYouPhp\Exercise\ConcernedAboutSeparation;
use PhpSchool\LearnYouPhp\Exercise\TimeServer;
use PhpSchool\LearnYouPhp\Exercise\DatabaseRead;
use PhpSchool\LearnYouPhp\Exercise\ExceptionalCoding;
Expand All @@ -36,6 +37,7 @@
$app->addExercise(BabySteps::class);
$app->addExercise(MyFirstIo::class);
$app->addExercise(FilteredLs::class);
$app->addExercise(ConcernedAboutSeparation::class);
$app->addExercise(ArrayWeGo::class);
$app->addExercise(ExceptionalCoding::class);
$app->addExercise(DatabaseRead::class);
Expand Down
9 changes: 9 additions & 0 deletions app/config.php
Expand Up @@ -3,8 +3,10 @@
use function DI\factory;
use function DI\object;
use Interop\Container\ContainerInterface;
use PhpParser\Parser;
use PhpSchool\LearnYouPhp\Exercise\ArrayWeGo;
use PhpSchool\LearnYouPhp\Exercise\BabySteps;
use PhpSchool\LearnYouPhp\Exercise\ConcernedAboutSeparation;
use PhpSchool\LearnYouPhp\Exercise\DatabaseRead;
use PhpSchool\LearnYouPhp\Exercise\ExceptionalCoding;
use PhpSchool\LearnYouPhp\Exercise\FilteredLs;
Expand All @@ -27,6 +29,13 @@
FilteredLs::class => factory(function (ContainerInterface $c) {
return new FilteredLs($c->get(Filesystem::class));
}),
ConcernedAboutSeparation::class => factory(function (ContainerInterface $c) {
return new ConcernedAboutSeparation(
$c->get(Filesystem::class),
FakerFactory::create(),
$c->get(Parser::class)
);
}),
ArrayWeGo::class => factory(function (ContainerInterface $c) {
return new ArrayWeGo($c->get(Filesystem::class), FakerFactory::create());
}),
Expand Down
103 changes: 53 additions & 50 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions exercises/concerned-about-separation/problem/problem.md
@@ -0,0 +1,52 @@
This problem is the same as the previous but introduces the concept of **classes**. You will need to create two files to solve this.

Create a program that prints a list of files in a given directory, filtered by the extension of the files. The first argument is the directory name and the second argument is the extension filter. Print the list of files (one file per line) to the console.

You must write a *class* file to do most of the work. The file must *define* a single class with a single function that takes **two** arguments: the directory name and the filename extension string in that order. The filename extension argument must be the same as what was passed to your program. Don't turn it into a regular expression or prefix with "." or do anything except pass it to your class method where you can do what you need to make your filter work.

You **must** not print directly to the console from your class, only from your original program.

The benefit of having a contract like this is that your class can be used by anyone who expects this contract. So your class could be used by anyone else who does learnyouphp, or the verifier, and just work.

----------------------------------------------------------------------
## HINTS

Create a new class by creating a new file that just contains your directory reading and filtering code in a class method. To define a *single method* *class*, use the following syntax:

```php
<?php

class DirectoryFilter
{
public function filter($args)
{
/** ... */
}
}
```

To use your new class in your original program file, use the `require_once` construct with the filename. So, if your file is named mymodule.php then:

```php
<?php
require_once __DIR__ . '/mymodule.php';
```

You can now create an instance of your class and assign it to a variable!

```php
<?php
$myFilter = new DirectoryFilter;
```

You can then call the method you defined with its required arguments.

Documentation on class basics can be found here:

[http://php.net/manual/en/language.oop5.basic.php]()

Documentation on `require_once` can be found here:

[http://php.net/manual/en/function.require-once.php]()

----------------------------------------------------------------------

0 comments on commit 93f6e0a

Please sign in to comment.