Skip to content

Commit

Permalink
Merge pull request #15 from matryoshka-model/develop
Browse files Browse the repository at this point in the history
v0.5.0
  • Loading branch information
leodido committed Oct 9, 2014
2 parents 8d35aec + 759e6ba commit 21ac331
Show file tree
Hide file tree
Showing 22 changed files with 1,540 additions and 112 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ matrix:
fast_finish: true
allow_failures:
- php: hhvm

notifications:
email:
- ripaclub@gmail.com
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
},
"suggest": {
"matryoshka-model/zf2-matryoshka-module": "ZF2 module for matryoshka library",
"matryoshka-model/mongo-wrapper": "MongoDB matryoshka wrapper"
"matryoshka-model/mongo-wrapper": "MongoDB matryoshka wrapper",
"matryoshka-model/rest-wrapper": "Matryoshka wrapper aimed at creating restful API clients"
}
}
4 changes: 2 additions & 2 deletions library/Matryoshka/Model/Hydrator/ClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class ClassMethods extends ZendClassMethods
/**
* {@inheritdoc}
*/
public function __construct()
public function __construct($underscoreSeparatedKeys = false)
{
parent::__construct();
parent::__construct($underscoreSeparatedKeys);
// Exclude this methods from the extraction
$this->filterComposite->addFilter('model', new MethodMatchFilter('getModel'), FilterComposite::CONDITION_AND);
$this->filterComposite->addFilter('hydrator', new MethodMatchFilter('getHydrator'), FilterComposite::CONDITION_AND);
Expand Down
147 changes: 147 additions & 0 deletions library/Matryoshka/Model/Object/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Matryoshka
*
* @link https://github.com/matryoshka-model/matryoshka
* @copyright Copyright (c) 2014, Copyright (c) 2014, Ripa Club
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace Matryoshka\Model\Object;

use Matryoshka\Model\Exception;
use Matryoshka\Model\ModelAwareInterface;
use Matryoshka\Model\ModelInterface;
use Matryoshka\Model\AbstractModel;
use Matryoshka\Model\Criteria\ActiveRecord\AbstractCriteria;
use Matryoshka\Model\ModelAwareTrait;

/**
*
*
*/
abstract class AbstractActiveRecord extends AbstractObject implements
ModelAwareInterface,
ActiveRecordInterface
{

use ModelAwareTrait;

/**
* @var AbstractCriteria
*/
protected $activeRecordCriteriaPrototype;

/**
* Set Active Record Criteria Prototype
*
* @param AbstractCriteria $criteria
* @return $this
*/
public function setActiveRecordCriteriaPrototype(AbstractCriteria $criteria)
{
$this->activeRecordCriteriaPrototype = $criteria;
return $this;
}


/**
* Set Model
*
* @param ModelInterface $model
* @return $this
*/
public function setModel(ModelInterface $model)
{
if (!$model instanceof AbstractModel) {
throw new Exception\InvalidArgumentException(
'AbstractModel required in order to work with ActiveRecord'
);
}
$this->model = $model;
return $this;
}

/**
* Save
*
* @return null|int
* @throws Exception\RuntimeException
*/
public function save()
{
if (!$this->activeRecordCriteriaPrototype) {
throw new Exception\RuntimeException('An Active Record Criteria Prototype must be set prior to calling save()');
}

if (!$this->getModel()) {
throw new Exception\RuntimeException('A Model must be set prior to calling save()');
}

$criteria = clone $this->activeRecordCriteriaPrototype;
$result = $this->getModel()->save($criteria, $this);
return $result;
}

/**
* Delete
*
* @return null|int
* @throws Exception\RuntimeException
*/
public function delete()
{
if (!$this->getId()) {
throw new Exception\RuntimeException('An ID must be set prior to calling delete()');
}

if (!$this->activeRecordCriteriaPrototype) {
throw new Exception\RuntimeException('An Active Record Criteria Prototype must be set prior to calling delete()');
}

if (!$this->getModel()) {
throw new Exception\RuntimeException('A Model must be set prior to calling delete()');
}

$criteria = clone $this->activeRecordCriteriaPrototype;
$criteria->setId($this->getId());
$result = $this->getModel()->delete($criteria);
return $result;
}

/**
* Get
*
* @param $name
* @throws Exception\InvalidArgumentException
* @return void
*/
public function __get($name)
{
throw new Exception\InvalidArgumentException('Not a valid field in this object: ' . $name);
}

/**
* Set
*
* @param string $name
* @param mixed $value
* @throws Exception\InvalidArgumentException
* @return void
*/
public function __set($name, $value)
{
throw new Exception\InvalidArgumentException('Not a valid field in this object: ' . $name);
}

/**
* Unset
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return void
*/
public function __unset($name)
{
throw new Exception\InvalidArgumentException('Not a valid field in this object: ' . $name);
}
}
34 changes: 33 additions & 1 deletion library/Matryoshka/Model/Object/ObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,49 @@
*/
namespace Matryoshka\Model\Object;

use Matryoshka\Model\Exception;
use Matryoshka\Model\Object\Service\ObjectAbstractServiceFactory;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\AbstractPluginManager;

/**
* Class ObjectManager
*/
class ObjectManager extends ServiceManager
class ObjectManager extends AbstractPluginManager
{
/**
* Share by default
* @var bool
*/
protected $shareByDefault = false;

/**
* Constructor
* Add a default initializer to ensure the plugin is valid after instance
* creation.
* @param null|ConfigInterface $configuration
*/
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);
$this->addAbstractFactory(new ObjectAbstractServiceFactory());
}

/**
* Validate the plugin
* Checks that the object loaded is an object.
* @param mixed $plugin
* @throws Exception\InvalidPluginException
*/
public function validatePlugin($plugin)
{
if (!is_object($plugin)) {
throw new Exception\InvalidPluginException(sprintf(
'Type %s is invalid; must implement be an object',
gettype($plugin)
));
}
}

}

0 comments on commit 21ac331

Please sign in to comment.