Skip to content

Commit

Permalink
Closes #40. Intended usage
Browse files Browse the repository at this point in the history
- Inherit `Container` and override `getResourceType` method
- Inherit `SchemaFactory` and override `createContainer` method
- Inherit `Encoder` and override `getFactories` method where `$schemaFactory` should return the new schema factory instance
  • Loading branch information
neomerx committed Jun 29, 2015
1 parent 5d6c4c4 commit 423f211
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
38 changes: 28 additions & 10 deletions src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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\Schema\SchemaProviderInterface;
use \Neomerx\JsonApi\Contracts\Document\DocumentFactoryInterface;
use \Neomerx\JsonApi\Contracts\Encoder\Parser\DataAnalyzerInterface;
Expand Down Expand Up @@ -192,25 +193,42 @@ protected function encodeToJson(array $document)
* @return Encoder
*/
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) =
self::getFactories();

$container = $schemaFactory->createContainer($schemas);

return new self(
$documentFactory,
$parserFactory,
$handlerFactory,
$parameterFactory,
$container,
$encodeOptions
);
}

/**
* @return array [$schemaFactory, $documentFactory, $parserFactory, $handlerFactory, $parameterFactory]
*/
protected static function getFactories()
{
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$schemaFactory = new \Neomerx\JsonApi\Schema\SchemaFactory();
$container = $schemaFactory->createContainer($schemas);
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$documentFactory = new \Neomerx\JsonApi\Document\DocumentFactory();
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$encoderFactory = new \Neomerx\JsonApi\Encoder\Factory\EncoderFactory();
$parserFactory = $handlerFactory = new \Neomerx\JsonApi\Encoder\Factory\EncoderFactory();
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$parameterFactory = new \Neomerx\JsonApi\Parameters\ParametersFactory();

return new self(
$documentFactory,
$encoderFactory,
$encoderFactory,
$parameterFactory,
$container,
$encodeOptions
);
return [$schemaFactory, $documentFactory, $parserFactory, $handlerFactory, $parameterFactory];
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/Schema/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function registerArray(array $schemas)
*/
public function getSchema($resource)
{
$resourceType = get_class($resource);
$resourceType = $this->getResourceType($resource);

if (isset($this->createdProviders[$resourceType])) {
return $this->createdProviders[$resourceType];
Expand All @@ -105,4 +105,14 @@ public function getSchema($resource)

return $schema;
}

/**
* @param object $resource
*
* @return string
*/
protected function getResourceType($resource)
{
return get_class($resource);
}
}

0 comments on commit 423f211

Please sign in to comment.