-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
3,249 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.