Entity.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Core;
  19. use App\Core\DB\DB;
  20. use App\Util\Exception\NotFoundException;
  21. use App\Util\Formatting;
  22. use DateTime;
  23. /**
  24. * Base class to all entities, with some utilities
  25. */
  26. abstract class Entity
  27. {
  28. public function __call(string $name , array $arguments): mixed
  29. {
  30. if (Formatting::startsWith($name, 'has')) {
  31. $prop = Formatting::camelCaseToSnakeCase(Formatting::removePrefix($name, 'has'));
  32. // https://wiki.php.net/rfc/closure_apply#proposal
  33. $private_property_accessor = function ($prop) { return isset($this->{$prop}); };
  34. $private_property_accessor = $private_property_accessor->bindTo($this, get_called_class());
  35. return $private_property_accessor($prop);
  36. }
  37. throw new \BadMethodCallException('Non existent method ' . get_called_class() . "::{$name} called with arguments: " . print_r($arguments, true));
  38. }
  39. /**
  40. * Create an instance of the called class or fill in the
  41. * properties of $obj with the associative array $args. Doesn't
  42. * persist the result
  43. *
  44. * @param null|mixed $obj
  45. */
  46. public static function create(array $args, $obj = null)
  47. {
  48. $class = get_called_class();
  49. $obj = $obj ?: new $class();
  50. $args['created'] = $args['modified'] = new DateTime();
  51. foreach ($args as $prop => $val) {
  52. if (property_exists($class, $prop) && $val != null) {
  53. $set = 'set' . Formatting::snakeCaseToCamelCase($prop);
  54. $obj->{$set}($val);
  55. } else {
  56. Log::error("Property {$class}::{$prop} doesn't exist");
  57. }
  58. }
  59. return $obj;
  60. }
  61. /**
  62. * Create a new instance, but check for duplicates
  63. */
  64. public static function createOrUpdate(array $args, array $find_by)
  65. {
  66. $table = Formatting::camelCaseToSnakeCase(get_called_class());
  67. return self::create($args, DB::findOneBy($table, $find_by));
  68. }
  69. /**
  70. * Remove a given $obj or whatever is found by `DB::findBy(..., $args)`
  71. * from the database. Doesn't flush
  72. *
  73. * @param null|mixed $obj
  74. */
  75. public static function remove(array $args, $obj = null)
  76. {
  77. $class = '\\' . get_called_class();
  78. if ($obj == null) {
  79. $obj = DB::findBy($class, $args);
  80. }
  81. DB::remove($obj);
  82. }
  83. /**
  84. * Get an Entity from its id
  85. *
  86. * @param int $id
  87. *
  88. * @return null|static
  89. */
  90. public static function getFromId(int $id): ?self
  91. {
  92. $array = explode('\\', get_called_class());
  93. $class = end($array);
  94. try {
  95. return DB::findOneBy($class, ['id' => $id]);
  96. } catch (NotFoundException $e) {
  97. return null;
  98. }
  99. }
  100. }