Skip to content

Lord of the strings #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/program.php
/solution
/exercises/*/solution/vendor
/test/solutions/*/*/vendor
.phpunit.result.cache
2 changes: 2 additions & 0 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpSchool\PHP8Appreciate\Exercise\CautionWithCatches;
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
use PhpSchool\PHP8Appreciate\Exercise\LordOfTheStrings;
use PhpSchool\PhpWorkshop\Application;

$app = new Application('PHP8 Appreciate', __DIR__ . '/config.php');
Expand All @@ -31,6 +32,7 @@
$app->addExercise(HaveTheLastSay::class);
$app->addExercise(PhpPromotion::class);
$app->addExercise(CautionWithCatches::class);
$app->addExercise(LordOfTheStrings::class);

$art = <<<ART
_ __ _
Expand Down
6 changes: 5 additions & 1 deletion app/config.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

use PhpSchool\PHP8Appreciate\AstService;
use PhpSchool\PHP8Appreciate\Exercise\AMatchMadeInHeaven;
use PhpSchool\PHP8Appreciate\Exercise\CautionWithCatches;
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
use PhpSchool\PHP8Appreciate\Exercise\LordOfTheStrings;
use Psr\Container\ContainerInterface;

use function DI\create;
use function DI\factory;
use function DI\object;
Expand All @@ -26,4 +27,7 @@
CautionWithCatches::class => function (ContainerInterface $c) {
return new CautionWithCatches($c->get(PhpParser\Parser::class), $c->get(\Faker\Generator::class));
},
LordOfTheStrings::class => function (ContainerInterface $c) {
return new LordOfTheStrings($c->get(\Faker\Generator::class));
},
];
8 changes: 4 additions & 4 deletions composer.lock

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

66 changes: 66 additions & 0 deletions exercises/lord-of-the-strings/problem/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
PHP has a huge standard library with the most extravagant and highly specific string and array functions available.
`levenshtein` & `array_change_key_case` anyone??

On the flip side, historically it has missed some very common string operations. There's always been a way to get the same results, but they are cumbersome and error prone.

For example, if you want to check if a string contains a string you might use `strpos`. Eg:

```php
if (strpos('colourful', 'colour')) {
}
```
Well, this check will fail because `strpos` will return `0` which will be interpreted as false. You will need to adapt that check to account for 0 with `strpos('colourful', 'colour') !== false` where the strict comparison is important.

Thankfully, PHP 8 has done away with all that nonsense. Please welcome the following functions in to the world of PHP:

* `str_contains`
* `str_starts_with`
* `str_ends_with`

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

Create a program the accepts two arguments, the first is a word, the second is a sentence (random words).

You must print a table which contains the results of each of the above functions with the provided word and sentence.

* First, does the sentence contain the word?
* Second, does the sentence start with the word?
* Lastly, does the sentence end with the word?

You will use the composer package `symfony/console` to print a table using the `Table` helper.

The table headers should be `Function` & `Result`

There should be three rows, one for each function. The function column should contain the function name, eg `str_contains`.
The result column should be `true` or `false` based on the result of the corresponding function call.

### The advantages of the new string functions

* Simpler and more concise.
* Saner return types.
* It is harder to get their usage wrong, for example checking for 0 vs false with `strpos`
* The functions are faster, being implemented in C.
* The operations require less function calls, for example no usages of `strlen` are required.

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

Point your browser to [https://getcomposer.org/doc/00-intro.md](https://getcomposer.org/doc/00-intro.md) which will walk you through **Installing Composer** if you haven't already!

Use `composer init` to create your `composer.json` file with interactive search.

For more details look at the docs for...

**Composer** - [https://getcomposer.org/doc/01-basic-usage.md](https://getcomposer.org/doc/01-basic-usage.md)
**Symfony Console** - [https://symfony.com/doc/current/components/console.html](https://symfony.com/doc/current/components/console.html)
**str_contains** - [https://www.php.net/manual/en/function.str-contains.php](https://www.php.net/manual/en/function.str-contains.php)
**str_starts_with** - [https://www.php.net/manual/en/function.str-starts-with.php](https://www.php.net/manual/en/function.str-starts-with.php)
**str_ends_with** - [https://www.php.net/manual/en/function.str-ends-with.php](https://www.php.net/manual/en/function.str-ends-with.php)

For Symfony Console you will want to look specifically for the Table Helper.

Oh, and don't forget to use the Composer autoloader with:

```php
require_once __DIR__ . '/vendor/autoload.php';
```
7 changes: 7 additions & 0 deletions exercises/lord-of-the-strings/solution/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "php8-appreciate/lord-of-the-strings",
"description": "String manipulation with Composer",
"require": {
"symfony/console": "^5"
}
}
Loading