Skip to content

Commit

Permalink
Created AbstractTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
moura137 committed Aug 28, 2016
1 parent 1f8a7f5 commit 370c5e0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/NwLaravel/Transformer/AbstractTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace NwLaravel\Transformer;

use League\Fractal\TransformerAbstract;
use \DateTime;

/**
* Class AbstractTransformer
* @abstract
*/
abstract class AbstractTransformer extends TransformerAbstract
{
/**
* @var boolean
*/
protected $includeData = false;

/**
* Construct
*
* @param boolean $includeData Boolean Include Data
*/
public function __construct($includeData = false)
{
$this->includeData = $includeData;
}

/**
* Has Include Data
*
* @return boolean
*/
public function hasIncludeData()
{
return $this->includeData;
}

/**
* Format Date
*
* @param DateTime $date Date Time
* @param string $format String Format
*
* @return string
*/
public function formatDate($date, $format)
{
if ($date instanceof DateTime) {
return $date->format($format);
}

return null;
}
}
26 changes: 26 additions & 0 deletions tests/Transformer/AbstractTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Tests\Transformer;

use Tests\TestCase;
use Mockery as m;
use NwLaravel\Transformer\AbstractTransformer;

class AbstractTransformerTest extends TestCase
{
public function testConstructInstanceOf()
{
$transformer = m::mock(AbstractTransformer::class.'[]');
$this->assertAttributeEquals(false, 'includeData', $transformer);
$this->assertFalse($transformer->hasIncludeData());
}

public function testMethods()
{
$transformer = m::mock(AbstractTransformer::class.'[]', [true]);

$this->assertAttributeEquals(true, 'includeData', $transformer);
$this->assertTrue($transformer->hasIncludeData());
$this->assertNull($transformer->formatDate('', 'd/m/Y'));
$this->assertEquals('04/12/1982', $transformer->formatDate(new \Datetime('1982-12-04'), 'd/m/Y'));
}
}

0 comments on commit 370c5e0

Please sign in to comment.