-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
228cea4
commit c63cc7c
Showing
5 changed files
with
151 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crell\Serde\Formatter; | ||
|
||
use Crell\Serde\Attributes\ClassSettings; | ||
use Crell\Serde\Attributes\Field; | ||
use Crell\Serde\Deserializer; | ||
use Devium\Toml\Toml; | ||
use Devium\Toml\TomlError; | ||
|
||
class TomlFormatter implements Formatter, Deformatter, SupportsCollecting | ||
{ | ||
use ArrayBasedFormatter; | ||
use ArrayBasedDeformatter; | ||
|
||
/** | ||
* Constructor parameters map directly to the devium/toml component's encode() and decode() methods. | ||
* | ||
* @see Toml::encode() | ||
* @see Toml::decode() | ||
*/ | ||
public function __construct() {} | ||
|
||
public function format(): string | ||
{ | ||
return 'toml'; | ||
} | ||
|
||
/** | ||
* @param ClassSettings $classDef | ||
* @param Field $rootField | ||
* @return array<string, mixed> | ||
*/ | ||
public function serializeInitialize(ClassSettings $classDef, Field $rootField): array | ||
{ | ||
return ['root' => []]; | ||
} | ||
|
||
public function serializeFinalize(mixed $runningValue, ClassSettings $classDef): string | ||
{ | ||
return Toml::encode($runningValue['root']); | ||
} | ||
|
||
/** | ||
* @param mixed $serialized | ||
* @param ClassSettings $classDef | ||
* @param Field $rootField | ||
* @param Deserializer $deserializer | ||
* @return array<string, mixed> | ||
* @throws TomlError | ||
*/ | ||
public function deserializeInitialize( | ||
mixed $serialized, | ||
ClassSettings $classDef, | ||
Field $rootField, | ||
Deserializer $deserializer | ||
): array | ||
{ | ||
return ['root' => Toml::decode($serialized ?: '', true)]; | ||
} | ||
|
||
public function deserializeFinalize(mixed $decoded): void | ||
{ | ||
|
||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crell\Serde; | ||
|
||
use Devium\Toml\Toml; | ||
use Crell\Serde\Formatter\TomlFormatter; | ||
|
||
class TomlFormatterTest extends ArrayBasedFormatterTestCases | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->formatters = [new TomlFormatter()]; | ||
$this->format = 'toml'; | ||
$this->emptyData = ''; | ||
|
||
$this->aliasedData = Toml::encode([ | ||
'un' => 1, | ||
'dos' => 'dos', | ||
'dot' => [ | ||
'x' => 1, | ||
'y' => 2, | ||
'z' => 3, | ||
] | ||
]); | ||
|
||
$this->invalidDictStringKey = Toml::encode([ | ||
'stringKey' => ['a' => 'A', 2 => 'B'], | ||
// The 'd' key here is invalid and won't deserialize. | ||
'intKey' => [5 => 'C', 'd' => 'D'], | ||
]); | ||
|
||
$this->invalidDictIntKey = Toml::encode([ | ||
// The 2 key here is invalid and won't deserialize. | ||
'stringKey' => ['a' => 'A', 2 => 'B'], | ||
'intKey' => [5 => 'C', 10 => 'D'], | ||
]); | ||
|
||
$this->missingOptionalData = Toml::encode(['a' => 'A']); | ||
|
||
$this->dictsInSequenceShouldFail = Toml::encode([ | ||
'strict' => ['a' => 'A', 'b' => 'B'], | ||
'nonstrict' => ['a' => 'A', 'b' => 'B'], | ||
]); | ||
|
||
$this->dictsInSequenceShouldPass = Toml::encode([ | ||
'strict' => ['A', 'B'], | ||
'nonstrict' => ['a' => 'A', 'b' => 'B'], | ||
]); | ||
} | ||
|
||
protected function arrayify(mixed $serialized): array | ||
{ | ||
return Toml::decode($serialized, true); | ||
} | ||
|
||
public static function non_strict_properties_examples(): iterable | ||
{ | ||
foreach (self::non_strict_properties_examples_data() as $k => $v) { | ||
$v['serialized'] = Toml::encode($v['serialized']); | ||
yield $k => $v; | ||
} | ||
} | ||
|
||
public static function strict_mode_throws_examples(): iterable | ||
{ | ||
foreach (self::strict_mode_throws_examples_data() as $k => $v) { | ||
$v['serialized'] = Toml::encode($v['serialized']); | ||
yield $k => $v; | ||
} | ||
} | ||
} |