Skip to content

Commit

Permalink
Working full static Formatter class
Browse files Browse the repository at this point in the history
  • Loading branch information
franatieu committed Jul 18, 2016
1 parent 7554866 commit 0ab0600
Show file tree
Hide file tree
Showing 2 changed files with 308 additions and 42 deletions.
57 changes: 20 additions & 37 deletions src/anekdotes/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,45 @@
*/
class Formatter
{
/**
* Provided Input. Uses a key->value format.
*
* @var string[]
*/
private $items;

/**
* Formatting Rules to be used. The key represents the key of the Input to format. The value associated to said key is a list of strings representing which rules to use to format the input's value.
*
* @var array[]
*/
private $rules;

/**
* Generates an instance of a Formater, using the provided items and rules.
* Format everything in Formater according to rules.
*
* @param string[] $items Provided Input, contains key->values pair to be formatted
* @param array[] $rules Formatting Rules to be used. The key represents the key of the Input to format. The value associated to said key is a list of strings representing which rules to use to format the input's value.
*
* @return Formater Generated Formated instance
* @return string[] The input given on formatter creation, now formatted
*/
public static function make($items, $rules)
{
$Formater = new Formater();
$Formater->items = $items;
$Formater->rules = $rules;

return $Formater;
}
if (!is_array($items) || !is_array($rules)){
return false;
}
if (empty($items) || empty($rules)){
return false;
}

/**
* Format everything in Formater according to rules.
*
* @return string[] The input given on formatter creation, now formatted
*/
public function format()
{
$mergedParams = [];
foreach ($this->rules as $itemName => $ruleNames) {
foreach ($rules as $itemName => $ruleNames) {
foreach ($ruleNames as $rule) {
$ruleParams = explode(':', $rule);
$rule = $ruleParams[0];
array_splice($ruleParams, 0, 1);
if (array_key_exists($itemName, $this->items)) {
$mergedParams[] = $this->items[$itemName];
if (array_key_exists($itemName, $items)) {
$mergedParams[] = $items[$itemName];
if (count($ruleParams) > 0) {
$ruleParams = explode(',', $ruleParams[0]);
$mergedParams = array_merge($mergedParams, $ruleParams);
}
$this->items[$itemName] = call_user_func_array([$this, $rule], $mergedParams);
$items[$itemName] = call_user_func_array(['Anekdotes\Formatter\Formatter', $rule], $mergedParams);
} else {
// item doesn't exist
}
array_splice($mergedParams, 0, count($mergedParams));
}
}

return $this->items;
return $items;
}

/**
Expand All @@ -80,12 +60,15 @@ public function format()
public static function postalCode($value)
{
if (strlen($value) > 0) {
$value = str_replace(' ', '', $value);
$value = substr($value, 0, 3).' '.substr($value, 3, 3);
$value = Str::upper($value);
$new = str_replace(' ', '', $value);
$new = substr($value, 0, 3);
if(strlen($value) > 3){
$new .= ' '.substr($value, 3, 3);
}
$new = Str::upper($new);
}

return $value;
return $new;
}

/**
Expand Down
293 changes: 288 additions & 5 deletions tests/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,294 @@

class FormatterTest extends PHPUnit_Framework_TestCase
{
//Tests the instantion of the formatter
public function testInstantiateFormatter()
//Tests the make method of the formatter
public function testFormatterMake1()
{
$stub = $this->createMock(MailerAdapter::class);
$mailer = new Mailer($stub);
$this->assertInstanceOf(Mailer::class, $mailer);
$formatter = Formatter::make([], []);
$this->assertFalse($formatter);
}

//Tests the make method of the formatter
public function testFormatterMake2()
{
$formatter = Formatter::make('foo', 'bar');
$this->assertFalse($formatter);
}

//Tests the make method of the formatter
public function testFormatterMake3()
{
$formatter = Formatter::make('foo', 'bar');
$this->assertFalse($formatter);
}

//Tests the phoneNumber of format method of the formatter
public function testFormatterFormatPhone1()
{
$values = [
'foo' => 'bar'
];
$rules = [
'foo' => ['phoneNumber']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => ''
]);
}

//Tests the phoneNumber of format method of the formatter
public function testFormatterFormatPhone2()
{
$values = [
'foo' => '450'
];
$rules = [
'foo' => ['phoneNumber']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '450'
]);
}

//Tests the phoneNumber of format method of the formatter
public function testFormatterFormatPhone3()
{
$values = [
'foo' => '4504441'
];
$rules = [
'foo' => ['phoneNumber']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '450-4441'
]);
}

//Tests the phoneNumber of format method of the formatter
public function testFormatterFormatPhone4()
{
$values = [
'foo' => '4504441919'
];
$rules = [
'foo' => ['phoneNumber']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '(450) 444-1919'
]);
}

//Tests the phoneNumber of format method of the formatter
public function testFormatterFormatPhone5()
{
$values = [
'foo' => '14504441919'
];
$rules = [
'foo' => ['phoneNumber']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '1 (450) 444-1919'
]);
}

//Tests the phoneNumber of format method of the formatter
public function testFormatterFormatPhone6()
{
$values = [
'foo' => '145044419192'
];
$rules = [
'foo' => ['phoneNumber']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '145 (044) 419-192'
]);
}

//Tests the postalCode of format method of the formatter
public function testFormatterFormatPostal1()
{
$values = [
'foo' => 'j4y2b4'
];
$rules = [
'foo' => ['postalCode']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => 'J4Y 2B4'
]);
}

//Tests the postalCode of format method of the formatter
public function testFormatterFormatPostal2()
{
$values = [
'foo' => 'j4y'
];
$rules = [
'foo' => ['postalCode']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => 'J4Y'
]);
}

//Tests the postalCode of format method of the formatter
public function testFormatterFormatPostal3()
{
$values = [
'foo' => 'j4y2b4424'
];
$rules = [
'foo' => ['postalCode']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => 'J4Y 2B4'
]);
}

//Tests the float of format method of the formatter
public function testFormatterFormatFloat1()
{
$values = [
'foo' => '122.2ABD'
];
$rules = [
'foo' => ['float']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '122.2'
]);
}

//Tests the float of format method of the formatter
public function testFormatterFormatFloat2()
{
$values = [
'foo' => 'a122.2a'
];
$rules = [
'foo' => ['float']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '0.0'
]);
}

//Tests the float of format method of the formatter
public function testFormatterFormatFloat3()
{
$values = [
'foo' => '123'
];
$rules = [
'foo' => ['float']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '123'
]);
}

//Tests the int of format method of the formatter
public function testFormatterFormatInt1()
{
$values = [
'foo' => '123'
];
$rules = [
'foo' => ['int']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '123'
]);
}

//Tests the int of format method of the formatter
public function testFormatterFormatInt2()
{
$values = [
'foo' => '123a'
];
$rules = [
'foo' => ['int']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '123'
]);
}

//Tests the int of format method of the formatter
public function testFormatterFormatInt3()
{
$values = [
'foo' => '123.12a'
];
$rules = [
'foo' => ['int']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '123'
]);
}

//Tests the integer of format method of the formatter
public function testFormatterFormatInteger1()
{
$values = [
'foo' => '123.12a'
];
$rules = [
'foo' => ['int']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '123'
]);
}

//Tests the datetime of format method of the formatter
public function testFormatterFormatDate1()
{
$values = [
'foo' => '1231'
];
$rules = [
'foo' => ['datetime']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => '1231'
]);
}

//Tests the datetime of format method of the formatter
public function testFormatterFormatDate2()
{
$values = [
'foo' => ''
];
$rules = [
'foo' => ['datetime']
];
$newValues = Formatter::make($values, $rules);
$this->assertEquals($newValues, [
'foo' => null
]);
}
}

0 comments on commit 0ab0600

Please sign in to comment.