From 7869457e26572929d7fd2516d467c39530e9d5f8 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Fri, 24 May 2024 18:03:12 +0200 Subject: [PATCH] Moved `Location::$path` magic getter logic to `Location::getPath()` --- .../Repository/Values/Content/Location.php | 37 +++++++++++++++++-- .../Repository/Values/Content/Location.php | 19 ++-------- .../Repository/Values/Content/TrashItem.php | 19 ++-------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/contracts/Repository/Values/Content/Location.php b/src/contracts/Repository/Values/Content/Location.php index 49474c1c82..972302e695 100644 --- a/src/contracts/Repository/Values/Content/Location.php +++ b/src/contracts/Repository/Values/Content/Location.php @@ -158,6 +158,13 @@ abstract class Location extends ValueObject */ protected $pathString; + /** + * Same as {@see Location::$pathString} but as array, e.g.: [ '1', '2', '4', '23' ]. + * + * @var string[] + */ + protected array $path; + /** * Depth location has in the location tree. * @@ -260,13 +267,19 @@ public function getPathString(): string } /** - * Same as {@see Location::getPathString()} but as array, e.g. [ 1, 2, 4, 23 ]. + * Same as {@see Location::getPathString()} but as array, e.g.: [ '1', '2', '4', '23' ]. * - * @return int[] + * @return string[] */ public function getPath(): array { - return $this->path; + if (isset($this->path)) { + return $this->path; + } + + $pathString = trim($this->pathString ?? '', '/'); + + return $this->path = !empty($pathString) ? explode('/', $pathString) : []; } /** @@ -290,6 +303,24 @@ public function getDepth(): int { return $this->depth; } + + public function __isset($property) + { + if ($property === 'path') { + return true; + } + + return parent::__isset($property); + } + + public function __get($property) + { + if ($property === 'path') { + return $this->getPath(); + } + + return parent::__get($property); + } } class_alias(Location::class, 'eZ\Publish\API\Repository\Values\Content\Location'); diff --git a/src/lib/Repository/Values/Content/Location.php b/src/lib/Repository/Values/Content/Location.php index 8da1969c17..4b5caf6b1f 100644 --- a/src/lib/Repository/Values/Content/Location.php +++ b/src/lib/Repository/Values/Content/Location.php @@ -25,9 +25,6 @@ class Location extends APILocation */ protected $contentInfo; - /** @var array */ - protected $path; - /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location|null */ protected $parentLocation; @@ -71,18 +68,8 @@ protected function getProperties($dynamicProperties = ['contentId']) */ public function __get($property) { - switch ($property) { - case 'contentId': - return $this->getContentInfo()->getId(); - case 'path': - if ($this->path !== null) { - return $this->path; - } - if (isset($this->pathString[1]) && $this->pathString[0] === '/') { - return $this->path = explode('/', trim($this->pathString, '/')); - } - - return $this->path = []; + if ($property === 'contentId') { + return $this->getContentInfo()->getId(); } return parent::__get($property); @@ -97,7 +84,7 @@ public function __get($property) */ public function __isset($property) { - if ($property === 'contentId' || $property === 'path') { + if ($property === 'contentId') { return true; } diff --git a/src/lib/Repository/Values/Content/TrashItem.php b/src/lib/Repository/Values/Content/TrashItem.php index e2753954c2..0c0bc1e30e 100644 --- a/src/lib/Repository/Values/Content/TrashItem.php +++ b/src/lib/Repository/Values/Content/TrashItem.php @@ -26,9 +26,6 @@ class TrashItem extends APITrashItem */ protected $contentInfo; - /** @var array */ - protected $path; - /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location */ protected $parentLocation; @@ -83,18 +80,8 @@ protected function getProperties($dynamicProperties = ['contentId', 'path']) */ public function __get($property) { - switch ($property) { - case 'contentId': - return $this->contentInfo->id; - case 'path': - if ($this->path !== null) { - return $this->path; - } - if (isset($this->pathString[1]) && $this->pathString[0] === '/') { - return $this->path = explode('/', trim($this->pathString, '/')); - } - - return $this->path = []; + if ($property === 'contentId') { + return $this->contentInfo->id; } return parent::__get($property); @@ -109,7 +96,7 @@ public function __get($property) */ public function __isset($property) { - if ($property === 'contentId' || $property === 'path') { + if ($property === 'contentId') { return true; }