2020-08-07 11:04:53 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace App\Core;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
2020-11-10 06:25:57 +09:00
|
|
|
use App\Util\Exception\NotFoundException;
|
2020-08-07 11:04:53 +09:00
|
|
|
use App\Util\Formatting;
|
2020-08-09 01:10:25 +09:00
|
|
|
use DateTime;
|
2020-08-07 11:04:53 +09:00
|
|
|
|
2020-11-07 04:47:15 +09:00
|
|
|
/**
|
|
|
|
* Base class to all entities, with some utilities
|
|
|
|
*/
|
|
|
|
abstract class Entity
|
2020-08-07 11:04:53 +09:00
|
|
|
{
|
2021-04-28 05:56:13 +09:00
|
|
|
public function __call(string $name , array $arguments): mixed
|
|
|
|
{
|
2021-04-29 05:15:43 +09:00
|
|
|
if (Formatting::startsWith($name, 'has')) {
|
|
|
|
$prop = Formatting::camelCaseToSnakeCase(Formatting::removePrefix($name, 'has'));
|
2021-05-02 08:14:24 +09:00
|
|
|
// https://wiki.php.net/rfc/closure_apply#proposal
|
2021-05-07 06:54:10 +09:00
|
|
|
$private_property_accessor = function ($prop) { return isset($this->{$prop}); };
|
2021-05-02 08:14:24 +09:00
|
|
|
$private_property_accessor = $private_property_accessor->bindTo($this, get_called_class());
|
|
|
|
return $private_property_accessor($prop);
|
2021-04-28 05:56:13 +09:00
|
|
|
}
|
2021-05-13 00:44:09 +09:00
|
|
|
throw new \BadMethodCallException('Non existent method ' . get_called_class() . "::{$name} called with arguments: " . print_r($arguments, true));
|
2021-04-28 05:56:13 +09:00
|
|
|
}
|
|
|
|
|
2020-11-07 04:47:15 +09:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-08-07 11:04:53 +09:00
|
|
|
public static function create(array $args, $obj = null)
|
|
|
|
{
|
2021-07-22 01:41:21 +09:00
|
|
|
$class = get_called_class();
|
|
|
|
$obj = $obj ?: new $class();
|
|
|
|
$date = new DateTime();
|
|
|
|
foreach (['created', 'modified'] as $prop) {
|
|
|
|
if (property_exists($class, $prop)) {
|
|
|
|
$args[$prop] = $date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 11:04:53 +09:00
|
|
|
foreach ($args as $prop => $val) {
|
2021-08-04 02:47:25 +09:00
|
|
|
if (property_exists($class, $prop)) {
|
2020-08-09 01:10:25 +09:00
|
|
|
$set = 'set' . Formatting::snakeCaseToCamelCase($prop);
|
|
|
|
$obj->{$set}($val);
|
2020-08-07 11:04:53 +09:00
|
|
|
} else {
|
2021-07-22 01:41:21 +09:00
|
|
|
Log::error($m = "Property {$class}::{$prop} doesn't exist");
|
|
|
|
throw new \InvalidArgumentException($m);
|
2020-08-07 11:04:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2020-11-07 04:47:15 +09:00
|
|
|
/**
|
|
|
|
* Create a new instance, but check for duplicates
|
|
|
|
*/
|
2021-07-22 01:41:21 +09:00
|
|
|
public static function createOrUpdate(array $args, array $find_by_keys = [])
|
2020-08-07 11:04:53 +09:00
|
|
|
{
|
2021-07-22 01:41:21 +09:00
|
|
|
$table = DB::getTableForClass(get_called_class());
|
|
|
|
$find_by = $find_by_keys == [] ? $args : array_intersect_key($args, array_flip($find_by_keys));
|
2020-11-07 04:47:15 +09:00
|
|
|
return self::create($args, DB::findOneBy($table, $find_by));
|
2020-08-07 11:04:53 +09:00
|
|
|
}
|
2020-09-04 23:15:58 +09:00
|
|
|
|
2020-11-07 04:47:15 +09:00
|
|
|
/**
|
2021-07-22 01:41:21 +09:00
|
|
|
* Get an Entity from its primary key
|
2020-11-10 06:25:57 +09:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return null|static
|
|
|
|
*/
|
2021-07-22 01:41:21 +09:00
|
|
|
public static function getWithPK(mixed $values): ?self
|
2020-11-10 06:25:57 +09:00
|
|
|
{
|
2021-07-22 01:41:21 +09:00
|
|
|
$values = is_array($values) ? $values : [$values];
|
|
|
|
$class = get_called_class();
|
|
|
|
$keys = DB::getPKForClass($class);
|
|
|
|
$find_by = [];
|
|
|
|
foreach ($values as $k => $v) {
|
|
|
|
if (is_string($k)) {
|
|
|
|
$find_by[$k] = $v;
|
|
|
|
} else {
|
|
|
|
$find_by[$keys[$k]] = $v;
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 06:25:57 +09:00
|
|
|
try {
|
2021-07-22 01:41:21 +09:00
|
|
|
return DB::findOneBy($class, $find_by);
|
2020-11-10 06:25:57 +09:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 11:04:53 +09:00
|
|
|
}
|