Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 118 additions & 80 deletions src/Rox/Gateway/MongoDb/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,132 +8,165 @@

/**
* Use this class to implement collection especific methods
* @todo Review docs and transactions checking success and thow exceptions in negative cases
*
* TODO Review docs and transactions checking success and thow exceptions in negative cases
* TODO implements a interface for common methods
* @author Marcelo Araújo
*/
class AbstractGateway extends RoxGateway
{
/**
*
* @param mixed $id
* @param string $module
* @param string $collection
* @return array
*/
public function getReference($id, $module, $collection){
$className = "$module\Gateway\MongoDb\\$collection";
$gateway = new $className($this->db);
return $gateway->findById($id);
}
/**
*
* @param array $documents
* @param int $id
* @return array|NULL
*/
public function getSubDocument($documents, $id) {
foreach ($documents as $document) {
if($document['_id'] == $id){
return $document;
}
}
return null;
}

/**
*
*
* @param mixed $id
* @param string $module
* @param string $collection
* @return array
*/
public function getReference($id, $module, $collection)
{
$className = "$module\Gateway\MongoDb\\$collection";
$gateway = new $className($this->db);
return $gateway->findById($id);
}

/**
*
* @param array $documents
* @param int $id
* @return array|NULL
*/
public function getSubDocument($documents, $id)
{
foreach ($documents as $document) {
if ($document['_id'] == $id) {
return $document;
}
}
return null;
}

/**
*
* @param \MongoCursor $cursor
* @return \PhlyMongo\HydratingMongoCursor
*/
public function hydrateCollection(\MongoCursor $cursor){
return new HydratingMongoCursor(
$cursor,
$this->hydrator,
$this->model
);
public function hydrateCollection(\MongoCursor $cursor)
{
return new HydratingMongoCursor($cursor, $this->model->getHydrator(), $this->model);
}

/**
*
* @return \MongoCollection
*/
public function getCollection(){
return $this->db->{$this->name};
public function getCollection()
{
return $this->db->{$this->name};
}

/**
* Find all documents from especific colection
*
* @return \Zend\Paginator\Paginator
*/
public function findAll($criteria = [], $sort = null){
$cursor = $this->db->{$this->name}->find($criteria);
if($sort){
$cursor->sort($sort);
}
$adapter = new MongoPaginatorAdapter(new HydratingMongoCursor(
$cursor,
$this->hydrator,
$this->model
));
return new Paginator($adapter);
public function findAll($criteria = [], $sort = null)
{
$cursor = $this->getCollection()->find($criteria);
if ($sort) {
$cursor->sort($sort);
}
$adapter = new MongoPaginatorAdapter(new HydratingMongoCursor($cursor, $this->model->getHydrator(), $this->model));
return new Paginator($adapter);
}

/**
*
* @param array $criteria
* @return array
*/
public function count($criteria = []){
return $this->db->{$this->name}->count($criteria);
public function count($criteria = [])
{
return $this->getCollection()->count($criteria);
}

/**
*
*
* @param array $criteria
* @return array
*/
public function findCurrent($criteria = []){
return $this->db->{$this->name}->findOne($criteria);
public function findCurrent($criteria = [])
{
return $this->getCollection()->findOne($criteria);
}

/**
*
* @param string $label
* @return array An associative array with [value => label] format
*/
public function getAssocArray($criteria = [], $label = 'name'){
$assoc = [];
$data = $this->db->{$this->name}->find($criteria,['_id', $label])->sort([$label => 1]);
foreach ($data as $record){
$assoc[$record['_id']->{'$id'}] = $record[$label];
}
return $assoc;
public function getAssocArray($criteria = [], $label = 'name')
{
$assoc = [];
$data = $this->getCollection()
->find($criteria, [
'_id',
$label
])
->sort([
$label => 1
]);
foreach ($data as $record) {
$assoc[$record['_id']->{'$id'}] = $record[$label];
}
return $assoc;
}

/**
* Find and return a document by its mongoId
* @param mixed $id of document
*
* @param mixed $id
* of document
* @return array
*/
public function findById($id){
return $this->db->{$this->name}->findOne(['_id' => $this->getMongoId($id)]);
public function findById($id)
{
return $this->getCollection()->findOne([
'_id' => $this->getMongoId($id)
]);
}

/**
* @param array $data with _id
*
* @param array $data
* with _id
* @return mixed
*/
public function update(array $data){
$data = $this->filterData($data);
$id = $this->getMongoId($data['_id']);
unset($data['_id']);
return $this->getCollection()->update(['_id' => $id], ['$set' => $data]);
public function update(array $data)
{
$id = $this->getMongoId($data['_id']);
unset($data['_id']);
return $this->getCollection()->update([
'_id' => $id
], [
'$set' => $data
]);
}

/**
*
* @param array $data
* @return mixed
*/
public function save(array $data){
$data = $this->filterData($data);
if(!$data['_id']){
unset($data['_id']);
} else {
$data['_id'] = $this->getMongoId($data['_id']);
}

$this->db->{$this->name}->save($data);
return $data;
public function save(array $data)
{
if (! $data['_id']) {
unset($data['_id']);
} else {
$data['_id'] = $this->getMongoId($data['_id']);
}
$this->getCollection()->save($data);
return $data;
}

/**
Expand All @@ -153,11 +186,16 @@ public function getMongoId($id, $field = 'unknown')
throw new \Exception("Parâmetro inválido: $field");
}
}

/**
*
* @param mixed $id
* @return mixed
*/
public function delete($id){
return $this->db->{$this->name}->remove(['_id' => $this->getMongoId($id)]);
public function delete($id)
{
return $this->getCollection()->remove([
'_id' => $this->getMongoId($id)
]);
}
}
109 changes: 44 additions & 65 deletions src/Rox/Gateway/RoxGateway.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,51 @@
<?php

namespace Rox\Gateway;

use Doctrine\Common\Inflector\Inflector;
use Rox\Hydrator\MagicMethods;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Rox\Model\AbstractModel;

class RoxGateway {

protected $model;
protected $db;
protected $hydrator;
protected $name;
protected $reflectionClass;
/**
*
* @param mixed $db
* @param Rox\Model\AbstractModel $model
* @param Zend\Stdlib\Hydrator\HydratorInterface $hydrator
*/

public function __construct($db, AbstractModel $model = null, HydratorInterface $hydrator = null)
{
$this->db = $db;
$this->reflectionClass = new \ReflectionClass(get_class($this));
$this->name = $this->reflectionClass->getShortName();
$this->setModel($model);
$this->setHydrator($hydrator);

}
/**
*
* @param array $data
*/
public function filterData(array $data){
$model = $this->hydrator->hydrate($data, $this->model);
return $this->hydrator->extract($model);
}
public function setModel($model){
if($model){
$this->model = $model;
} else {
$modelName = $this->getModelName();
$namespace = $this->reflectionClass->getNamespaceName();
$modelNamespace = substr($namespace, 0, strpos($namespace,"\\"));
$modelClassName = sprintf('\%s\Model\%s',$modelNamespace, $modelName);
$this->model = new $modelClassName;
}
}
public function getModelName() {
$inflector = new Inflector();
$name = $this->reflectionClass->getShortName();
return $inflector->singularize($name);
}
public function setHydrator($hydrator){
if($hydrator){
$this->hydrator = $hydrator;
} else {
$this->hydrator = new MagicMethods();
}
}
/**
* Proxy for Rox\Model\AbstractModel::getInputFilter()
* @return Zend\InputFilter\InputFilter
*/
public function getInputFilter($fields = null)
{
return $this->model->getInputFilter($fields);
}
class RoxGateway
{

protected $model;

protected $db;

protected $name;

protected $reflectionClass;

/**
*
* @param mixed $db
* @param Rox\Model\AbstractModel $model
* @param Zend\Stdlib\Hydrator\HydratorInterface $hydrator
*/
public function __construct($db, AbstractModel $model = null)
{
$this->db = $db;
$this->reflectionClass = new \ReflectionClass(get_class($this));
$this->name = $this->reflectionClass->getShortName();
$this->setModel($model);
}

public function setModel($model)
{
if ($model) {
$this->model = $model;
} else {
$modelName = $this->getModelName();
$namespace = $this->reflectionClass->getNamespaceName();
$modelNamespace = substr($namespace, 0, strpos($namespace, "\\"));
$modelClassName = sprintf('\%s\Model\%s', $modelNamespace, $modelName);
$this->model = new $modelClassName();
}
}

public function getModelName()
{
$inflector = new Inflector();
$name = $this->reflectionClass->getShortName();
return $inflector->singularize($name);
}
}
Loading