Skip to content

Commit

Permalink
Moved Location::$path magic getter logic to Location::getPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed May 24, 2024
1 parent 6f2708b commit 7869457
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
37 changes: 34 additions & 3 deletions src/contracts/Repository/Values/Content/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ abstract class Location extends ValueObject
*/
protected $pathString;

/**
* Same as {@see Location::$pathString} but as array, e.g.: <code>[ '1', '2', '4', '23' ]</code>.
*
* @var string[]
*/
protected array $path;

/**
* Depth location has in the location tree.
*
Expand Down Expand Up @@ -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.: <code>[ '1', '2', '4', '23' ]</code>.
*
* @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) : [];
}

/**
Expand All @@ -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');
19 changes: 3 additions & 16 deletions src/lib/Repository/Values/Content/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -97,7 +84,7 @@ public function __get($property)
*/
public function __isset($property)
{
if ($property === 'contentId' || $property === 'path') {
if ($property === 'contentId') {
return true;
}

Expand Down
19 changes: 3 additions & 16 deletions src/lib/Repository/Values/Content/TrashItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class TrashItem extends APITrashItem
*/
protected $contentInfo;

/** @var array */
protected $path;

/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location */
protected $parentLocation;

Expand Down Expand Up @@ -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);
Expand All @@ -109,7 +96,7 @@ public function __get($property)
*/
public function __isset($property)
{
if ($property === 'contentId' || $property === 'path') {
if ($property === 'contentId') {
return true;
}

Expand Down

0 comments on commit 7869457

Please sign in to comment.