Skip to content

Commit

Permalink
Closes #30
Browse files Browse the repository at this point in the history
- ResultSet\PrototypeStrategy sub-package moved to Object\PrototypeStrategy
- Tests adapted
- ResultSet\PrototypeStrategy\Service\ServiceLocatorStrategyFactory leaved in place (for backward compatibility) but deprecated (it'll be removed soon)
- DateTimeStrategy revisited
  • Loading branch information
leodido committed Jun 24, 2015
1 parent f1c690b commit 3b55f3f
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 135 deletions.
17 changes: 11 additions & 6 deletions library/Hydrator/Strategy/DateTimeStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct($format = null)
* {@inheritdoc}
* Convert a string value into a DateTime object
*
* @param string|int|null $value
* @return DateTime|null
*/
public function hydrate($value)
Expand All @@ -46,23 +47,23 @@ public function hydrate($value)
return null;
}

if (is_string($value)) {
if (is_string($value) || is_int($value)) {
if ($dateTime = DateTime::createFromFormat($this->getFormat(), $value)) {
return $dateTime;
}

throw new Exception\InvalidArgumentException(sprintf(
'Invalid value: must be a string representing the time according to DateTime::createFromFormat(), "%s" given.',
'Invalid format or value: format must be a string representing a valid \DateTime format, "%s" given;' .
'value must be a string representing the time according to \DateTime::createFromFormat(), "%s" given.',
$this->getFormat(),
$value
));
}

throw new Exception\InvalidArgumentException(sprintf(
'Invalid value: must be a string representing the time, "%s" given',
'Invalid value: must be a string or integer representing the time according to the format, "%s" given',
is_object($value) ? get_class($value) : gettype($value)
));

return null;
}

/**
Expand All @@ -88,16 +89,20 @@ public function extract($value)
}

/**
* Set format
*
* @param string $format
* @return DateTimeStrategy
*/
public function setFormat($format)
{
$this->format = $format;
$this->format = (string) $format;
return $this;
}

/**
* Get format
*
* @return string
*/
public function getFormat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright Copyright (c) 2014-2015, Ripa Club
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace Matryoshka\Model\ResultSet\PrototypeStrategy;
namespace Matryoshka\Model\Object\PrototypeStrategy;

/**
* Class CloneStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright Copyright (c) 2014-2015, Ripa Club
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace Matryoshka\Model\ResultSet\PrototypeStrategy;
namespace Matryoshka\Model\Object\PrototypeStrategy;

/**
* Interface PrototypeStrategyInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Matryoshka
*
* @link https://github.com/matryoshka-model/matryoshka
* @copyright Copyright (c) 2014-2015, Ripa Club
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace Matryoshka\Model\Object\PrototypeStrategy\Service;

use Matryoshka\Model\Object\PrototypeStrategy\ServiceLocatorStrategy;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Class ServiceLocatatorStrategyFactory
*/
class ServiceLocatorStrategyFactory implements FactoryInterface
{
/**
* @var string
*/
protected $configKey = 'matryoshka-object-servicelocatorstrategy';

/**
* Config
* @var array
*/
protected $config;

/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $this->getConfig($serviceLocator);

if (isset($config['service_locator'])) {
$objectServiceLocator = $serviceLocator->get($config['service_locator']);
} else {
$objectServiceLocator = $serviceLocator->has('Matryoshka\Model\Object\ObjectManager') ?
$serviceLocator->get('Matryoshka\Model\Object\ObjectManager')
: $serviceLocator;
}

$strategy = new ServiceLocatorStrategy($objectServiceLocator);

if (isset($config['type_field'])) {
$strategy->setTypeField($config['type_field']);
}

if (isset($config['validate_object'])) {
$strategy->setValidateObject($config['validate_object']);
}

if (isset($config['clone_object'])) {
$strategy->setCloneObject($config['clone_object']);
}

return $strategy;
}

