Skip to content

Commit

Permalink
Update Entity and mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
lbacik committed Apr 18, 2024
1 parent b781314 commit 2c8b516
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/SDK/Client/Mapper/EntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public function mapArray(array $data): object
return new Entity(
$data['id'] ?? null,
$data['@id'] ?? null,
$data['slug'] ?? '',
$data['slug'] ?? null,
$data['data'],
$data['definition'] ?? null,
$data['parent'] ?? null,
!empty($data['parent'])
? $data['parent']['@id'] ?? sprintf('/api/entities/%s', $data['parent']['id'])
: null,
$data['private'] ?? false,
$data['owned'] ?? false,
);
Expand Down
32 changes: 28 additions & 4 deletions src/SDK/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,39 @@

readonly class Entity
{
private const FIELD_TO_IRI = [
'parent' => '/api/entities/%s',
'definition' => '/api/definitions/%s',
];


public function __construct(
public string|null $id,
public string|null $iri,
public string $slug,
public array $data,
public string|null $slug,
public array|null $data,
public string|null $definition,
public string|null $parent = null,
public bool $private = false,
public bool $owned = false,
public bool|null $private = null,
public bool|null $owned = null,
) {
}

public function toArray(): array
{
return array_reduce(
array_keys(get_object_vars($this)),
function ($carry, $key) {
if ($this->{$key} !== null) {
if (array_key_exists($key, self::FIELD_TO_IRI)) {
$carry[$key] = sprintf(self::FIELD_TO_IRI[$key], $this->{$key});
} else {
$carry[$key] = $this->{$key};
}
}
return $carry;
},
[]
);
}
}

0 comments on commit 2c8b516

Please sign in to comment.