Skip to content

Commit

Permalink
ArrayedObjectInstantiator and ArrayedObjectFactory singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyoak committed Jun 17, 2015
1 parent 7507307 commit 958dbb2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/DataType/ArrayedObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@

class ArrayedObjectFactory
{
private function __construct()
{
}

public static function instance()
{
static $instance;

if (null === $instance) {
$instance = new static();
}

return $instance;
}

public function create($object)
{
return new ArrayedObject(get_class($object), $this->arrayzeObjectVars($object));
Expand Down
15 changes: 15 additions & 0 deletions src/DataType/ArrayedObjectInstantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ class ArrayedObjectInstantiator
\DateInterval::class => 'createDateIntervalInstance',
];

private function __construct()
{
}

public static function instance()
{
static $instance;

if (null === $instance) {
$instance = new static();
}

return $instance;
}

/**
* @param ArrayedObject $arrayedObject
*
Expand Down
11 changes: 10 additions & 1 deletion test/DataType/ArrayedObjectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ class ArrayedObjectFactoryTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->factory = new ArrayedObjectFactory();
$this->factory = ArrayedObjectFactory::instance();
}


/** @test */
public function shouldBeSingleton()
{
$isConstructorCallable = is_callable([$this->factory, '__construct']);

$this->assertFalse($isConstructorCallable);
}

/** @test */
Expand Down
10 changes: 9 additions & 1 deletion test/DataType/ArrayedObjectInstantiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ class ArrayedObjectInstantiatorTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->instantiator = new ArrayedObjectInstantiator();
$this->instantiator = ArrayedObjectInstantiator::instance();
}

/** @test */
public function shouldBeSingleton()
{
$isConstructorCallable = is_callable([$this->instantiator, '__construct']);

$this->assertFalse($isConstructorCallable);
}

/** @test */
Expand Down

0 comments on commit 958dbb2

Please sign in to comment.