Skip to content

Commit

Permalink
improvements and conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Aug 31, 2015
1 parent 91b6a08 commit cb54aaa
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 302 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

PHP library to provide CRUD functions (Create, Read, Update, Delete) in Mysql/Sqlite databases with zero configuration and some magic.

## Conventions:

This library relies in some conventions for simplify the configuration.

* Table names must be in [singular](http://stackoverflow.com/a/5841297) and **camelCase**
* Fields names must be in **singular** and **camelCase**
* The primary key of all tables must be `id`.
* Foreign keys must be `[tableName]_id`. For example, `post` table uses `post_id` as foreign key.
* Associative tables must use a underscore joining the two tables in alphabetic order. For example, the relationship between `post` and `tag` is `post_tag` but `post` and `category` is `category_post`.

## Components

Expand Down
83 changes: 56 additions & 27 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,75 @@ class Entity implements ArrayAccess
protected $queryFactory;

public $name;
public $table;
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)
public function __construct($name, SimpleCrud $db, QueryFactory $queryFactory = null, FieldFactory $fieldFactory = null)
{
$this->db = $db;
$this->name = strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($name)));
$this->table = strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($this->name)));
$this->foreignKey = "{$this->table}_id";

$this->setQueryFactory(new QueryFactory());
$this->setFieldFactory(new FieldFactory());
$this->name = $name;
$this->foreignKey = "{$this->name}_id";

$this->row = new Row($this);
$this->collection = new RowCollection($this);
}
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 (empty($queryFactory)) {
$queryFactory = new QueryFactory();
}

protected function setQueryFactory(QueryFactory $queryFactory)
{
$queryFactory->setEntity($this);
$this->queryFactory = $queryFactory;

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

protected function setFieldFactory(FieldFactory $fieldFactory)
/**
* Retrieves the field type used
*
* @param string $name
* @param string $default
*
* @throws SimpleCrudException
*
* @return QueryInterface|null
*/
protected function getFieldType($name, $default)
{
$fieldFactory->setEntity($this);
$this->fields = [];

$fields = $this->db->getFields($this->table);
foreach ($this->smartFields as $type => $names) {
if (in_array($name, $names, true)) {
return $type;
}
}

foreach ($fields as $field => $type) {
$this->fields[$field] = $fieldFactory->get($type);
if (substr($name, -3) === '_id') {
return 'Integer';
}

return $default;
}

/**
Expand All @@ -84,7 +113,7 @@ public function __call($name, $arguments)
*/
public function offsetExists($offset)
{
return $this->db->count($this->name)
return $this->count()
->byId($offset)
->limit(1)
->get() === 1;
Expand All @@ -99,7 +128,7 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
return $this->db->select($this->name)
return $this->select()
->byId($offset)
->one();
}
Expand All @@ -112,14 +141,14 @@ public function offsetGet($offset)
public function offsetSet($offset, $value)
{
if (!empty($offset) && $this->offsetExists($offset)) {
$this->db->update($this->name)
$this->update()
->data($value)
->byId($offset)
->limit(1)
->run();
} else {
$value['id'] = $offset;
$this->db->insert($this->name)
$this->insert()
->data($value)
->run();
}
Expand All @@ -132,7 +161,7 @@ public function offsetSet($offset, $value)
*/
public function offsetUnset($offset)
{
$this->db->delete($this->name)
$this->delete()
->byId($offset)
->limit(1)
->run();
Expand Down Expand Up @@ -320,9 +349,9 @@ public function getRelation($entity)
public function getBridge(Entity $entity)
{
if ($this->name < $entity->name) {
$name = "{$this->name}__{$entity->name}";
$name = "{$this->name}_{$entity->name}";
} else {
$name = "{$entity->name}__{$this->name}";
$name = "{$entity->name}_{$this->name}";
}

if ($this->db->has($name)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/ContainerException.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace SimpleCrud;
namespace SimpleCrud\Exceptions;

use Interop\Container\Exception\ContainerException as ContainerExceptionInterface;

class NotFoundException extends SimpleCrudException implements ContainerExceptionInterface
class ContainerException extends SimpleCrudException implements ContainerExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace SimpleCrud;
namespace SimpleCrud\Exceptions;

use Interop\Container\Exception\NotFoundException as NotFoundExceptionInterface;

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/SimpleCrudException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace SimpleCrud;
namespace SimpleCrud\Exceptions;

class SimpleCrudException extends \Exception
{
Expand Down
143 changes: 0 additions & 143 deletions src/Factory.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Queries/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function get()
*/
public function __toString()
{
$query = "SELECT COUNT(*) FROM `{$this->entity->table}`";
$query = "SELECT COUNT(*) FROM `{$this->entity->name}`";

$query .= $this->whereToString();
$query .= $this->limitToString();
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function run()
*/
public function __toString()
{
$query = "DELETE FROM `{$this->entity->table}`";
$query = "DELETE FROM `{$this->entity->name}`";

$query .= $this->whereToString();
$query .= $this->limitToString();
Expand Down
4 changes: 2 additions & 2 deletions src/Queries/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public function get()
public function __toString()
{
if (empty($this->data)) {
return "INSERT INTO `{$this->entity->table}` (`id`) VALUES (NULL)";
return "INSERT INTO `{$this->entity->name}` (`id`) VALUES (NULL)";
}

$fields = array_keys($this->data);

$query = "INSERT INTO `{$this->entity->table}`";
$query = "INSERT INTO `{$this->entity->name}`";
$query .= ' (`'.implode('`, `', $fields).'`)';
$query .= ' VALUES (:'.implode(', :', $fields).')';

Expand Down
Loading

0 comments on commit cb54aaa

Please sign in to comment.