Nice PHP console application
Sometime you need to create command line interface (CLI) for your application and when you want to have it nicely formated, colorful or you want to have a table there it is not easy to do it. You can look on internet and find out, how to do such a thing, but you would have to write a lot of code.
Symfony console
With Symfony console you can do whatever you are used to see when working with console. E. g. tables, progress bars, questions, dialogs, etc. And it will help you with lot of other common things. It is very easy to add required or optional argument, create help or add another command.
New problem – Anagram
I want to continue on my previous article [What is and how to use Composer]. There was an example project where you have to solve if given string is palindrome or not. Now, we can expand it with new problem.
An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.
- Wikipedia
For example silent – listen, admirer – married, funeral – real fun. The solution involves a CLI where I can easily test if words are anagrams or palindromes. Of course there has to be help to each command and a hint how to use it.
Step by step
You can download the project from GitHub commit or go through mentioned article and create it yourself. You should end up with directory structure like this :
src
Palindrome.php
tests
PalindromeTest.php
vendor
…
composer.json
composer.lock
Again start with the tests. So create file AnagramTest.php in src directory. The class name will be Anagram
and method might be isAnagram
. So you have to test an real anagram and some other string which is not an anagram.
use src\Anagram;
class AnagramTest extends PHPUnit_Framework_TestCase {
private $anagram;
public function setUp() {
$this->anagram = new Anagram();
}
public function testSuitableString() {
$this->assertTrue($this->anagram->isAnagram('silent', 'listen'));
$this->assertTrue($this->anagram->isAnagram('I am Lord Voldemort', 'Tom Marvolo Riddle'));
}
public function testNotSuitableString() {
$this->assertFalse($this->anagram->isAnagram('anagram', 'palindrome'));
}
}
Anagram
There are many ways how to check if strings are anagram. One of them is to sort both strings and compare them. If they are equal they are anagram. To be sure, you can delete spaces and make the strings lowercase.
namespace src;
class Anagram {
/**
*
* @param string $str1
* @param string $str2
* @return boolean
*/
public function isAnagram( $str1, $str2 ) {
return $this->sortString($this->sanitizeString($str1)) === $this->sortString($this->sanitizeString($str2));
}
/**
* Removes all spaces and transorms letters to lowercase.
* @param string $str
* @return string
*/
private function sanitizeString( $str ) {
return \strtolower(\str_replace(' ', '', $str));
}
/**
* Sort characters and return them.
* @param string $str
* @return string
*/
private function sortString( $str ) {
$chars = str_split($str);
sort($chars);
return implode('', $chars);
}
}
Now run the test phpunit --bootstrap ./vendor/autoload.php ./tests/AnagramTest.php. The result should be OK (2 tests, 3 assertions)
and you may continue to create the CLI.