mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-07-13 18:26:25 +02:00
Add factory method to parser
This commit is contained in:
@ -38,8 +38,7 @@ class JsonParser implements IteratorAggregate
|
|||||||
public function __construct(mixed $source)
|
public function __construct(mixed $source)
|
||||||
{
|
{
|
||||||
$this->config = new Config();
|
$this->config = new Config();
|
||||||
$source = AnySource::from($source, $this->config);
|
$this->parser = Parser::for(AnySource::from($source, $this->config));
|
||||||
$this->parser = new Parser(new Lexer($source), $this->config);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Cerbero\JsonParser;
|
namespace Cerbero\JsonParser;
|
||||||
|
|
||||||
use Cerbero\JsonParser\Pointers\Pointers;
|
use Cerbero\JsonParser\Pointers\Pointers;
|
||||||
|
use Cerbero\JsonParser\Sources\Source;
|
||||||
use Cerbero\JsonParser\Tokens\Token;
|
use Cerbero\JsonParser\Tokens\Token;
|
||||||
use IteratorAggregate;
|
use IteratorAggregate;
|
||||||
use Traversable;
|
use Traversable;
|
||||||
@ -39,6 +40,17 @@ class Parser implements IteratorAggregate
|
|||||||
$this->pointers = new Pointers(...$config->pointers);
|
$this->pointers = new Pointers(...$config->pointers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate the class statically
|
||||||
|
*
|
||||||
|
* @param Source $source
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public static function for(Source $source): static
|
||||||
|
{
|
||||||
|
return new static(new Lexer($source), $source->config());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the JSON fragments
|
* Retrieve the JSON fragments
|
||||||
*
|
*
|
||||||
|
@ -87,6 +87,16 @@ abstract class Source implements IteratorAggregate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the underlying configuration
|
||||||
|
*
|
||||||
|
* @return Config
|
||||||
|
*/
|
||||||
|
public function config(): Config
|
||||||
|
{
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the size of the JSON source and cache it
|
* Retrieve the size of the JSON source and cache it
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user