1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-17 21:18:23 +01:00

3 separate decoding result objects

This commit is contained in:
Filip Halaxa 2021-12-23 17:11:12 +01:00
parent 8cdfe92ae7
commit 9e27e03f4d
8 changed files with 101 additions and 101 deletions

View File

@ -6,9 +6,18 @@ namespace JsonMachine\JsonDecoder;
interface ChunkDecoder
{
public function decodeInternalKey($jsonScalarKey): ChunkDecodingStringResult;
/**
* @return InvalidResult|ValidStringResult
*/
public function decodeInternalKey($jsonScalarKey);
public function decodeKey($jsonScalarKey): ChunkDecodingResult;
/**
* @return InvalidResult|ValidResult
*/
public function decodeKey($jsonScalarKey);
public function decodeValue($jsonValue): ChunkDecodingResult;
/**
* @return InvalidResult|ValidResult
*/
public function decodeValue($jsonValue);
}

View File

@ -1,49 +0,0 @@
<?php
namespace JsonMachine\JsonDecoder;
class ChunkDecodingResult
{
/**
* @var bool
*/
private $isOk;
/**
* @var mixed
*/
private $value;
/**
* @var string|null
*/
private $errorMessage;
public function __construct(bool $isOk, $value, string $errorMessage = null)
{
$this->isOk = $isOk;
$this->value = $value;
$this->errorMessage = $errorMessage;
}
public function isOk(): bool
{
return $this->isOk;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* @return string|null
*/
public function getErrorMessage()
{
return $this->errorMessage;
}
}

View File

@ -1,46 +0,0 @@
<?php
namespace JsonMachine\JsonDecoder;
class ChunkDecodingStringResult
{
/**
* @var bool
*/
private $isOk;
/**
* @var string
*/
private $value;
/**
* @var string|null
*/
private $errorMessage;
public function __construct(bool $isOk, string $value, string $errorMessage = null)
{
$this->isOk = $isOk;
$this->value = $value;
$this->errorMessage = $errorMessage;
}
public function isOk(): bool
{
return $this->isOk;
}
public function getValue(): string
{
return $this->value;
}
/**
* @return string|null
*/
public function getErrorMessage()
{
return $this->errorMessage;
}
}

View File

@ -3,9 +3,11 @@
namespace JsonMachine\JsonDecoder;
@trigger_error(sprintf(
'Class %s is deprecated. Use %s instead.',
'Class %s is deprecated. Use one of %s, %s or %s instead.',
DecodingResult::class,
ChunkDecodingResult::class
ValidResult::class,
ValidStringResult::class,
InvalidResult::class
), E_USER_DEPRECATED);
/**
@ -31,6 +33,7 @@ class DecodingResult
}
/**
* @deprecated
* @return bool
*/
public function isOk()
@ -39,6 +42,7 @@ class DecodingResult
}
/**
* @deprecated
* @return mixed
*/
public function getValue()
@ -47,6 +51,7 @@ class DecodingResult
}
/**
* @deprecated
* @return string|null
*/
public function getErrorMessage()

View File

@ -0,0 +1,26 @@
<?php
namespace JsonMachine\JsonDecoder;
class InvalidResult
{
/**
* @var string
*/
private $errorMessage;
public function __construct(string $errorMessage)
{
$this->errorMessage = $errorMessage;
}
public function getErrorMessage(): string
{
return $this->errorMessage;
}
public function isOk(): bool
{
return false;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace JsonMachine\JsonDecoder;
class ValidResult
{
/**
* @var mixed
*/
private $value;
public function __construct($value)
{
$this->value = $value;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
public function isOk(): bool
{
return true;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace JsonMachine\JsonDecoder;
class ValidStringResult
{
/**
* @var mixed
*/
private $value;
public function __construct(string $value)
{
$this->value = $value;
}
public function getValue(): string
{
return $this->value;
}
public function isOk(): bool
{
return true;
}
}

View File

@ -155,7 +155,7 @@ class Parser implements \IteratorAggregate, PositionAware
$key = $token;
if ($this->jsonDecoder instanceof ChunkDecoder) {
$keyResult = $this->jsonDecoder->decodeInternalKey($token);
} else {
} elseif ($this->jsonDecoder instanceof Decoder) {
$keyResult = $this->jsonDecoder->decodeKey($token);
}
if (! $keyResult->isOk()) {