-
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
Showing
2 changed files
with
48 additions
and
1 deletion.
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
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); | ||
} | ||
} |