Skip to content

Commit

Permalink
advances
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Sep 1, 2015
1 parent cb54aaa commit 63311f7
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 441 deletions.
634 changes: 331 additions & 303 deletions README.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"issues": "https://github.com/oscarotero/simple-crud/issues"
},
"require": {
"php": ">=5.4",
"container-interop/container-interop": "^1.1"
"php": ">=5.4"
},
"autoload": {
"psr-4": {
Expand Down
73 changes: 33 additions & 40 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,72 +21,45 @@ class Entity implements ArrayAccess
public $fields = [];
public $foreignKey;

protected $smartFields = [
'Boolean' => ['active'],
'Datetime' => ['pubdate', 'createdAt', 'updatedAt'],
'Integer' => ['id'],
];

/**
* Constructor
*
* @param string $name
* @param SimpleCrud $db
*/
public function __construct($name, SimpleCrud $db, QueryFactory $queryFactory = null, FieldFactory $fieldFactory = null)
public function __construct($name, SimpleCrud $db, QueryFactory $queryFactory, FieldFactory $fieldFactory)
{
$this->db = $db;
$this->name = $name;
$this->foreignKey = "{$this->name}_id";

if (empty($fieldFactory)) {
$fieldFactory = new FieldFactory();
}

$fieldFactory->setEntity($this);

if (empty($this->fields)) {
$this->fields = $this->db->getFields($this->name);
}

foreach ($this->fields as $name => $type) {
$this->fields[$name] = $fieldFactory->get($this->getFieldType($name, $type));
}
if (is_int($name)) {
$name = $type;
$type = null;
}

if (empty($queryFactory)) {
$queryFactory = new QueryFactory();
$this->fields[$name] = $fieldFactory->get($name, $type);
}

$queryFactory->setEntity($this);
$this->queryFactory = $queryFactory;
$this->queryFactory = $queryFactory->setEntity($this);

$this->row = new Row($this);
$this->collection = new RowCollection($this);
$this->setRow(new Row($this));
$this->setCollection(new RowCollection($this));

$this->init();
}

/**
* Retrieves the field type used
*
* @param string $name
* @param string $default
*
* @throws SimpleCrudException
*
* @return QueryInterface|null
* Callback used to init the entity
*/
protected function getFieldType($name, $default)
protected function init()
{
foreach ($this->smartFields as $type => $names) {
if (in_array($name, $names, true)) {
return $type;
}
}

if (substr($name, -3) === '_id') {
return 'Integer';
}

return $default;
}

/**
Expand Down Expand Up @@ -189,6 +162,26 @@ public function getAttribute($name)
return $this->db->getAttribute($name);
}

/**
* Defines the Row class used by this entity
*
* @param Row $row
*/
public function setRow(Row $row)
{
$this->row = $row;
}

/**
* Defines the RowCollection class used by this entity
*
* @param RowCollection $collection
*/
public function setCollection(RowCollection $collection)
{
$this->collection = $collection;
}

/**
* Creates a new row instance.
*
Expand Down
82 changes: 61 additions & 21 deletions src/EntityFactory.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
<?php
namespace SimpleCrud;

use Interop\Container\ContainerInterface;
use SimpleCrud\Exceptions\ContainerException;
use SimpleCrud\Exceptions\NotFoundException;
use Exception;

/**
* Class to create instances of entities.
*/
class EntityFactory implements ContainerInterface
class EntityFactory implements EntityFactoryInterface
{
protected $db;
protected $tables;
protected $namespace;
protected $defaultEntity;
protected $queryFactory;
protected $fieldFactory;

/**
* Constructor.
*
* @param QueryFactory|null $queryFactory
* @param FieldFactory|null $fieldFactory
*/
public function __construct(QueryFactory $queryFactory = null, FieldFactory $fieldFactory = null)
{
$this->setQueryFactory($queryFactory ?: new QueryFactory());
$this->setFieldFactory($fieldFactory ?: new FieldFactory());
}

/**
* @see EntityFactoryInterface
*
* {@inheritdoc}
*/
public function setDb(SimpleCrud $db)
{
$this->db = $db;

return $this;
}

/**
Expand All @@ -35,6 +51,34 @@ public function setNamespace($namespace)
return $this;
}

/**
* Set the queryFactory instance used by the entities
*
* @param QueryFactory $queryFactory
*
* @return self
*/
public function setQueryFactory(QueryFactory $queryFactory)
{
$this->queryFactory = $queryFactory;

return $this;
}

/**
* Set the fieldFactory instance used by the entities
*
* @param FieldFactory $fieldFactory
*
* @return self
*/
public function setFieldFactory(FieldFactory $fieldFactory)
{
$this->fieldFactory = $fieldFactory;

return $this;
}

/**
* Set whether the entities are autocreated or not
*
Expand All @@ -50,45 +94,41 @@ public function setAutocreate($defaultEntity = 'SimpleCrud\\Entity')
}

/**
* Check whether or not an Entity is instantiable.
*
* @param string $name
*
* @return boolean
* @see EntityFactoryInterface
*
* {@inheritdoc}
*/
public function has($name)
{
return ($this->defaultEntity && in_array($name, $this->getTables())) || class_exists($this->namespace.ucfirst($name));
}

/**
* Creates a new instance of an Entity.
*
* @param string $name
* @see EntityFactoryInterface
*
* @throws NotFoundException
*
* @return Entity|null
* {@inheritdoc}
*/
public function get($name)
{
try {
$class = $this->namespace.ucfirst($name);
$queryFactory = clone $this->queryFactory;
$fieldFactory = clone $this->fieldFactory;

if (class_exists($class)) {
return new $class($name, $this->db);
return new $class($name, $this->db, $queryFactory, $fieldFactory);
}

if ($this->defaultEntity && in_array($name, $this->getTables())) {
$class = $this->defaultEntity;

return new $class($name, $this->db);
return new $class($name, $this->db, $queryFactory, $fieldFactory);
}
} catch (Exception $exception) {
throw new ContainerException("Error getting the '{$name}' entity", 0, $exception);
throw new SimpleCrudException("Error getting the '{$name}' entity", 0, $exception);
}

throw new NotFoundException("The entity '{$name}' is not found");
throw new SimpleCrudException("Entity '{$name}' not found");
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/EntityFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace SimpleCrud;

/**
* Interface used by the entity factory
*/
interface EntityFactoryInterface
{
/**
* Set the database used for the entities
*
* @param SimpleCrud $db
*
* @return self
*/
public function setDb(SimpleCrud $db);

/**
* Check whether or not an Entity is instantiable.
*
* @param string $name
*
* @return boolean
*/
public function has($name);

/**
* Creates a new instance of an Entity.
*
* @param string $name
*
* @throws SimpleCrudException
*
* @return Entity
*/
public function get($name);
}
8 changes: 0 additions & 8 deletions src/Exceptions/ContainerException.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exceptions/NotFoundException.php

This file was deleted.

Loading

0 comments on commit 63311f7

Please sign in to comment.