PHP code example of digital-craftsman / deserializing-connection
1. Go to this page and download the library: Download digital-craftsman/deserializing-connection library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
digital-craftsman / deserializing-connection example snippets
final readonly class User
{
public function __construct(
public UserId $userId,
public string $name,
public ProjectIdList $accessibleProjects,
) {
}
}
$user = $this->deserializingConnection->getOne(
sql: <<<'SQL'
SELECT
user_id AS "userId",
name,
accessible_projects AS "accessibleProjects"
FROM
`user`
WHERE user_id = :userId
SQL,
class: User::class,
parameters: [
'userId' => $userId,
],
decoderTypes: [
'accessibleProjects' => DecoderType::JSON,
],
);
final readonly class User
{
public function __construct(
public UserId $userId,
public string $name,
public string $companyLink,
) {
}
}
$this->deserializingConnection->getOne(
sql: <<<'SQL'
SELECT
user_id AS "userId",
name,
companyLink
FROM
`user`
WHERE user_id = :userId
SQL,
class: ReadModel\User::class,
parameters: [
'userId' => $userId,
],
decoderTypes: [
'company' => DecoderType::JSON,
],
resultTransformers: [
ResultTransformer::toTransformAndRename(
key: 'companyLink',
denormalizeResultToClass: CompanyLink::class,
transformer: fn(CompanyLink $companyLink) => $this->router->generate(
'company_show',
[
'companyId' => $companyLink->companyId,
],
),
isTransformedResultNormalized: false,
renameTo: 'link',
),
],
);