Add factory method to parser

This commit is contained in:
Andrea Marco Sartori 2022-11-10 21:45:56 +10:00
parent a5146b2ef6
commit ab11f4e416
3 changed files with 23 additions and 2 deletions

View File

@ -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));
}
/**

View File

@ -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
*

View File

@ -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
*