Skip to content

Commit

Permalink
New interface Normalizable
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 28, 2021
1 parent 95eb39c commit 3598c8c
Show file tree
Hide file tree
Showing 43 changed files with 823 additions and 165 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ parameters:
count: 1
path: src/Tag/TimestampTag.php
-
message: '#Method CBOR\\OtherObject\\NullObject\:\:getNormalizedData\(\) should return mixed but return statement is missing\.#'
message: '#Method CBOR\\OtherObject\\NullObject\:\:normalize\(\) should return mixed but return statement is missing\.#'
count: 1
path: src/OtherObject/NullObject.php
11 changes: 8 additions & 3 deletions src/ByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace CBOR;

final class ByteStringObject extends AbstractCBORObject
final class ByteStringObject extends AbstractCBORObject implements Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING;

Expand Down Expand Up @@ -62,11 +62,16 @@ public function getLength(): int
return mb_strlen($this->value, '8bit');
}

public function normalize(): string
{
return $this->value;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
return $this->value;
return $this->normalize();
}
}
2 changes: 1 addition & 1 deletion src/CBORObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getMajorType(): int;
public function getAdditionalInformation(): int;

/**
* @deprecated The method will be removed on v3.0. No replacement
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*
* @return mixed|null
*/
Expand Down
17 changes: 11 additions & 6 deletions src/IndefiniteLengthByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @final
*/
class IndefiniteLengthByteStringObject extends AbstractCBORObject
class IndefiniteLengthByteStringObject extends AbstractCBORObject implements Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
Expand Down Expand Up @@ -81,16 +81,21 @@ public function getLength(): int
return $length;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): string
public function normalize(): string
{
$result = '';
foreach ($this->chunks as $chunk) {
$result .= $chunk->getNormalizedData($ignoreTags);
$result .= $chunk->normalize();
}

return $result;
}

/**
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
return $this->normalize();
}
}
18 changes: 13 additions & 5 deletions src/IndefiniteLengthListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @phpstan-implements IteratorAggregate<int, CBORObject>
* @final
*/
class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate
class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
Expand Down Expand Up @@ -55,15 +55,23 @@ public function __toString(): string
}

/**
* @deprecated The method will be removed on v3.0. No replacement
* @return mixed[]
*/
public function normalize(): array
{
return array_map(static function (CBORObject $object) {
return $object instanceof Normalizable ? $object->normalize() : $object;
}, $this->data);
}

/**
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*
* @return mixed[]
*/
public function getNormalizedData(bool $ignoreTags = false): array
{
return array_map(static function (CBORObject $item) use ($ignoreTags) {
return $item->getNormalizedData($ignoreTags);
}, $this->data);
return $this->normalize();
}

public function add(CBORObject $item): self
Expand Down
24 changes: 19 additions & 5 deletions src/IndefiniteLengthMapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
use ArrayIterator;
use function count;
use Countable;
use InvalidArgumentException;
use Iterator;
use IteratorAggregate;

/**
* @phpstan-implements IteratorAggregate<int, MapItem>
* @final
*/
class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate
class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
Expand Down Expand Up @@ -79,17 +80,30 @@ public function getIterator(): Iterator
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*
* @return mixed[]
*/
public function getNormalizedData(bool $ignoreTags = false): array
public function normalize(): array
{
$result = [];
foreach ($this->data as $object) {
$result[$object->getKey()->getNormalizedData($ignoreTags)] = $object->getValue()->getNormalizedData($ignoreTags);
$keyObject = $object->getKey();
if (!$keyObject instanceof Normalizable) {
throw new InvalidArgumentException('Invalid key. Shall be normalizable');
}
$valueObject = $object->getValue();
$result[$keyObject->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $object;
}

return $result;
}

/**
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*
* @return mixed[]
*/
public function getNormalizedData(bool $ignoreTags = false): array
{
return $this->normalize();
}
}
17 changes: 11 additions & 6 deletions src/IndefiniteLengthTextStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @final
*/
class IndefiniteLengthTextStringObject extends AbstractCBORObject
class IndefiniteLengthTextStringObject extends AbstractCBORObject implements Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_TEXT_STRING;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
Expand Down Expand Up @@ -81,16 +81,21 @@ public function getLength(): int
return $length;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): string
public function normalize(): string
{
$result = '';
foreach ($this->data as $object) {
$result .= $object->getNormalizedData($ignoreTags);
$result .= $object->normalize();
}

return $result;
}

/**
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
return $this->normalize();
}
}
15 changes: 10 additions & 5 deletions src/ListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* @phpstan-implements IteratorAggregate<int, CBORObject>
*/
class ListObject extends AbstractCBORObject implements Countable, IteratorAggregate
class ListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;

