2020-08-07 03:04:53 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-08-07 03:04:53 +01:00
|
|
|
// {{{ 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;
|
2021-10-10 09:26:18 +01:00
|
|
|
use BadMethodCallException;
|
2020-08-08 17:10:25 +01:00
|
|
|
use DateTime;
|
2021-10-10 09:26:18 +01:00
|
|
|
use Exception;
|
|
|
|
use InvalidArgumentException;
|
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-24 05:30:54 +01:00
|
|
|
abstract class Entity
|
2020-08-07 03:04:53 +01:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
public function __call(string $name, array $arguments): mixed
|
2021-04-27 21:56:13 +01:00
|
|
|
{
|
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-10-10 09:26:18 +01:00
|
|
|
$private_property_accessor = fn ($prop) => isset($this->{$prop});
|
|
|
|
$private_property_accessor = $private_property_accessor->bindTo($this, \get_called_class());
|
2021-05-02 00:14:24 +01:00
|
|
|
return $private_property_accessor($prop);
|
2021-04-27 21:56:13 +01:00
|
|
|
}
|
2021-10-10 09:26:18 +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-09-06 19:49:03 +01:00
|
|
|
$class = static::class;
|
2021-12-05 03:11:08 +00:00
|
|
|
|
2021-12-16 11:14:34 +00:00
|
|
|
$date = new DateTime();
|
|
|
|
if (!\is_null($obj)) { // Update modified
|
2021-12-05 03:11:08 +00:00
|
|
|
if (property_exists($class, 'modified')) {
|
|
|
|
$args['modified'] = $date;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$obj = new $class();
|
|
|
|
foreach (['created', 'modified'] as $prop) {
|
|
|
|
if (property_exists($class, $prop)) {
|
|
|
|
$args[$prop] = $date;
|
|
|
|
}
|
2021-07-21 17:41:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2021-10-10 09:26:18 +01:00
|
|
|
throw new InvalidArgumentException($m);
|
2020-08-07 03:04:53 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-05 03:11:08 +00:00
|
|
|
|
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
|
|
|
*
|
2021-09-06 23:47:28 +01:00
|
|
|
* @return array [$obj, $is_update]
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2021-10-10 09:26:18 +01:00
|
|
|
public static function createOrUpdate(array $args, array $find_by_keys = []): array
|
2020-08-07 03:04:53 +01:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
$table = DB::getTableForClass(\get_called_class());
|
2021-07-21 17:41:21 +01:00
|
|
|
$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-10-10 09:26:18 +01:00
|
|
|
} catch (Exception $e) {
|
2021-08-07 19:19:01 +01:00
|
|
|
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
|
|
|
*
|
2021-08-14 20:41:22 +01:00
|
|
|
* Support multiple formats:
|
|
|
|
* - mixed $values - convert to array and check next
|
|
|
|
* - array[int => mixed] $values - get keys for entity and set them in order and proceed to next case
|
|
|
|
* - array[string => mixed] $values - Perform a regular find
|
|
|
|
*
|
|
|
|
* Examples:
|
2021-12-16 11:14:34 +00:00
|
|
|
* Entity::getByPK(42);
|
|
|
|
* Entity::getByPK([42, 'foo']);
|
|
|
|
* Entity::getByPK(['key1' => 42, 'key2' => 'foo'])
|
2020-11-09 21:25:57 +00:00
|
|
|
*
|
|
|
|
* @return null|static
|
|
|
|
*/
|
2021-12-16 11:14:34 +00:00
|
|
|
public static function getByPK(mixed $values): ?self
|
2020-11-09 21:25:57 +00:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
$values = \is_array($values) ? $values : [$values];
|
|
|
|
$class = \get_called_class();
|
2021-07-21 17:41:21 +01:00
|
|
|
$keys = DB::getPKForClass($class);
|
|
|
|
$find_by = [];
|
|
|
|
foreach ($values as $k => $v) {
|
2021-10-10 09:26:18 +01:00
|
|
|
if (\is_string($k)) {
|
2021-07-21 17:41:21 +01:00
|
|
|
$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-11-27 04:12:44 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-21 16:04:50 +00:00
|
|
|
* Who should be notified about this object?
|
|
|
|
*
|
|
|
|
* @return array of ids of Actors
|
2021-11-27 04:12:44 +00:00
|
|
|
*/
|
2021-12-21 16:04:50 +00:00
|
|
|
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array
|
2021-11-27 04:12:44 +00:00
|
|
|
{
|
2021-12-21 16:04:50 +00:00
|
|
|
// Additional actors that should know about this
|
|
|
|
if (array_key_exists('additional', $ids_already_known)) {
|
|
|
|
return $ids_already_known['additional'];
|
|
|
|
}
|
2021-11-27 04:12:44 +00:00
|
|
|
return [];
|
|
|
|
}
|
2021-12-21 16:04:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Who should be notified about this object?
|
|
|
|
*
|
|
|
|
* @return array of Actors
|
|
|
|
*/
|
|
|
|
public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array
|
|
|
|
{
|
|
|
|
$target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id);
|
|
|
|
return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]);
|
|
|
|
}
|
2020-08-07 03:04:53 +01:00
|
|
|
}
|