Skip to content

Commit

Permalink
Merge pull request #33 from matryoshka-model/develop
Browse files Browse the repository at this point in the history
README
  • Loading branch information
leodido committed Jul 4, 2015
2 parents 3e0acfc + 24c36b4 commit 1e46d22
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Last but not least, the Matryoshka design allows you to use just single componen

#### Community

For questions and support please visit the [slack channel](http://matryoshka-slackin.herokuapp.com).
For questions and support please visit the [slack channel](http://matryoshka.slack.com) (get an invite [here](http://matryoshka-slackin.herokuapp.com)).

## Theory of operation

Expand Down
8 changes: 8 additions & 0 deletions library/Object/PrototypeStrategy/CloneStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
namespace Matryoshka\Model\Object\PrototypeStrategy;

use Matryoshka\Model\Exception;

/**
* Class CloneStrategy
*
Expand All @@ -22,6 +24,12 @@ class CloneStrategy implements PrototypeStrategyInterface
*/
public function createObject($objectPrototype, array $context = null)
{
if (!is_object($objectPrototype)) {
throw new Exception\InvalidArgumentException(sprintf(
'Object prototype must be an object, given "%s"',
gettype($objectPrototype)
));
}
return clone $objectPrototype;
}
}
17 changes: 12 additions & 5 deletions library/Object/PrototypeStrategy/ServiceLocatorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
*/
namespace Matryoshka\Model\Object\PrototypeStrategy;

use Matryoshka\Model\Exception\ErrorException;
use Matryoshka\Model\Exception\RuntimeException;
use Matryoshka\Model\Exception;
use Matryoshka\Model\ModelAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand Down Expand Up @@ -135,12 +134,20 @@ public function getCloneObject()
* @param object $objectPrototype
* @param array $context
* @return array|object
* @throws ErrorException
* @throws Exception\ErrorException
* @throws Exception\RuntimeException
*/
public function createObject($objectPrototype, array $context = null)
{
if (!is_object($objectPrototype)) {
throw new Exception\InvalidArgumentException(sprintf(
'Object prototype must be an object, given "%s"',
gettype($objectPrototype)
));
}

if (!isset($context[$this->typeField])) {
throw new RuntimeException(sprintf(
throw new Exception\RuntimeException(sprintf(
'"%s" is not present within object data',
$this->typeField
));
Expand All @@ -149,7 +156,7 @@ public function createObject($objectPrototype, array $context = null)
$object = $this->serviceLocator->get($context[$this->typeField]);

if ($this->validateObject && !($object instanceof $objectPrototype)) {
throw new ErrorException(sprintf(
throw new Exception\ErrorException(sprintf(
'Object must be an instance of %s',
get_class($objectPrototype)
));
Expand Down
2 changes: 1 addition & 1 deletion library/ResultSet/HydratingResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function setObjectPrototype($objectPrototype)
if (!is_object($objectPrototype)) {
throw new Exception\InvalidArgumentException(
sprintf(
'An object must be set as the object prototype, a "%s" was provided.',
'Object prototype must be an object, given "%s"',
gettype($objectPrototype)
)
);
Expand Down
11 changes: 11 additions & 0 deletions tests/Object/PrototypeStrategy/CloneStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ public function testCreateObject()
$this->assertEquals($objectPrototype, $object);
$this->assertNotSame($objectPrototype, $object);
}

/**
* @expectedException \Matryoshka\Model\Exception\InvalidArgumentException
*/
public function testCreateObjectShouldThrowExceptionWhenNonObject()
{
$strategy = new CloneStrategy;
$objectPrototype = 1; // not an object

$object = $strategy->createObject($objectPrototype);
}
}
11 changes: 11 additions & 0 deletions tests/Object/PrototypeStrategy/ServiceLocatorStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public function testCreateObject()
$object = $strategy->createObject(new \stdClass(), $data);
}

/**
* @expectedException \Matryoshka\Model\Exception\InvalidArgumentException
*/
public function testCreateObjectShouldThrowExceptionWhenNonObject()
{
$strategy = new ServiceLocatorStrategy($this->serviceManager);
$objectPrototype = 1; // not an object

$object = $strategy->createObject($objectPrototype);
}

public function testCreateShouldThrowExceptionWhenFieldIsNotPresent()
{
$strategy = new ServiceLocatorStrategy($this->serviceManager);
Expand Down

0 comments on commit 1e46d22

Please sign in to comment.