mirror of
https://github.com/halaxa/json-machine.git
synced 2025-03-15 17:09:39 +01:00
Removed JsonMachine class
This commit is contained in:
parent
7857b3229f
commit
3c98e16441
10
CHANGELOG.md
10
CHANGELOG.md
@ -1,15 +1,19 @@
|
||||
# Changelog
|
||||
|
||||
## master
|
||||
Nothing yet
|
||||
### Removed
|
||||
- Removed deprecated functions `objects()` and `httpClientChunks()`.
|
||||
- Removed deprecated `JsonMachine` entrypoint class.
|
||||
<br>
|
||||
<br>
|
||||
|
||||
## 0.8.0
|
||||
### Changed
|
||||
- Internal decoders moved to `ItemDecoder`. `ErrorWrappingDecoder` decorator now requires `ItemDecoder` as well.
|
||||
|
||||
### Deprecated
|
||||
- `JsonMachine\JsonMachine` entry point class is deprecated, use `JsonMachine\Items` instead.
|
||||
- `JsonMachine\JsonDecoder\Decoder` interface is deprecated. Use `JsonMachine\JsonDecoder\ItemDecoder` instead.
|
||||
- Internal decoders moved to `ItemDecoder`. `ErrorWrappingDecoder` decorator now requires `ItemDecoder` as well.
|
||||
- `JsonMachine\JsonDecoder\Decoder` interface is deprecated. Use `JsonMachine\JsonDecoder\ItemDecoder` instead.
|
||||
|
||||
### Added
|
||||
- New entry point class `Items` replaces `JsonMachine`.
|
||||
|
@ -1,113 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace JsonMachine;
|
||||
|
||||
use JsonMachine\JsonDecoder\ItemDecoder;
|
||||
use JsonMachine\JsonDecoder\Decoder;
|
||||
use JsonMachine\JsonDecoder\ExtJsonDecoder;
|
||||
|
||||
/**
|
||||
* @deprecated Use class Items instead
|
||||
*/
|
||||
class JsonMachine implements \IteratorAggregate, PositionAware
|
||||
{
|
||||
/**
|
||||
* @var iterable
|
||||
*/
|
||||
private $bytesIterator;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $jsonPointer;
|
||||
|
||||
/**
|
||||
* @var Decoder|ItemDecoder|null
|
||||
*/
|
||||
private $jsonDecoder;
|
||||
|
||||
/**
|
||||
* @var Parser
|
||||
*/
|
||||
private $parser;
|
||||
|
||||
/**
|
||||
* @param iterable $bytesIterator
|
||||
* @param string $jsonPointer
|
||||
* @param Decoder|ItemDecoder $jsonDecoder
|
||||
* @throws Exception\InvalidArgumentException
|
||||
*/
|
||||
public function __construct($bytesIterator, $jsonPointer = '', $jsonDecoder = null)
|
||||
{
|
||||
@trigger_error("Class JsonMachine is deprecated. Use class Items instead.", E_USER_DEPRECATED);
|
||||
|
||||
$this->bytesIterator = $bytesIterator;
|
||||
$this->jsonPointer = $jsonPointer;
|
||||
$this->jsonDecoder = $jsonDecoder;
|
||||
|
||||
$this->parser = new Parser(
|
||||
new Lexer($this->bytesIterator, true),
|
||||
$this->jsonPointer,
|
||||
$this->jsonDecoder ?: new ExtJsonDecoder(true)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @param string $jsonPointer
|
||||
* @param Decoder|ItemDecoder $jsonDecoder
|
||||
* @return self
|
||||
* @throws Exception\InvalidArgumentException
|
||||
*/
|
||||
public static function fromString($string, $jsonPointer = '', $jsonDecoder = null)
|
||||
{
|
||||
return new static(new StringChunks($string), $jsonPointer, $jsonDecoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @param string $jsonPointer
|
||||
* @param Decoder|ItemDecoder $jsonDecoder
|
||||
* @return self
|
||||
* @throws Exception\InvalidArgumentException
|
||||
*/
|
||||
public static function fromFile($file, $jsonPointer = '', $jsonDecoder = null)
|
||||
{
|
||||
return new static(new FileChunks($file), $jsonPointer, $jsonDecoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $stream
|
||||
* @param string $jsonPointer
|
||||
* @param Decoder|ItemDecoder $jsonDecoder
|
||||
* @return self
|
||||
* @throws Exception\InvalidArgumentException
|
||||
*/
|
||||
public static function fromStream($stream, $jsonPointer = '', $jsonDecoder = null)
|
||||
{
|
||||
return new static(new StreamChunks($stream), $jsonPointer, $jsonDecoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable $iterable
|
||||
* @param string $jsonPointer
|
||||
* @param Decoder|ItemDecoder $jsonDecoder
|
||||
* @return self
|
||||
* @throws Exception\InvalidArgumentException
|
||||
*/
|
||||
public static function fromIterable($iterable, $jsonPointer = '', $jsonDecoder = null)
|
||||
{
|
||||
return new static($iterable, $jsonPointer, $jsonDecoder);
|
||||
}
|
||||
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator()
|
||||
{
|
||||
return $this->parser;
|
||||
}
|
||||
|
||||
public function getPosition()
|
||||
{
|
||||
return $this->parser->getPosition();
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ class FileChunksTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testGeneratorYieldsStringChunks($chunkSize, array $expectedResult)
|
||||
{
|
||||
$fileChunks = new FileChunks(__DIR__ . '/JsonMachineTest.json', $chunkSize);
|
||||
$fileChunks = new FileChunks(__DIR__ . '/ItemsTest.json', $chunkSize);
|
||||
$result = iterator_to_array($fileChunks);
|
||||
|
||||
$this->assertSame($expectedResult, $result);
|
||||
|
@ -41,13 +41,13 @@ class ItemsTest extends \PHPUnit_Framework_TestCase
|
||||
foreach ([
|
||||
[$extJsonResult, 'fromStream', fopen('data://text/plain,{"path": {"key":"value"}}', 'r'), '/path', null, $debug],
|
||||
[$extJsonResult, 'fromString', '{"path": {"key":"value"}}', '/path', null, $debug],
|
||||
[$extJsonResult, 'fromFile', __DIR__ . '/JsonMachineTest.json', '/path', null, $debug],
|
||||
[$extJsonResult, 'fromFile', __DIR__ . '/ItemsTest.json', '/path', null, $debug],
|
||||
[$extJsonResult, 'fromIterable', ['{"path": {"key', '":"value"}}'], '/path', null, $debug],
|
||||
[$extJsonResult, 'fromIterable', new \ArrayIterator(['{"path": {"key', '":"value"}}']), '/path', null, $debug],
|
||||
|
||||
[$passThruResult, 'fromStream', fopen('data://text/plain,{"path": {"key":"value"}}', 'r'), '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromString', '{"path": {"key":"value"}}', '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromFile', __DIR__ . '/JsonMachineTest.json', '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromFile', __DIR__ . '/ItemsTest.json', '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromIterable', ['{"path": {"key', '":"value"}}'], '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromIterable', new \ArrayIterator(['{"path": {"key', '":"value"}}']), '/path', $ptDecoder, $debug],
|
||||
] as $case) {
|
||||
|
@ -1,111 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace JsonMachineTest;
|
||||
|
||||
use JsonMachine\JsonDecoder\ItemDecoder;
|
||||
use JsonMachine\JsonDecoder\Decoder;
|
||||
use JsonMachine\JsonDecoder\DecodingResult;
|
||||
use JsonMachine\JsonDecoder\ExtJsonDecoder;
|
||||
use JsonMachine\JsonDecoder\PassThruDecoder;
|
||||
use JsonMachine\JsonMachine;
|
||||
use JsonMachine\Lexer;
|
||||
use JsonMachine\Parser;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
class JsonMachineTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataFactories
|
||||
*/
|
||||
public function testFactories($expected, $methodName, ...$args)
|
||||
{
|
||||
$iterator = call_user_func_array(JsonMachine::class."::$methodName", $args);
|
||||
$this->assertSame($expected, iterator_to_array($iterator));
|
||||
}
|
||||
|
||||
public function testJsonMachineYieldsArraysByDefault()
|
||||
{
|
||||
$iterator = JsonMachine::fromString('{"path": {"key":"value"}}');
|
||||
foreach ($iterator as $item) {
|
||||
$this->assertEquals(['key' => 'value'], $item);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataFactories()
|
||||
{
|
||||
$extJsonResult = ['key' => 'value'];
|
||||
$passThruResult = ['key' => '"value"'];
|
||||
$ptDecoder = new PassThruDecoder();
|
||||
|
||||
foreach ([true, false] as $debug) {
|
||||
foreach ([
|
||||
[$extJsonResult, 'fromStream', fopen('data://text/plain,{"path": {"key":"value"}}', 'r'), '/path', null, $debug],
|
||||
[$extJsonResult, 'fromString', '{"path": {"key":"value"}}', '/path', null, $debug],
|
||||
[$extJsonResult, 'fromFile', __DIR__ . '/JsonMachineTest.json', '/path', null, $debug],
|
||||
[$extJsonResult, 'fromIterable', ['{"path": {"key', '":"value"}}'], '/path', null, $debug],
|
||||
[$extJsonResult, 'fromIterable', new \ArrayIterator(['{"path": {"key', '":"value"}}']), '/path', null, $debug],
|
||||
|
||||
[$passThruResult, 'fromStream', fopen('data://text/plain,{"path": {"key":"value"}}', 'r'), '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromString', '{"path": {"key":"value"}}', '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromFile', __DIR__ . '/JsonMachineTest.json', '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromIterable', ['{"path": {"key', '":"value"}}'], '/path', $ptDecoder, $debug],
|
||||
[$passThruResult, 'fromIterable', new \ArrayIterator(['{"path": {"key', '":"value"}}']), '/path', $ptDecoder, $debug],
|
||||
] as $case) {
|
||||
yield $case;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function testIterationWithoutForeach()
|
||||
{
|
||||
$iterator =
|
||||
JsonMachine::fromString('{"key1":1, "key2":2}')
|
||||
->getIterator()->getIterator();
|
||||
|
||||
$iterator->rewind();
|
||||
$this->assertTrue($iterator->valid());
|
||||
$this->assertSame(['key1', 1], [$iterator->key(), $iterator->current()]);
|
||||
$iterator->next();
|
||||
$this->assertTrue($iterator->valid());
|
||||
$this->assertSame(['key2', 2], [$iterator->key(), $iterator->current()]);
|
||||
$iterator->next();
|
||||
$this->assertFalse($iterator->valid());
|
||||
}
|
||||
|
||||
public function testParserSupportsOldDecoderInterface()
|
||||
{
|
||||
$parser = new Parser(new Lexer(['{"key": "value"}']), "", new DeprecatedDecoderImpl());
|
||||
|
||||
foreach ($parser as $key => $value) {
|
||||
$this->assertSame('key', $key);
|
||||
$this->assertSame('value', $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DeprecatedDecoderImpl implements Decoder
|
||||
{
|
||||
/**
|
||||
* @var ItemDecoder
|
||||
*/
|
||||
private $decoder;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->decoder = new ExtJsonDecoder(true);
|
||||
}
|
||||
|
||||
public function decodeKey($jsonScalarKey)
|
||||
{
|
||||
return $this->decoder->decodeKey($jsonScalarKey);
|
||||
}
|
||||
|
||||
public function decodeValue($jsonValue)
|
||||
{
|
||||
return $this->decoder->decodeValue($jsonValue);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user