From dd5748977de4ac29897771fe09d52e9e0e223103 Mon Sep 17 00:00:00 2001 From: EK Date: Wed, 8 Jul 2015 14:26:22 +0300 Subject: [PATCH] Closes #44 --- .../Factories/FactoryInterface.php} | 35 +-- src/Document/Document.php | 8 +- src/Encoder/Encoder.php | 108 ++----- src/Encoder/Factory/EncoderFactory.php | 108 ------- src/Factories/Factory.php | 269 ++++++++++++++++++ src/Parameters/ParametersFactory.php | 118 -------- src/Schema/SchemaFactory.php | 62 ---- tests/Document/DocumentTest.php | 10 +- tests/Document/FactoryTest.php | 4 +- tests/Encoder/Parser/ParserTest.php | 9 +- tests/Encoder/SchemaTest.php | 4 +- tests/Encoder/Stack/StackTest.php | 4 +- tests/Exceptions/RenderContainerTest.php | 9 +- tests/Parameters/FactoryTest.php | 4 +- tests/Parameters/ParameterParserTest.php | 4 +- .../RestrictiveParameterCheckerTest.php | 4 +- tests/Schema/FactoryTest.php | 4 +- 17 files changed, 332 insertions(+), 432 deletions(-) rename src/{Document/DocumentFactory.php => Contracts/Factories/FactoryInterface.php} (52%) delete mode 100644 src/Encoder/Factory/EncoderFactory.php create mode 100644 src/Factories/Factory.php delete mode 100644 src/Parameters/ParametersFactory.php delete mode 100644 src/Schema/SchemaFactory.php diff --git a/src/Document/DocumentFactory.php b/src/Contracts/Factories/FactoryInterface.php similarity index 52% rename from src/Document/DocumentFactory.php rename to src/Contracts/Factories/FactoryInterface.php index faf6b6c6..7d3fcb68 100644 --- a/src/Document/DocumentFactory.php +++ b/src/Contracts/Factories/FactoryInterface.php @@ -1,4 +1,4 @@ -bufferForData[$type][$idx]; unset($this->bufferForData[$type][$idx]); - if (empty($representation[Document::KEYWORD_RELATIONSHIPS]) === true) { + if (empty($representation[self::KEYWORD_RELATIONSHIPS]) === true) { // if no relationships have been added remove empty placeholder - unset($representation[Document::KEYWORD_RELATIONSHIPS]); + unset($representation[self::KEYWORD_RELATIONSHIPS]); } else { // relationship might have meta $relShipsMeta = $resource->getRelationshipsPrimaryMeta(); @@ -262,9 +262,9 @@ public function setResourceCompleted(ResourceObjectInterface $resource) $representation = $this->bufferForIncluded[$type][$idx]; unset($this->bufferForIncluded[$type][$idx]); - if (empty($representation[Document::KEYWORD_RELATIONSHIPS]) === true) { + if (empty($representation[self::KEYWORD_RELATIONSHIPS]) === true) { // if no relationships have been added remove empty placeholder - unset($representation[Document::KEYWORD_RELATIONSHIPS]); + unset($representation[self::KEYWORD_RELATIONSHIPS]); } else { // relationship might have meta $relShipsMeta = $resource->getRelationshipsInclusionMeta(); diff --git a/src/Encoder/Encoder.php b/src/Encoder/Encoder.php index 354431a2..85608f9b 100644 --- a/src/Encoder/Encoder.php +++ b/src/Encoder/Encoder.php @@ -17,17 +17,14 @@ */ use \Iterator; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\JsonApi\Contracts\Document\ErrorInterface; use \Neomerx\JsonApi\Contracts\Encoder\EncoderInterface; use \Neomerx\JsonApi\Contracts\Schema\ContainerInterface; -use \Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface; +use \Neomerx\JsonApi\Contracts\Factories\FactoryInterface; use \Neomerx\JsonApi\Contracts\Schema\SchemaProviderInterface; -use \Neomerx\JsonApi\Contracts\Document\DocumentFactoryInterface; use \Neomerx\JsonApi\Contracts\Encoder\Parser\DataAnalyzerInterface; -use \Neomerx\JsonApi\Contracts\Encoder\Parser\ParserFactoryInterface; -use \Neomerx\JsonApi\Contracts\Parameters\ParametersFactoryInterface; use \Neomerx\JsonApi\Contracts\Parameters\EncodingParametersInterface; -use \Neomerx\JsonApi\Contracts\Encoder\Handlers\HandlerFactoryInterface; /** * @package Neomerx\JsonApi @@ -40,24 +37,9 @@ class Encoder implements EncoderInterface protected $container; /** - * @var DocumentFactoryInterface + * @var FactoryInterface */ - protected $documentFactory; - - /** - * @var ParserFactoryInterface - */ - private $parserFactory; - - /** - * @var HandlerFactoryInterface - */ - private $handlerFactory; - - /** - * @var ParametersFactoryInterface - */ - private $parametersFactory; + protected $factory; /** * @var EncoderOptions|null @@ -65,27 +47,15 @@ class Encoder implements EncoderInterface protected $encoderOptions; /** - * @param DocumentFactoryInterface $documentFactory - * @param ParserFactoryInterface $parserFactory - * @param HandlerFactoryInterface $handlerFactory - * @param ParametersFactoryInterface $parametersFactory - * @param ContainerInterface $container - * @param EncoderOptions|null $encoderOptions + * @param FactoryInterface $factory + * @param array $schemas + * @param EncoderOptions|null $encoderOptions */ - public function __construct( - DocumentFactoryInterface $documentFactory, - ParserFactoryInterface $parserFactory, - HandlerFactoryInterface $handlerFactory, - ParametersFactoryInterface $parametersFactory, - ContainerInterface $container, - EncoderOptions $encoderOptions = null - ) { - $this->container = $container; - $this->parserFactory = $parserFactory; - $this->handlerFactory = $handlerFactory; - $this->encoderOptions = $encoderOptions; - $this->documentFactory = $documentFactory; - $this->parametersFactory = $parametersFactory; + public function __construct(FactoryInterface $factory, array $schemas, EncoderOptions $encoderOptions = null) + { + $this->factory = $factory; + $this->container = $factory->createContainer($schemas); + $this->encoderOptions = $encoderOptions; } /** @@ -97,14 +67,14 @@ public function encode( $meta = null, EncodingParametersInterface $parameters = null ) { - $dataAnalyzer = $this->parserFactory->createAnalyzer($this->container); + $dataAnalyzer = $this->factory->createAnalyzer($this->container); $parameters = $this->getEncodingParameters($data, $dataAnalyzer, $parameters); - $docWriter = $this->documentFactory->createDocument(); - $parserManager = $this->parserFactory->createManager($parameters); - $parser = $this->parserFactory->createParser($dataAnalyzer, $parserManager); - $interpreter = $this->handlerFactory->createReplyInterpreter($docWriter, $parameters); + $docWriter = $this->factory->createDocument(); + $parserManager = $this->factory->createManager($parameters); + $parser = $this->factory->createParser($dataAnalyzer, $parserManager); + $interpreter = $this->factory->createReplyInterpreter($docWriter, $parameters); $this->encoderOptions !== null && $this->encoderOptions->getUrlPrefix() !== null ? $docWriter->setUrlPrefix($this->encoderOptions->getUrlPrefix()) : null; @@ -128,7 +98,7 @@ public function encode( */ public function error(ErrorInterface $error) { - $docWriter = $this->documentFactory->createDocument(); + $docWriter = $this->factory->createDocument(); $docWriter->addError($error); @@ -140,7 +110,7 @@ public function error(ErrorInterface $error) */ public function errors($errors) { - $docWriter = $this->documentFactory->createDocument(); + $docWriter = $this->factory->createDocument(); foreach ($errors as $error) { assert('$error instanceof '.ErrorInterface::class); $docWriter->addError($error); @@ -154,7 +124,7 @@ public function errors($errors) */ public function meta($meta) { - $docWriter = $this->documentFactory->createDocument(); + $docWriter = $this->factory->createDocument(); $docWriter->setMetaToDocument($meta); $docWriter->unsetData(); @@ -194,41 +164,15 @@ protected function encodeToJson(array $document) */ public static function instance(array $schemas, EncoderOptions $encodeOptions = null) { - /** @var SchemaFactoryInterface $schemaFactory */ - /** @var DocumentFactoryInterface $documentFactory */ - /** @var ParserFactoryInterface $parserFactory */ - /** @var HandlerFactoryInterface $handlerFactory */ - /** @var ParametersFactoryInterface $parameterFactory */ - list($schemaFactory, $documentFactory, $parserFactory, $handlerFactory, $parameterFactory) = - static::getFactories(); - - $container = $schemaFactory->createContainer($schemas); - - return new self( - $documentFactory, - $parserFactory, - $handlerFactory, - $parameterFactory, - $container, - $encodeOptions - ); + return new self(static::getFactory(), $schemas, $encodeOptions); } /** - * @return array [$schemaFactory, $documentFactory, $parserFactory, $handlerFactory, $parameterFactory] + * @return FactoryInterface */ - protected static function getFactories() + protected static function getFactory() { - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $schemaFactory = new \Neomerx\JsonApi\Schema\SchemaFactory(); - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $documentFactory = new \Neomerx\JsonApi\Document\DocumentFactory(); - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $parserFactory = $handlerFactory = new \Neomerx\JsonApi\Encoder\Factory\EncoderFactory(); - /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - $parameterFactory = new \Neomerx\JsonApi\Parameters\ParametersFactory(); - - return [$schemaFactory, $documentFactory, $parserFactory, $handlerFactory, $parameterFactory]; + return new Factory(); } /** @@ -245,14 +189,14 @@ private function getEncodingParameters($data, DataAnalyzerInterface $analyzer, $ list($isDataEmpty, , $schema) = $analyzer->analyze($data); if ($isDataEmpty === true && $parameters === null) { - return $this->parametersFactory->createEncodingParameters(); + return $this->factory->createEncodingParameters(); } elseif ($parameters !== null && $parameters->getIncludePaths() !== null) { return $parameters; } else { $includePaths = $schema->getIncludePaths(); $fieldSets = $parameters === null ? null : $parameters->getFieldSets(); - return $this->parametersFactory->createEncodingParameters($includePaths, $fieldSets); + return $this->factory->createEncodingParameters($includePaths, $fieldSets); } } } diff --git a/src/Encoder/Factory/EncoderFactory.php b/src/Encoder/Factory/EncoderFactory.php deleted file mode 100644 index 2ae39f13..00000000 --- a/src/Encoder/Factory/EncoderFactory.php +++ /dev/null @@ -1,108 +0,0 @@ -schemaFactory = new SchemaFactory(); - $this->documentFactory = new DocumentFactory(); - $this->document = $this->documentFactory->createDocument(); + + $this->documentFactory = $this->schemaFactory = new Factory(); + + $this->document = $this->documentFactory->createDocument(); } /** diff --git a/tests/Document/FactoryTest.php b/tests/Document/FactoryTest.php index 8b838136..d39b039e 100644 --- a/tests/Document/FactoryTest.php +++ b/tests/Document/FactoryTest.php @@ -17,8 +17,8 @@ */ use \Neomerx\JsonApi\Schema\Link; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; -use \Neomerx\JsonApi\Document\DocumentFactory; use \Neomerx\JsonApi\Contracts\Document\DocumentInterface; use \Neomerx\JsonApi\Contracts\Document\DocumentFactoryInterface; @@ -39,7 +39,7 @@ class FactoryTest extends BaseTestCase protected function setUp() { parent::setUp(); - $this->factory = new DocumentFactory(); + $this->factory = new Factory(); } /** diff --git a/tests/Encoder/Parser/ParserTest.php b/tests/Encoder/Parser/ParserTest.php index c223ef94..1dde6ffb 100644 --- a/tests/Encoder/Parser/ParserTest.php +++ b/tests/Encoder/Parser/ParserTest.php @@ -18,13 +18,12 @@ use \Neomerx\Tests\JsonApi\Data\Post; use \Neomerx\Tests\JsonApi\Data\Author; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\Data\Comment; use \Neomerx\Tests\JsonApi\BaseTestCase; -use \Neomerx\JsonApi\Schema\SchemaFactory; use \Neomerx\Tests\JsonApi\Data\PostSchema; use \Neomerx\Tests\JsonApi\Data\AuthorSchema; use \Neomerx\Tests\JsonApi\Data\CommentSchema; -use \Neomerx\JsonApi\Encoder\Factory\EncoderFactory; use \Neomerx\JsonApi\Contracts\Encoder\Parser\ParserInterface; use \Neomerx\JsonApi\Contracts\Encoder\Parser\ParserReplyInterface; @@ -85,9 +84,9 @@ protected function setUp() Comment::class => CommentSchema::class, Post::class => PostSchema::class, ]; - $container = (new SchemaFactory())->createContainer($schemas); - $encoderFactory = new EncoderFactory(); - $this->parser = $encoderFactory->createParser($encoderFactory->createAnalyzer($container)); + $factory = new Factory(); + $container = $factory->createContainer($schemas); + $this->parser = $factory->createParser($factory->createAnalyzer($container)); $this->author = Author::instance(9, 'Dan', 'Gebhardt'); $this->comments = [ diff --git a/tests/Encoder/SchemaTest.php b/tests/Encoder/SchemaTest.php index e5f65f84..700d32be 100644 --- a/tests/Encoder/SchemaTest.php +++ b/tests/Encoder/SchemaTest.php @@ -16,8 +16,8 @@ * limitations under the License. */ +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; -use \Neomerx\JsonApi\Schema\SchemaFactory; use \Neomerx\Tests\JsonApi\Data\DummySchema; /** @@ -37,7 +37,7 @@ protected function setUp() { parent::setUp(); - $schemaFactory = new SchemaFactory(); + $schemaFactory = new Factory(); $this->schema = new DummySchema($schemaFactory, $schemaFactory->createContainer()); } diff --git a/tests/Encoder/Stack/StackTest.php b/tests/Encoder/Stack/StackTest.php index ed3fdb73..64316aae 100644 --- a/tests/Encoder/Stack/StackTest.php +++ b/tests/Encoder/Stack/StackTest.php @@ -17,8 +17,8 @@ */ use \Mockery; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; -use \Neomerx\JsonApi\Encoder\Factory\EncoderFactory; use \Neomerx\JsonApi\Contracts\Encoder\Stack\StackInterface; use \Neomerx\JsonApi\Contracts\Schema\ResourceObjectInterface; use \Neomerx\JsonApi\Contracts\Schema\RelationshipObjectInterface; @@ -50,7 +50,7 @@ protected function setUp() { parent::setUp(); - $this->stack = (new EncoderFactory())->createStack(); + $this->stack = (new Factory())->createStack(); $this->mockLinkObject = Mockery::mock(RelationshipObjectInterface::class); $this->mockResourceObject = Mockery::mock(ResourceObjectInterface::class); diff --git a/tests/Exceptions/RenderContainerTest.php b/tests/Exceptions/RenderContainerTest.php index ed85e0f3..1ca66458 100644 --- a/tests/Exceptions/RenderContainerTest.php +++ b/tests/Exceptions/RenderContainerTest.php @@ -23,9 +23,9 @@ use \InvalidArgumentException; use \Neomerx\JsonApi\Document\Error; use \Neomerx\JsonApi\Encoder\Encoder; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; use \Neomerx\JsonApi\Exceptions\RenderContainer; -use \Neomerx\JsonApi\Parameters\ParametersFactory; use \Neomerx\JsonApi\Contracts\Exceptions\RenderContainerInterface; use \Neomerx\JsonApi\Contracts\Integration\NativeResponsesInterface; use \Neomerx\JsonApi\Contracts\Parameters\SupportedExtensionsInterface; @@ -66,12 +66,7 @@ protected function setUp() /** @var NativeResponsesInterface $mockResponses */ $mockResponses = $this->mockResponses; - $this->container = new RenderContainer( - new ParametersFactory(), - $mockResponses, - $extensionsClosure, - self::DEFAULT_CODE - ); + $this->container = new RenderContainer(new Factory(), $mockResponses, $extensionsClosure, self::DEFAULT_CODE); } /** diff --git a/tests/Parameters/FactoryTest.php b/tests/Parameters/FactoryTest.php index d273897e..98480ffc 100644 --- a/tests/Parameters/FactoryTest.php +++ b/tests/Parameters/FactoryTest.php @@ -17,9 +17,9 @@ */ use \Mockery; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; use \Neomerx\JsonApi\Parameters\Headers\MediaType; -use \Neomerx\JsonApi\Parameters\ParametersFactory; use \Neomerx\JsonApi\Contracts\Parameters\SortParameterInterface; use \Neomerx\JsonApi\Contracts\Parameters\Headers\HeaderInterface; use \Neomerx\JsonApi\Contracts\Parameters\ParametersFactoryInterface; @@ -41,7 +41,7 @@ class FactoryTest extends BaseTestCase protected function setUp() { parent::setUp(); - $this->factory = new ParametersFactory(); + $this->factory = new Factory(); } /** diff --git a/tests/Parameters/ParameterParserTest.php b/tests/Parameters/ParameterParserTest.php index 57c36d4a..81acfff0 100644 --- a/tests/Parameters/ParameterParserTest.php +++ b/tests/Parameters/ParameterParserTest.php @@ -18,8 +18,8 @@ use \Mockery; use \Mockery\MockInterface; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; -use \Neomerx\JsonApi\Parameters\ParametersFactory; use \Neomerx\JsonApi\Contracts\Integration\CurrentRequestInterface; use \Neomerx\JsonApi\Contracts\Parameters\ParametersParserInterface; use \Neomerx\JsonApi\Contracts\Parameters\Headers\MediaTypeInterface; @@ -55,7 +55,7 @@ protected function setUp() { parent::setUp(); - $this->parser = (new ParametersFactory())->createParametersParser(); + $this->parser = (new Factory())->createParametersParser(); $this->mockRequest = Mockery::mock(CurrentRequestInterface::class); $this->mockThrower = Mockery::mock(ExceptionThrowerInterface::class); } diff --git a/tests/Parameters/RestrictiveParameterCheckerTest.php b/tests/Parameters/RestrictiveParameterCheckerTest.php index 71353aa5..10de1bf3 100644 --- a/tests/Parameters/RestrictiveParameterCheckerTest.php +++ b/tests/Parameters/RestrictiveParameterCheckerTest.php @@ -18,10 +18,10 @@ use \Mockery; use \Mockery\MockInterface; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; use \Neomerx\JsonApi\Codec\CodecMatcher; use \Neomerx\JsonApi\Parameters\Headers\MediaType; -use \Neomerx\JsonApi\Parameters\ParametersFactory; use \Neomerx\JsonApi\Parameters\RestrictiveParameterChecker; use \Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface; use \Neomerx\JsonApi\Contracts\Integration\CurrentRequestInterface; @@ -76,7 +76,7 @@ protected function setUp() { parent::setUp(); - $this->parser = (new ParametersFactory())->createParametersParser(); + $this->parser = (new Factory())->createParametersParser(); $this->mockRequest = Mockery::mock(CurrentRequestInterface::class); $this->mockThrower = Mockery::mock(ExceptionThrowerInterface::class); } diff --git a/tests/Schema/FactoryTest.php b/tests/Schema/FactoryTest.php index 2d3c9828..980388a1 100644 --- a/tests/Schema/FactoryTest.php +++ b/tests/Schema/FactoryTest.php @@ -18,8 +18,8 @@ use \Mockery; use \stdClass; +use \Neomerx\JsonApi\Factories\Factory; use \Neomerx\Tests\JsonApi\BaseTestCase; -use \Neomerx\JsonApi\Schema\SchemaFactory; use \Neomerx\JsonApi\Contracts\Schema\LinkInterface; use \Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface; use \Neomerx\JsonApi\Contracts\Schema\SchemaProviderInterface; @@ -40,7 +40,7 @@ class FactoryTest extends BaseTestCase protected function setUp() { parent::setUp(); - $this->factory = new SchemaFactory(); + $this->factory = new Factory(); } /**