AbstractEntity.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Entity;
  3. abstract class AbstractEntity {
  4. private $name;
  5. private $slug;
  6. private $data = [];
  7. protected function setFromData(array $data) {
  8. foreach ($data as $key => $value) {
  9. $prop = ltrim($key, '_');
  10. $method = sprintf("set%s", ucfirst(
  11. $prop
  12. ));
  13. if (method_exists($this, $method)) {
  14. [$this, $method]($value);
  15. } elseif (property_exists($this, $prop)) {
  16. $this->{$prop} = $value;
  17. } else {
  18. if (property_exists($this, 'data')) {
  19. if (isset($this->data[$key]))
  20. continue;
  21. if (strpos($key, '-') !== false)
  22. continue;
  23. $this->data[$key] = $value;
  24. }
  25. }
  26. }
  27. }
  28. public function getName(): ?string {
  29. return $this->name;
  30. }
  31. public function setName(string $name): self {
  32. $this->name = $name;
  33. return $this;
  34. }
  35. public function getSlug(): ?string {
  36. return $this->slug;
  37. }
  38. public function setSlug(string $slug): self {
  39. $this->slug = $slug;
  40. return $this;
  41. }
  42. public function getData(): ?array {
  43. return $this->data;
  44. }
  45. }