Skip to content

Commit

Permalink
Closes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jul 10, 2015
1 parent 24cd1a9 commit dd57489
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 432 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php namespace Neomerx\JsonApi\Document;
<?php namespace Neomerx\JsonApi\Contracts\Factories;

/**
* Copyright 2015 info@neomerx.com (www.neomerx.com)
Expand All @@ -16,35 +16,16 @@
* limitations under the License.
*/

use \Neomerx\JsonApi\Contracts\Schema\LinkInterface;
use \Neomerx\JsonApi\Contracts\Document\DocumentFactoryInterface;
use \Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface as SchFI;
use \Neomerx\JsonApi\Contracts\Document\DocumentFactoryInterface as DFI;
use \Neomerx\JsonApi\Contracts\Encoder\Stack\StackFactoryInterface as StkFI;
use \Neomerx\JsonApi\Contracts\Encoder\Parser\ParserFactoryInterface as PrsFI;
use \Neomerx\JsonApi\Contracts\Parameters\ParametersFactoryInterface as PrmFI;
use \Neomerx\JsonApi\Contracts\Encoder\Handlers\HandlerFactoryInterface as HFI;

/**
* @package Neomerx\JsonApi
*/
class DocumentFactory implements DocumentFactoryInterface
interface FactoryInterface extends DFI, PrsFI, StkFI, HFI, PrmFI, SchFI
{
/**
* @inheritdoc
*/
public function createDocument()
{
return new Document();
}

/**
* @inheritdoc
*/
public function createError(
$idx = null,
LinkInterface $aboutLink = null,
$status = null,
$code = null,
$title = null,
$detail = null,
$source = null,
array $meta = null
) {
return new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
}
}
8 changes: 4 additions & 4 deletions src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ public function setResourceCompleted(ResourceObjectInterface $resource)
$representation = $this->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();
Expand All @@ -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();
Expand Down
108 changes: 26 additions & 82 deletions src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,52 +37,25 @@ 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
*/
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;
}

/**
Expand All @@ -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;
Expand All @@ -128,7 +98,7 @@ public function encode(
*/
public function error(ErrorInterface $error)
{
$docWriter = $this->documentFactory->createDocument();
$docWriter = $this->factory->createDocument();

$docWriter->addError($error);

Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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);
}
}
}
108 changes: 0 additions & 108 deletions src/Encoder/Factory/EncoderFactory.php

This file was deleted.

Loading

0 comments on commit dd57489

Please sign in to comment.