Skip to content

Commit

Permalink
Database : JsonType
Browse files Browse the repository at this point in the history
  • Loading branch information
Trehinos committed Jan 30, 2023
1 parent d666754 commit ccb7f88
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion thor/Database/PdoExtension/PdoRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ final public function getPdoHandler(): PdoHandler
return $this->handler;
}

static function in(array $elements) : string
public static function in(array $elements) : string
{
return '(' . implode(',', array_fill(0, count($elements), '?')) . ')';
}
Expand Down
47 changes: 47 additions & 0 deletions thor/Database/PdoTable/TableType/JsonType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Thor\Database\PdoTable\TableType;

/**
*
*/

/**
*
*/
class JsonType extends BaseType
{

/**
* @param int $sqlStringSize
* @param string $sqlStringType
* @param bool $associative
*/
public function __construct(
int $sqlStringSize = 16384,
string $sqlStringType = 'VARCHAR',
private readonly bool $associative = true,
) {
parent::__construct("$sqlStringType($sqlStringSize)", 'int');
}

/**
* @param mixed $sqlValue
*
* @return mixed
*/
public function toPhpValue(mixed $sqlValue): array
{
return json_decode($sqlValue, $this->associative);
}

/**
* @param mixed $phpValue
*
* @return string
*/
public function toSqlValue(mixed $phpValue): string
{
return json_encode($phpValue);
}
}

0 comments on commit ccb7f88

Please sign in to comment.