My attempts at exercism.org
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(php): proverb

cosmeak 5b7f32aa 5d4e9231

+150
+21
php/proverb/Proverb.php
··· 1 + <?php 2 + 3 + declare(strict_types=1); 4 + 5 + class Proverb 6 + { 7 + public function recite(array $pieces): array 8 + { 9 + $output = []; 10 + 11 + for ($i = 0; $i < count($pieces); $i++) { 12 + if ($i == count($pieces) - 1 || count($pieces) == 1) { 13 + $output[] = "And all for the want of a {$pieces[0]}."; 14 + } else { 15 + $output[] = "For want of a {$pieces[$i]} the {$pieces[$i + 1]} was lost."; 16 + } 17 + } 18 + 19 + return $output; 20 + } 21 + }
+101
php/proverb/ProverbTest.php
··· 1 + <?php 2 + 3 + /* 4 + * By adding type hints and enabling strict type checking, code can become 5 + * easier to read, self-documenting and reduce the number of potential bugs. 6 + * By default, type declarations are non-strict, which means they will attempt 7 + * to change the original type to match the type specified by the 8 + * type-declaration. 9 + * 10 + * In other words, if you pass a string to a function requiring a float, 11 + * it will attempt to convert the string value to a float. 12 + * 13 + * To enable strict mode, a single declare directive must be placed at the top 14 + * of the file. 15 + * This means that the strictness of typing is configured on a per-file basis. 16 + * This directive not only affects the type declarations of parameters, but also 17 + * a function's return type. 18 + * 19 + * For more info review the Concept on strict type checking in the PHP track 20 + * <link>. 21 + * 22 + * To disable strict typing, comment out the directive below. 23 + */ 24 + 25 + declare(strict_types=1); 26 + 27 + use PHPUnit\Framework\TestCase; 28 + 29 + class ProverbTest extends TestCase 30 + { 31 + private Proverb $proverb; 32 + 33 + public static function setUpBeforeClass(): void 34 + { 35 + require_once 'Proverb.php'; 36 + } 37 + 38 + public function setUp(): void 39 + { 40 + $this->proverb = new Proverb(); 41 + } 42 + 43 + public function testNoVerses(): void 44 + { 45 + $pieces = []; 46 + $expected = []; 47 + $this->assertEquals($expected, $this->proverb->recite($pieces)); 48 + } 49 + 50 + public function testOneVerse(): void 51 + { 52 + $pieces = ['nail']; 53 + $expected = ['And all for the want of a nail.']; 54 + $this->assertEquals($expected, $this->proverb->recite($pieces)); 55 + } 56 + 57 + public function testTwoVerses(): void 58 + { 59 + $pieces = ['nail', 'shoe']; 60 + $expected = ['For want of a nail the shoe was lost.', 'And all for the want of a nail.']; 61 + $this->assertEquals($expected, $this->proverb->recite($pieces)); 62 + } 63 + 64 + public function testThreeVerses(): void 65 + { 66 + $pieces = ['nail', 'shoe', 'horse']; 67 + $expected = [ 68 + 'For want of a nail the shoe was lost.', 69 + 'For want of a shoe the horse was lost.', 70 + 'And all for the want of a nail.' 71 + ]; 72 + $this->assertEquals($expected, $this->proverb->recite($pieces)); 73 + } 74 + 75 + public function testFullProverb(): void 76 + { 77 + $pieces = ['nail', 'shoe', 'horse', 'rider', 'message', 'battle', 'kingdom']; 78 + $expected = [ 79 + 'For want of a nail the shoe was lost.', 80 + 'For want of a shoe the horse was lost.', 81 + 'For want of a horse the rider was lost.', 82 + 'For want of a rider the message was lost.', 83 + 'For want of a message the battle was lost.', 84 + 'For want of a battle the kingdom was lost.', 85 + 'And all for the want of a nail.' 86 + ]; 87 + $this->assertEquals($expected, $this->proverb->recite($pieces)); 88 + } 89 + 90 + public function testFourModernizedVerses(): void 91 + { 92 + $pieces = ['pin', 'gun', 'soldier', 'battle']; 93 + $expected = [ 94 + 'For want of a pin the gun was lost.', 95 + 'For want of a gun the soldier was lost.', 96 + 'For want of a soldier the battle was lost.', 97 + 'And all for the want of a pin.' 98 + ]; 99 + $this->assertEquals($expected, $this->proverb->recite($pieces)); 100 + } 101 + }
+28
php/proverb/README.md
··· 1 + # Proverb 2 + 3 + Welcome to Proverb on Exercism's PHP Track. 4 + If you need help running the tests or submitting your code, check out `HELP.md`. 5 + 6 + ## Instructions 7 + 8 + For want of a horseshoe nail, a kingdom was lost, or so the saying goes. 9 + 10 + Given a list of inputs, generate the relevant proverb. For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme: 11 + 12 + ```text 13 + For want of a nail the shoe was lost. 14 + For want of a shoe the horse was lost. 15 + For want of a horse the rider was lost. 16 + For want of a rider the message was lost. 17 + For want of a message the battle was lost. 18 + For want of a battle the kingdom was lost. 19 + And all for the want of a nail. 20 + ``` 21 + 22 + Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. **No line of the output text should be a static**, unchanging string; all should vary according to the input given. 23 + 24 + ## Source 25 + 26 + ### Created by 27 + 28 + - @MichaelBunker