Skip to content

Commit

Permalink
introduce gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Bunge committed Apr 13, 2016
1 parent 67eb8ac commit ab00c8b
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @package Blast\Orm
*/
class Connection extends DbalConnection implements MapperFactoryInterface
class Connection extends DbalConnection implements MapperFactoryInterface, QueryFactoryInterface
{

use MapperFactoryTrait {
Expand Down Expand Up @@ -55,9 +55,9 @@ public function createMapper($entity){
*
* @param \Doctrine\DBAL\Query\QueryBuilder $builder
*
* @return Mapper
* @return \Blast\Orm\Query
*/
public function createQuery($entity, QueryBuilder $builder = null)
public function createQuery($entity = null, QueryBuilder $builder = null)
{
$query = new Query($this, $entity);
$query->setBuilder(null === $builder ? parent::createQueryBuilder() : $builder);
Expand Down
6 changes: 3 additions & 3 deletions src/ConnectionAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ trait ConnectionAwareTrait
{

/**
* @var \Doctrine\DBAL\Connection
* @var \Doctrine\DBAL\Connection|\Blast\Orm\Connection
*/
private $connection = null;

/**
* Get current connection
*
* @return \Doctrine\DBAL\Driver\Connection|\Doctrine\DBAL\Connection
* @return \Doctrine\DBAL\Driver\Connection|\Doctrine\DBAL\Connection|\Blast\Orm\Connection
*/
public function getConnection()
{
Expand All @@ -35,7 +35,7 @@ public function getConnection()
}

/**
* @param \Doctrine\DBAL\Driver\Connection|null $connection
* @param \Doctrine\DBAL\Driver\Connection|\Blast\Orm\Connection|null $connection
*
* @return $this
*/
Expand Down
98 changes: 98 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
*
* (c) Marco Bunge <marco_bunge@web.de>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*
* Date: 13.04.2016
* Time: 17:23
*
*/

namespace Blast\Orm;


use Blast\Orm\Relations\RelationInterface;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\Type;

class Gateway implements GatewayInterface, ConnectionAwareInterface
{

use ConnectionAwareTrait;

/**
* @var string
*/
private $table;

/**
* Create a new gateway for a single table
*
* @param $table
*/
public function __construct($table)
{
$this->table = $table;
}

/**
* Prepare insert statement
*
* @param $data
*
* @param Column[] $fields
* @return $this
*/
public function insert($data, $fields = [])
{
//cancel if $data has no entries
if (count($data) < 1) {
return false;
}

//prepare statement
$query = $this->getConnection()->createQuery();
$query->insert($this->table);

foreach ($data as $key => $value) {
if ($value instanceof RelationInterface) {
continue;
};

$query->setValue($key, $query->createPositionalParameter(
$value, array_key_exists($key, $fields) ?
$fields[$key]->getType()->getName() :
Type::STRING));
}

return $query;
}

/**
* Prepare update statement
*
* @param $primaryKey
* @param $data
*
* @return mixed
*/
public function update($primaryKey, $data)
{
// TODO: Implement update() method.
}

/**
* Prepare delete statement
*
* @param $primaryKey
*
* @return mixed
*/
public function delete($primaryKey)
{
// TODO: Implement delete() method.
}
}
59 changes: 59 additions & 0 deletions src/GatewayInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
*
* (c) Marco Bunge <marco_bunge@web.de>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*
* Date: 13.04.2016
* Time: 17:08
*
*/

namespace Blast\Orm;

/**
* The gateway is accessing a single table or view and interacts with database
*
* @package Blast\Orm
*/
interface GatewayInterface
{

/**
* Create a new gateway for a single table
*
* @param $table
*/
public function __construct($table);

/**
* Prepare insert statement
*
* @param $data
*
* @return $this
*/
public function insert($data);

/**
* Prepare update statement
*
* @param $primaryKey
* @param $data
*
* @return mixed
*/
public function update($primaryKey, $data);

/**
* Prepare delete statement
*
* @param $primaryKey
*
* @return mixed
*/
public function delete($primaryKey);

}
35 changes: 8 additions & 27 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,33 +148,14 @@ public function create($entity)

//disallow differing entities
$this->checkEntity($provider);

//prepare statement
$query = $this->createQuery();
$query->insert($provider->getDefinition()->getTableName());


//pass data without relations
$data = $provider->extract();

//cancel if $data has no entries
if (count($data) < 1) {
return false;
}

$fields = $provider->getDefinition()->getFields();

foreach ($data as $key => $value) {
if ($value instanceof RelationInterface) {
continue;
};

$query->setValue($key, $query->createPositionalParameter(
$value, array_key_exists($key, $fields) ?
$fields[$key]->getType()->getName() :
Type::STRING));
}

return $query;
$gateway = new Gateway($provider->getDefinition()->getTableName());

return $gateway->insert($data, $fields);
}

/**
Expand All @@ -192,15 +173,15 @@ public function update($entity)
$this->checkEntity($provider);

$pkName = $provider->getDefinition()->getPrimaryKeyName();
//pass data without relations
$data = $provider->extract();
$fields = $provider->getDefinition()->getFields();


//prepare statement
$query = $this->createQuery();
$query->update($provider->getDefinition()->getTableName());

//pass data without relations
$data = $provider->extract();

$fields = $provider->getDefinition()->getFields();

foreach ($data as $key => $value) {
if ($value instanceof RelationInterface) {
Expand Down
2 changes: 1 addition & 1 deletion src/QueryFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ interface QueryFactoryInterface
*
* @return Mapper
*/
public function createQuery($entity);
public function createQuery($entity = null);
}

0 comments on commit ab00c8b

Please sign in to comment.