Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Models\DataEntities;
- abstract class BaseEntity implements EntityInterface
- {
- /**
- * @var \ReflectionClass
- */
- protected static $reflection;
- /**
- * @var array
- */
- protected static $propertiesNames;
- public function toArray(): array
- {
- static::makeReflectionObject();
- static::makePropertiesNames();
- $result = [];
- foreach (static::$propertiesNames as $propertyName) {
- $result[$propertyName] = $this->{$propertyName};
- }
- return $result;
- }
- private static function makeReflectionObject(): void
- {
- if (static::$reflection !== null) {
- return;
- }
- try {
- static::$reflection = new \ReflectionClass(static::class);
- } catch (\ReflectionException $e) {
- throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
- }
- }
- private static function makePropertiesNames(): void
- {
- if (static::$propertiesNames !== null) {
- return;
- }
- $properties = static::$reflection->getProperties();
- foreach ($properties as $property) {
- if ($property->isProtected() && !$property->isStatic()) {
- static::$propertiesNames[] = $property->getName();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement