Skip to content

Commit

Permalink
orm
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Jul 19, 2017
1 parent 572d452 commit c376efe
Show file tree
Hide file tree
Showing 25 changed files with 3,249 additions and 57 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"intervention/image": "^2.3",
"intervention/imagecache": "^2.3",
"psy/psysh": "@stable",
"predis/predis": "1.1.*@dev"
"predis/predis": "1.1.*@dev",
"robmorgan/phinx": "@stable"
},
"require-dev": {
"phpunit/phpunit": "@stable"
Expand Down
124 changes: 124 additions & 0 deletions lib/checking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
namespace Octo;

class Checking
{
protected $data = [];
protected $rules = [];
protected $errors = [];

public function __construct($data = null)
{
$this->data = empty($data) ? $_POST : $data;
}

public function success()
{
return empty($this->errors);
}

public function fail()
{
return !empty($this->errors);
}

public function add($field)
{
$rule = new Fluent;
$this->rules[$field] = $rule;

return $rule;
}

public function validate()
{
foreach ($this->rules as $field => $rule) {
$checks = $rule->getAttributes();

foreach ($checks as $check => $args) {
$method = lcfirst(Strings::camelize('is_' . $check));
$this->$method($field, $args);
}
}
}

private function isCustom($field, $callable)
{
$value = isAke($this->data, $field, null);

$check = call_user_func_array($callable, [$field, $value]);

if (!$check) {
$this->addError($field, "$field does not match with custom rule.");
}
}

private function isMinLength($field, $length)
{
$value = isAke($this->data, $field, null);
$check = mb_strlen($value) >= $length;

if (!$check) {
$this->addError($field, "$field is too short.");
}
}

private function isMaxLength($field, $length)
{
$value = isAke($this->data, $field, null);
$check = mb_strlen($value) <= $length;

if (!$check) {
$this->addError($field, "$field is too long.");
}
}

private function isInteger($field)
{
$value = isAke($this->data, $field, null);
$check = reallyInt($value);

if (!$check) {
$this->addError($field, "$field is not an integer.");
}
}

private function isInt($field)
{
$value = isAke($this->data, $field, null);
$check = reallyInt($value);

if (!$check) {
$this->addError($field, "$field is not an integer.");
}
}

private function isRequired($field)
{
$check = isset($this->data[$field]) && strlen($this->data[$field]);

if (!$check) {
$this->addError($field, "$field is required but empty.");
}
}

private function isEmail($field)
{
$value = isAke($this->data, $field, null);

$check = filter_var($value, FILTER_VALIDATE_EMAIL) !== false;

if (!$check) {
$this->addError($field, "$field is not an email.");
}
}

protected function addError($field, $message)
{
if (!isset($this->errors[$field])) {
$this->errors[$field] = [];
}

$this->errors[$field][] = $message;
}
}
214 changes: 202 additions & 12 deletions lib/entity.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,217 @@
<?php
namespace Octo;

use PDOException;

class Entity
{
public static function __callStatic($method, $args)
protected $table;
protected $guarded = [];
protected $fillable = [];
protected $hidden = [];
protected $timestamps = true;
protected $softDelete = false;
protected $primaryKey = 'id';

protected static $booted = [];

public function __construct()
{
$class = get_called_class();

$methods = get_class_methods($this);

if (!isset(static::$booted[$class])) {
static::$booted[$class] = true;

if (in_array('events', $methods)) {
static::events($this);
}

if (in_array('policies', $methods)) {
static::policies($this);
}

if (in_array('boot', $methods)) {
static::boot($this);
}

$this->fire('booting');

$traits = class_uses($class);

if (!empty($traits)) {
foreach ($traits as $trait) {
$tab = explode('\\', $trait);
$traitName = Strings::lower(end($tab));
$method = lcfirst(Strings::camelize('boot_' . $traitName . '_trait'));

if (in_array($method, $methods)) {
call_user_func_array([$this, $method], []);
}
}
}

$this->fire('booted');
}
}

public function fire($event, $concern = null, $return = false)
{
$methods = get_class_methods($this);
$method = 'on' . Strings::camelize($event);

if (in_array($method, $methods)) {
$result = $this->$method($concern);

if ($return) {
return $result;
}
}

return $concern;
}

public function setTable($table)
{
$this->table = $table;

return $this;
}

public static function table()
{
$db = Inflector::uncamelize($method);
return static::called()->table;
}

public static function pk()
{
return static::called()->primaryKey;
}

public static function called()
{
$class = get_called_class();
$i = maker($class);

if (fnmatch('*_*', $db)) {
list($database, $table) = explode('_', $db, 2);
if (!isset($i->table)) {
$table = $i->table = Strings::lower(
Arrays::last(
explode(
'\\',
$class
)
)
);
} else {
$database = SITE_NAME;
$table = $db;
$table = $i->table;
}

if (empty($args)) {
return db($database, $table);
} elseif (count($args) == 1) {
$id = array_shift($args);
return actual("orm.entity.$table", $i);
}

if (is_numeric($id)) {
return db($database, $table)->find($id);
public static function model(array $data = [])
{
return new Record($data, static::called());
}

protected static function db()
{
return foundry(Orm::class)->table(static::called()->table);
}

public static function new(array $data)
{
return static::create($data);
}

public static function create(array $data)
{
unset($data[static::pk()]);

try {
$new = static::db()
->insert($data)
->run();
} catch (PDOException $e) {
$row = $e->errorInfo[2];
$fields = array_keys($data);

foreach ($fields as $field) {
if (fnmatch("* $field*", $row)) {
unset($data[$field]);
}
}

return static::create($data);
}

return static::find(static::lastId());
}

public function pivot($record, $entityClass)
{
return $this->pivots($record, $entityClass, false);
}

public function pivots($record, $entityClass, $many = true)
{
$otherEntity = maker($entityClass);

$tables = [$this->table(), $otherEntity->table()];

sort($tables);

$pivot = implode('', $tables);

$pivotEntity = (new Entity)->setTable($pivot);

$getter = getter($this->pk());

$query = $pivotEntity
->where(
$this->table() . '_id',
$record->$getter()
);

if ($many) {
return $query->get();
}

return $query->first();
}

public static function __callStatic($m, $a)
{
$instance = static::db();

if ('new' == $m) {
return static::create(current($a));
}

$result = call_user_func_array([$instance, $m], $a);

if ($m != 'lastId' && (startsWith($m, 'find') || startsWith($m, 'first') || fnmatch('last*', $m))) {
return $result ? static::model($result) : null;
}

return $result;
}

public function __call($m, $a)
{
$instance = static::db();

if ('new' == $m) {
return static::create(current($a));
}

$result = call_user_func_array([$instance, $m], $a);

if ($m != 'lastId' && (startsWith($m, 'find') || startsWith($m, 'first') || fnmatch('last*', $m))) {
return $result ? static::model($result) : null;
}

return $result;
}
}
10 changes: 6 additions & 4 deletions lib/fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,22 @@ public function offsetUnset($offset)
*/
public function __call($method, $parameters)
{
$method = Strings::uncamelize($method);

if ($callable = isAke(self::$macros, $method)) {
return call_user_func_array($callable, $parameters);
}

if (!empty($parameters)) {
$callable = current($parameters);

if (is_callable($callable)) {
if (is_callable($callable) && $method != 'custom') {
self::$macros[$method] = $callable;
} else {
$this->attributes[$method] = $callable;
}
} else {
$parameters[] = true;

$this->attributes[$method] = current($parameters);
$this->attributes[$method] = true;
}

return $this;
Expand Down
Loading

0 comments on commit c376efe

Please sign in to comment.