-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9736dee
commit fc58f17
Showing
11 changed files
with
409 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composite\Entity\Columns; | ||
|
||
use Composite\Entity\AbstractEntity; | ||
use Composite\Entity\Exceptions\EntityException; | ||
|
||
class CollectionColumn extends AbstractColumn | ||
{ | ||
/** | ||
* @throws EntityException | ||
*/ | ||
public function cast(mixed $dbValue): \ArrayAccess | ||
{ | ||
if (is_string($dbValue)) { | ||
try { | ||
$dbValue = (array)\json_decode($dbValue, true, 512, JSON_THROW_ON_ERROR); | ||
} catch (\JsonException $e) { | ||
throw new EntityException($e->getMessage(), $e); | ||
} | ||
} elseif (!is_array($dbValue)) { | ||
throw new EntityException("Cannot to cast value for column {$this->name}, it must be string or array."); | ||
} | ||
/** @var \ArrayAccess $collection */ | ||
$collection = new $this->type; | ||
foreach ($dbValue as $data) { | ||
if ($data === null) { | ||
continue; | ||
} | ||
$item = $this->subType->cast($data); | ||
$collection[] = $item; | ||
} | ||
return $collection; | ||
} | ||
|
||
/** | ||
* @param \Iterator|\IteratorAggregate $entityValue | ||
* @throws EntityException | ||
*/ | ||
public function uncast(mixed $entityValue): string | ||
{ | ||
$list = []; | ||
foreach ($entityValue as $item) { | ||
if ($item instanceof AbstractEntity) { | ||
$list[] = $item->toArray(); | ||
} else { | ||
$list[] = $this->subType->uncast($item); | ||
} | ||
} | ||
try { | ||
return \json_encode($list, JSON_THROW_ON_ERROR); | ||
} catch (\JsonException $e) { | ||
throw new EntityException($e->getMessage(), $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.