Expand Down Expand Up @@ -93,16 +93,21 @@ public function get(int $index): CBORObject
return $this->data[$index];
}

public function normalize(): array
{
return array_map(static function (CBORObject $object) {
return $object instanceof Normalizable ? $object->normalize() : $object;
}, $this->data);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*
* @return array<int|string, mixed>
*/
public function getNormalizedData(bool $ignoreTags = false): array
{
return array_map(static function (CBORObject $item) use ($ignoreTags) {
return $item->getNormalizedData($ignoreTags);
}, $this->data);
return $this->normalize();
}

public function count(): int
Expand Down
26 changes: 18 additions & 8 deletions src/MapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* @phpstan-implements IteratorAggregate<int, MapItem>
*/
final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate
final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;

Expand Down Expand Up @@ -97,18 +97,28 @@ public function getIterator(): Iterator
return new ArrayIterator($this->data);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*
* @return array<int|string, mixed>
*/
public function getNormalizedData(bool $ignoreTags = false): array
public function normalize(): array
{
$result = [];
foreach ($this->data as $object) {
$result[$object->getKey()->getNormalizedData($ignoreTags)] = $object->getValue()->getNormalizedData($ignoreTags);
$keyObject = $object->getKey();
if (!$keyObject instanceof Normalizable) {
throw new InvalidArgumentException('Invalid key. Shall be normalizable');
}
$valueObject = $object->getValue();
$result[$keyObject->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $object;
}

return $result;
}

/**
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*
* @return array<int|string, mixed>
*/
public function getNormalizedData(bool $ignoreTags = false): array
{
return $this->normalize();
}
}
11 changes: 8 additions & 3 deletions src/NegativeIntegerObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @final
*/
class NegativeIntegerObject extends AbstractCBORObject
class NegativeIntegerObject extends AbstractCBORObject implements Normalizable
{
private const MAJOR_TYPE = self::MAJOR_TYPE_NEGATIVE_INTEGER;

Expand Down Expand Up @@ -73,12 +73,17 @@ public function getValue(): string
return $minusOne->minus($result)->toBase(10);
}

public function normalize(): string
{
return $this->getValue();
}

/**
* @deprecated The method will be removed on v3.0. No replacement
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
return $this->getValue();
return $this->normalize();
}

private static function createBigInteger(BigInteger $integer): self
Expand Down
22 changes: 22 additions & 0 deletions src/Normalizable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* The MIT License (MIT)
*
* Copyright (c) 2018-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace CBOR;

interface Normalizable
{
/**
* @return mixed|null
*/
public function normalize();
}
2 changes: 1 addition & 1 deletion src/OtherObject/BreakObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string
}

/**
* @deprecated The method will be removed on v3.0. No replacement
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*/
public function getNormalizedData(bool $ignoreTags = false): bool
{
Expand Down
27 changes: 18 additions & 9 deletions src/OtherObject/DoublePrecisionFloatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
namespace CBOR\OtherObject;

use Brick\Math\BigInteger;
use CBOR\Normalizable;
use CBOR\OtherObject as Base;
use CBOR\Utils;
use InvalidArgumentException;

final class DoublePrecisionFloatObject extends Base
final class DoublePrecisionFloatObject extends Base implements Normalizable
{
public static function supportedAdditionalInformation(): array
{
Expand All @@ -40,20 +41,28 @@ public static function create(string $value): self
}

/**
* @deprecated The method will be removed on v3.0. No replacement
* @deprecated The method will be removed on v3.0. Please use CBOR\Normalizable interface
*/
public function getNormalizedData(bool $ignoreTags = false)
{
$exp = $this->getExponent();
$mant = $this->getMantissa();
return $this->normalize();
}

/**
* @return float|int
*/
public function normalize()
{
$exponent = $this->getExponent();
$mantissa = $this->getMantissa();
$sign = $this->getSign();

if (0 === $exp) {
$val = $mant * 2 ** (-(1022 + 52));
} elseif (0b11111111111 !== $exp) {
$val = ($mant + (1 << 52)) * 2 ** ($exp - (1023 + 52));
if (0 === $exponent) {
$val = $mantissa * 2 ** (-(1022 + 52));
} elseif (0b11111111111 !== $exponent) {
$val = ($mantissa + (1 << 52)) * 2 ** ($exponent - (1023 + 52));
} else {
$val = 0 === $mant ? INF : NAN;
$val = 0 === $mantissa ? INF : NAN;
}

return $sign * $val;
Expand Down
Loading

0 comments on commit 3598c8c

Please sign in to comment.