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;
|
2021-08-08 01:39:39 +01:00
|
|
|
use App\Util\Exception\ServerException;
|
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
|
|
|
|
*/
|
2021-08-08 01:39:39 +01:00
|
|
|
abstract class Entity implements \JsonSerializable
|
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) {
|
2021-08-11 23:46:02 +01:00
|
|
|
if (property_exists($obj, $prop)) {
|
2021-08-07 19:19:01 +01:00
|
|
|
$set = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($prop));
|
2020-08-08 17:10:25 +01:00
|
|
|
$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-08-07 19:19:01 +01:00
|
|
|
*
|
|
|
|
* @return [$obj, $is_update]
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
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));
|
2021-08-07 19:19:01 +01:00
|
|
|
try {
|
|
|
|
$obj = DB::findOneBy($table, $find_by);
|
|
|
|
} catch (NotFoundException) {
|
|
|
|
$obj = null;
|
2021-08-08 01:39:39 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-08-07 19:19:01 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::unexpected_exception($e);
|
2021-08-08 01:39:39 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-08-07 19:19:01 +01:00
|
|
|
}
|
|
|
|
$is_update = $obj !== null;
|
|
|
|
return [self::create($args, $obj), $is_update];
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-08-08 01:39:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when json_encode encounters this object. Not all
|
|
|
|
* entities will need json encoding, so it doesn't make sense to
|
|
|
|
* make this abstract
|
|
|
|
*
|
|
|
|
* @throw ServerException
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function jsonSerialize()
|
|
|
|
{
|
|
|
|
throw new ServerException(_m('Unimplemented method'));
|
|
|
|
}
|
2020-08-07 03:04:53 +01:00
|
|
|
}
|