mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-01-17 13:08:16 +01:00
Add factory method to parser
This commit is contained in:
parent
a5146b2ef6
commit
ab11f4e416
@ -38,8 +38,7 @@ class JsonParser implements IteratorAggregate
|
||||
public function __construct(mixed $source)
|
||||
{
|
||||
$this->config = new Config();
|
||||
$source = AnySource::from($source, $this->config);
|
||||
$this->parser = new Parser(new Lexer($source), $this->config);
|
||||
$this->parser = Parser::for(AnySource::from($source, $this->config));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Cerbero\JsonParser;
|
||||
|
||||
use Cerbero\JsonParser\Pointers\Pointers;
|
||||
use Cerbero\JsonParser\Sources\Source;
|
||||
use Cerbero\JsonParser\Tokens\Token;
|
||||
use IteratorAggregate;
|
||||
use Traversable;
|
||||
@ -39,6 +40,17 @@ class Parser implements IteratorAggregate
|
||||
$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
|
||||
*
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user