Skip to content

Commit

Permalink
native readonly propertes (#208)
Browse files Browse the repository at this point in the history

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Feb 6, 2023
1 parent 6cf4955 commit f582678
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 46 deletions.
10 changes: 2 additions & 8 deletions src/Core/Serialization/DOM/NormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ class NormalizerFactory
{
public const FORMAT = Format::XML;

/**
* @readonly
*/
private Spec $spec;
private readonly Spec $spec;

/**
* @readonly
*/
private DOMDocument $document;
private readonly DOMDocument $document;

/**
* @throws DomainException when the spec does not support XML format
Expand Down
5 changes: 1 addition & 4 deletions src/Core/Serialization/DOM/_BaseNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
*/
abstract class _BaseNormalizer
{
/**
* @readonly
*/
private NormalizerFactory $normalizerFactory;
private readonly NormalizerFactory $normalizerFactory;

public function __construct(NormalizerFactory $normalizerFactory)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Core/Serialization/JSON/NormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ class NormalizerFactory
{
public const FORMAT = Format::JSON;

/**
* @readonly
*/
private Spec $spec;
private readonly Spec $spec;

/**
* @throws DomainException when the spec does not support JSON format
Expand Down
5 changes: 1 addition & 4 deletions src/Core/Serialization/JSON/_BaseNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
*/
abstract class _BaseNormalizer
{
/**
* @readonly
*/
private NormalizerFactory $normalizerFactory;
private readonly NormalizerFactory $normalizerFactory;

public function __construct(NormalizerFactory $normalizerFactory)
{
Expand Down
33 changes: 21 additions & 12 deletions src/Core/Serialization/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
class JsonSerializer extends BaseSerializer
{
/**
* List of allowed options for $jsonEncodeFlags.
* Some flags will break the output...
* List of allowed options for {@see jsonEncodeFlags}.
*
* Bitmask consisting of JSON_*.
*
* Some JSON flags could break the output, so they are not whitelisted.
*
* @see https://www.php.net/manual/en/json.constants.php
*/
private const JsonEncodeFlagsAllowedOptions = 0
Expand All @@ -56,6 +57,20 @@ class JsonSerializer extends BaseSerializer
| \JSON_UNESCAPED_UNICODE
;

/**
* Defaults of {@see $jsonEncodeFlags}.
*
* Bitmask consisting of JSON_*.
*
* These defaults are required to have valid output in the end.
*
* @see https://www.php.net/manual/en/json.constants.php
*/
private const JsonEncodeFlagsDefaults = 0
| \JSON_THROW_ON_ERROR // prevent unexpected data
| \JSON_PRESERVE_ZERO_FRACTION // float/double not converted to int
;

/**
* List of mandatory options for $jsonEncodeFlags.
*
Expand All @@ -67,23 +82,16 @@ class JsonSerializer extends BaseSerializer
| \JSON_UNESCAPED_SLASHES // urls become shorter
;

/** @readonly */
private JSON\NormalizerFactory $normalizerFactory;
private readonly JSON\NormalizerFactory $normalizerFactory;

/**
* Flags for {@see \json_encode()}.
*
* Bitmask consisting of JSON_*.
*
* @see https://www.php.net/manual/en/json.constants.php
*
* @readonly
*/
private int $jsonEncodeFlags = 0
// These defaults are required to have valid output.
| \JSON_THROW_ON_ERROR // prevent unexpected data
| \JSON_PRESERVE_ZERO_FRACTION // float/double not converted to int
;
private readonly int $jsonEncodeFlags;

/**
* @param int $jsonEncodeFlags Bitmask consisting of JSON_*. see {@see JsonEncodeFlagsAllowedOptions}
Expand All @@ -93,7 +101,8 @@ public function __construct(
int $jsonEncodeFlags = self::JsonEncodeFlagsDefaultOptions
) {
$this->normalizerFactory = $normalizerFactory;
$this->jsonEncodeFlags |= $jsonEncodeFlags & self::JsonEncodeFlagsAllowedOptions;
$this->jsonEncodeFlags = self::JsonEncodeFlagsDefaults
| ($jsonEncodeFlags & self::JsonEncodeFlagsAllowedOptions);
}

protected function realNormalize(Bom $bom): array
Expand Down
9 changes: 3 additions & 6 deletions src/Core/Serialization/XmlSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@
*/
class XmlSerializer extends BaseSerializer
{
/** @readonly */
private DOM\NormalizerFactory $normalizerFactory;
private readonly DOM\NormalizerFactory $normalizerFactory;

/** @readonly */
private string $xmlVersion;
/** @readonly */
private string $xmlEncoding;
private readonly string $xmlVersion;
private readonly string $xmlEncoding;

public function __construct(
DOM\NormalizerFactory $normalizerFactory,
Expand Down
5 changes: 1 addition & 4 deletions src/Core/Validation/BaseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
*/
abstract class BaseValidator implements Validator
{
/**
* @readonly
*/
private Spec $spec;
private readonly Spec $spec;

public function __construct(Spec $spec)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Core/Validation/ValidationError.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
*/
class ValidationError
{
/**
* @readonly
*/
private string $message;
private readonly string $message;

/**
* keep for internal debug purposes.
Expand Down

0 comments on commit f582678

Please sign in to comment.