/**
* Get model configuration, if any
*
* @param ServiceLocatorInterface $serviceLocator
* @return array
*/
protected function getConfig(ServiceLocatorInterface $serviceLocator)
{
if ($this->config !== null) {
return $this->config;
}

if (!$serviceLocator->has('Config')) {
$this->config = [];
return $this->config;
}

$config = $serviceLocator->get('Config');
if (!isset($config[$this->configKey])
|| !is_array($config[$this->configKey])
) {
$this->config = [];
return $this->config;
}

$this->config = $config[$this->configKey];
return $this->config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright Copyright (c) 2014-2015, Ripa Club
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace Matryoshka\Model\ResultSet\PrototypeStrategy;
namespace Matryoshka\Model\Object\PrototypeStrategy;

use Matryoshka\Model\Exception\ErrorException;
use Matryoshka\Model\Exception\RuntimeException;
Expand Down
4 changes: 2 additions & 2 deletions library/ResultSet/HydratingResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

use ArrayObject;
use Matryoshka\Model\Exception;
use Matryoshka\Model\ResultSet\PrototypeStrategy\CloneStrategy;
use Matryoshka\Model\ResultSet\PrototypeStrategy\PrototypeStrategyInterface;
use Matryoshka\Model\Object\PrototypeStrategy\CloneStrategy;
use Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyInterface;
use Zend\Stdlib\Hydrator\ArraySerializable;
use Zend\Stdlib\Hydrator\HydratorAwareInterface;
use Zend\Stdlib\Hydrator\HydratorAwareTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,18 @@
*/
namespace Matryoshka\Model\ResultSet\PrototypeStrategy\Service;

use Matryoshka\Model\ResultSet\PrototypeStrategy\ServiceLocatorStrategy;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Matryoshka\Model\Object\PrototypeStrategy\Service\ServiceLocatorStrategyFactory as ObjectServiceLocatorStrategyFactory;

/**
* Class ServiceLocatatorStrategyFactory
*
* @deprecated
* NOTE: refactored to do not introduce breaking changes
*/
class ServiceLocatorStrategyFactory implements FactoryInterface
class ServiceLocatorStrategyFactory extends ObjectServiceLocatorStrategyFactory
{
/**
* @var string
*/
protected $configKey = 'matryoshka-resultset-servicelocatorstrategy';

/**
* Config
* @var array
*/
protected $config;

/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $this->getConfig($serviceLocator);

if (isset($config['service_locator'])) {
$objectServiceLocator = $serviceLocator->get($config['service_locator']);
} else {
$objectServiceLocator = $serviceLocator->has('Matryoshka\Model\Object\ObjectManager') ?
$serviceLocator->get('Matryoshka\Model\Object\ObjectManager')
: $serviceLocator;
}

$strategy = new ServiceLocatorStrategy($objectServiceLocator);

if (isset($config['type_field'])) {
$strategy->setTypeField($config['type_field']);
}

if (isset($config['validate_object'])) {
$strategy->setValidateObject($config['validate_object']);
}

if (isset($config['clone_object'])) {
$strategy->setCloneObject($config['clone_object']);
}

return $strategy;
}

/**
* Get model configuration, if any
*
* @param ServiceLocatorInterface $serviceLocator
* @return array
*/
protected function getConfig(ServiceLocatorInterface $serviceLocator)
{
if ($this->config !== null) {
return $this->config;
}

if (!$serviceLocator->has('Config')) {
$this->config = [];
return $this->config;
}

$config = $serviceLocator->get('Config');
if (!isset($config[$this->configKey])
|| !is_array($config[$this->configKey])
) {
$this->config = [];
return $this->config;
}

$this->config = $config[$this->configKey];
return $this->config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* @copyright Copyright (c) 2014-2015, Ripa Club
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace MatryoshkaTest\Model\ResultSet\PrototypeStrategy;
namespace MatryoshkaTest\Model\Object\PrototypeStrategy;

use Matryoshka\Model\ResultSet\PrototypeStrategy\CloneStrategy;
use Matryoshka\Model\Object\PrototypeStrategy\CloneStrategy;

/**
* Class CloneStrategyTest
Expand All @@ -18,8 +18,8 @@ class CloneStrategyTest extends \PHPUnit_Framework_TestCase

public function testCreateObject()
{
$strategy = new CloneStrategy();
$objectPrototype = new \stdClass();
$strategy = new CloneStrategy;
$objectPrototype = new \stdClass;

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

Expand Down
Loading

0 comments on commit 3b55f3f

Please sign in to comment.