. // }}} namespace App\Core; use App\Core\DB\DB; use App\Util\Formatting; use DateTime; /** * Base class to all entities, with some utilities */ abstract class Entity { /** * Create an instance of the called class or fill in the * properties of $obj with the associative array $args. Doesn't * persist the result * * @param null|mixed $obj */ public static function create(array $args, $obj = null) { $class = get_called_class(); $obj = $obj ?: new $class(); $args['created'] = $args['modified'] = new DateTime(); foreach ($args as $prop => $val) { if (property_exists($class, $prop) && $val != null) { $set = 'set' . Formatting::snakeCaseToCamelCase($prop); $obj->{$set}($val); } else { Log::error("Property {$class}::{$prop} doesn't exist"); } } return $obj; } /** * Create a new instance, but check for duplicates */ public static function createOrUpdate(array $args, array $find_by) { $table = Formatting::camelCaseToSnakeCase(get_called_class()); return self::create($args, DB::findOneBy($table, $find_by)); } /** * Remove a given $obj or whatever is found by `DB::findBy(..., $args)` * from the database. Doesn't flush * * @param null|mixed $obj */ public static function remove(array $args, $obj = null) { $class = '\\' . get_called_class(); if ($obj == null) { $obj = DB::findBy($class, $args); } DB::remove($obj); } }