2020-08-07 03:04:53 +01: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-09 21:25:57 +00:00
|
|
|
use App\Util\Exception\NotFoundException;
|
2020-08-07 03:04:53 +01:00
|
|
|
use App\Util\Formatting;
|
2020-08-08 17:10:25 +01:00
|
|
|
use DateTime;
|
2020-08-07 03:04:53 +01:00
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Base class to all entities, with some utilities
|
|
|
|
*/
|
|
|
|
abstract class Entity
|
2020-08-07 03:04:53 +01:00
|
|
|
{
|
2021-04-27 21:56:13 +01:00
|
|
|
public function __call(string $name , array $arguments): mixed
|
|
|
|
{
|
2021-04-28 21:15:43 +01:00
|
|
|
if (Formatting::startsWith($name, 'has')) {
|
|
|
|
$prop = Formatting::camelCaseToSnakeCase(Formatting::removePrefix($name, 'has'));
|
2021-05-02 00:14:24 +01:00
|
|
|
// https://wiki.php.net/rfc/closure_apply#proposal
|
2021-05-06 22:54:10 +01:00
|
|
|
$private_property_accessor = function ($prop) { return isset($this->{$prop}); };
|
2021-05-02 00:14:24 +01:00
|
|
|
$private_property_accessor = $private_property_accessor->bindTo($this, get_called_class());
|
|
|
|
return $private_property_accessor($prop);
|
2021-04-27 21:56:13 +01:00
|
|
|
}
|
2021-05-12 16:44:09 +01:00
|
|
|
throw new \BadMethodCallException('Non existent method ' . get_called_class() . "::{$name} called with arguments: " . print_r($arguments, true));
|
2021-04-27 21:56:13 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00: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 03:04:53 +01:00
|
|
|
public static function create(array $args, $obj = null)
|
|
|
|
{
|
2021-07-21 17:41:21 +01: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 03:04:53 +01:00
|
|
|
foreach ($args as $prop => $val) {
|
2020-08-28 21:16:26 +01:00
|
|
|
if (property_exists($class, $prop) && $val != null) {
|
2020-08-08 17:10:25 +01:00
|
|
|
$set = 'set' . Formatting::snakeCaseToCamelCase($prop);
|
|
|
|
$obj->{$set}($val);
|
2020-08-07 03:04:53 +01:00
|
|
|
} else {
|
2021-07-21 17:41:21 +01:00
|
|
|
Log::error($m = "Property {$class}::{$prop} doesn't exist");
|
|
|
|
throw new \InvalidArgumentException($m);
|
2020-08-07 03:04:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Create a new instance, but check for duplicates
|
|
|
|
*/
|
2021-07-21 17:41:21 +01:00
|
|
|
public static function createOrUpdate(array $args, array $find_by_keys = [])
|
2020-08-07 03:04:53 +01:00
|
|
|
{
|
2021-07-21 17:41:21 +01:00
|
|
|
$table = DB::getTableForClass(get_called_class());
|
|
|
|
$find_by = $find_by_keys == [] ? $args : array_intersect_key($args, array_flip($find_by_keys));
|
2020-11-06 19:47:15 +00:00
|
|
|
return self::create($args, DB::findOneBy($table, $find_by));
|
2020-08-07 03:04:53 +01:00
|
|
|
}
|
2020-09-04 15:15:58 +01:00
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
2021-07-21 17:41:21 +01:00
|
|
|
* Get an Entity from its primary key
|
2020-11-09 21:25:57 +00:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return null|static
|
|
|
|
*/
|
2021-07-21 17:41:21 +01:00
|
|
|
public static function getWithPK(mixed $values): ?self
|
2020-11-09 21:25:57 +00:00
|
|
|
{
|
2021-07-21 17:41:21 +01: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-09 21:25:57 +00:00
|
|
|
try {
|
2021-07-21 17:41:21 +01:00
|
|
|
return DB::findOneBy($class, $find_by);
|
2020-11-09 21:25:57 +00:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 03:04:53 +01:00
|
|
|
}
|