Skip to content

Commit 3e808aa

Browse files
committed
Move "toId" logic to builder
1 parent 04d5b6f commit 3e808aa

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/StaticEntity.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,7 @@ static public function getIds()
107107
*/
108108
static public function toId($idOrEntity)
109109
{
110-
if ($idOrEntity instanceof StaticEntity) {
111-
return $idOrEntity->getId();
112-
} elseif (self::exists($idOrEntity)) {
113-
return $idOrEntity;
114-
}
115-
116-
throw new \Exception(sprintf('%s => Invalid parameter: %s', __METHOD__, $idOrEntity));
110+
return self::getBuilder()->convertToId($idOrEntity);
117111
}
118112

119113

src/StaticEntityBuilder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,21 @@ private function createInstance($id)
160160

161161
$this->instances[$id] = $instance;
162162
}
163+
164+
/**
165+
* @param mixed|StaticEntity $staticEntity
166+
*
167+
* @return mixed
168+
* @throws \Exception
169+
*/
170+
public function convertToId($staticEntity)
171+
{
172+
if ($staticEntity instanceof StaticEntity) {
173+
return $staticEntity->getId();
174+
} elseif (!$this->hasId($staticEntity)) {
175+
throw new \Exception(sprintf('Unable to convert "%s" to a valid id for class %s', $staticEntity, $this->class));
176+
}
177+
178+
return $staticEntity;
179+
}
163180
}

0 commit comments

Comments
 (0)