Skip to content

Refactoring #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Dunglas\JsonLdApiBundle\Event\Events;
use Dunglas\JsonLdApiBundle\Event\ObjectEvent;
use Dunglas\JsonLdApiBundle\Resource;
use Dunglas\JsonLdApiBundle\JsonLd\Resource;
use Dunglas\JsonLdApiBundle\Response\JsonLdResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
Expand Down
8 changes: 5 additions & 3 deletions Doctrine/Orm/DataManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Dunglas\JsonLdApiBundle\Model\DataManipulatorInterface;
use Dunglas\JsonLdApiBundle\Resource;
use Dunglas\JsonLdApiBundle\Resources;
use Dunglas\JsonLdApiBundle\JsonLd\Resource;
use Dunglas\JsonLdApiBundle\JsonLd\Resources;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;

/**
Expand Down Expand Up @@ -47,7 +49,7 @@ class DataManipulator implements DataManipulatorInterface
private $defaultOrder;

/**
* @param RouterInterface $routerInterface
* @param RouterInterface $router
* @param ManagerRegistry $managerRegistry
* @param Resources $resources
* @param int $defaultByPage
Expand Down
2 changes: 1 addition & 1 deletion Event/ObjectEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Dunglas\JsonLdApiBundle\Event;

use Dunglas\JsonLdApiBundle\Resource;
use Dunglas\JsonLdApiBundle\JsonLd\Resource;
use Symfony\Component\EventDispatcher\Event;

/**
Expand Down
40 changes: 18 additions & 22 deletions ApiDocumentationBuilder.php → JsonLd/ApiDocumentationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Dunglas\JsonLdApiBundle;
namespace Dunglas\JsonLdApiBundle\JsonLd;

use Dunglas\JsonLdApiBundle\Mapping\ClassMetadataFactory;
use Symfony\Component\Routing\RouterInterface;
Expand All @@ -21,11 +21,6 @@
*/
class ApiDocumentationBuilder
{
/**
* @var string
*/
const HYDRA_NS = 'http://www.w3.org/ns/hydra/core#';

/**
* @var Resources
*/
Expand Down Expand Up @@ -193,7 +188,12 @@ public function getApiDocumentation()

// Resources
foreach ($this->resources as $resource) {
$metadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass());
$classMetadata = $this->classMetadataFactory->getMetadataFor(
$resource->getEntityClass(),
$resource->getNormalizationGroups(),
$resource->getDenormalizationGroups(),
$resource->getValidationGroups()
);
$shortName = $resource->getShortName();

$supportedClass = [
Expand All @@ -202,35 +202,31 @@ public function getApiDocumentation()
'hydra:title' => $resource->getShortName(),
];

$description = $metadata->getDescription();
$description = $classMetadata->getDescription();
if ($description) {
$supportedClass['hydra:description'] = $description;
}

$attributes = $metadata->getAttributes(
$resource->getNormalizationGroups(),
$resource->getDenormalizationGroups(),
$resource->getValidationGroups()
);
$attributes = $classMetadata->getAttributes();

$supportedClass['hydra:supportedProperty'] = [];
foreach ($attributes as $name => $details) {
foreach ($attributes as $attributeName => $attribute) {
$supportedProperty = [
'@type' => 'hydra:SupportedProperty',
'hydra:property' => [
'@id' => sprintf('%s/%s', $shortName, $name),
'@id' => sprintf('%s/%s', $shortName, $attributeName),
'@type' => 'rdf:Property',
'rdfs:label' => $name,
'rdfs:label' => $attributeName,
'domain' => $shortName,
],
'hydra:title' => $name,
'hydra:required' => $details['required'],
'hydra:readable' => $details['readable'],
'hydra:writable' => $details['writable'],
'hydra:title' => $attributeName,
'hydra:required' => $attribute->isRequired(),
'hydra:readable' => $attribute->isReadable(),
'hydra:writable' => $attribute->isWritable(),
];

if ($details['description']) {
$supportedProperty['hydra:description'] = $details['description'];
if ($description = $attribute->getDescription()) {
$supportedProperty['hydra:description'] = $description;
}

$supportedClass['hydra:supportedProperty'][] = $supportedProperty;
Expand Down
11 changes: 6 additions & 5 deletions ContextBuilder.php → JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Dunglas\JsonLdApiBundle;
namespace Dunglas\JsonLdApiBundle\JsonLd;

use Dunglas\JsonLdApiBundle\Mapping\ClassMetadataFactory;
use Symfony\Component\Routing\RouterInterface;
Expand Down Expand Up @@ -60,14 +60,15 @@ public function buildContext(Resource $resource = null)
];

if ($resource) {
$attributes = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass())->getAttributes(
$attributes = $this->classMetadataFactory->getMetadataFor(
$resource->getEntityClass(),
$resource->getNormalizationGroups(),
$resource->getDenormalizationGroups(),
$resource->getValidationGroups()
);
)->getAttributes();

foreach ($attributes as $attributeName => $data) {
if ($data['type']) {
foreach ($attributes as $attributeName => $attribute) {
if (isset($attribute->getTypes()[0]) && 'object' === $attribute->getTypes()[0]->getType()) {
$context[$attributeName] = ['@type' => '@id'];
}
}
Expand Down
4 changes: 2 additions & 2 deletions EntrypointBuilder.php → JsonLd/EntrypointBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
* file that was distributed with this source code.
*/

namespace Dunglas\JsonLdApiBundle;
namespace Dunglas\JsonLdApiBundle\JsonLd;

use Symfony\Component\Routing\RouterInterface;

/**
* Hydra's Entrypoint builder.
* API Entrypoint builder.
*
* @author Kévin Dunglas <[email protected]>
*/
Expand Down
2 changes: 1 addition & 1 deletion Resource.php → JsonLd/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Dunglas\JsonLdApiBundle;
namespace Dunglas\JsonLdApiBundle\JsonLd;

use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\Routing\Route;
Expand Down
4 changes: 3 additions & 1 deletion Resources.php → JsonLd/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* file that was distributed with this source code.
*/

namespace Dunglas\JsonLdApiBundle;
namespace Dunglas\JsonLdApiBundle\JsonLd;

use ArrayObject;

/**
* A collection of {@see Resource} classes.
Expand Down
204 changes: 204 additions & 0 deletions Mapping/AttributeMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

/*
* This file is part of the DunglasJsonLdApiBundle package.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Dunglas\JsonLdApiBundle\Mapping;

/**
* AttributeMetadata.
*
* @author Kévin Dunglas <[email protected]>
*/
class AttributeMetadata
{
/**
* @var string
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link getName()} instead.
*/
public $name;
/**
* @var \PropertyInfo\Type[]
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link getTypes()} instead.
*/
public $types;
/**
* @var string
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link getDescription()} instead.
*/
public $description;
/**
* @var bool
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link isReadable()} instead.
*/
public $readable = false;
/**
* @var bool
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link isWritable()} instead.
*/
public $writable = false;
/**
* @var bool
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link isRequired()} instead.
*/
public $required = false;

/**
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}

/**
* Gets name.
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set types.
*
* @param \PropertyInfo\Type[] $types
*/
public function setTypes(array $types)
{
$this->types = $types;
}

/**
* Gets types.
*
* @return \PropertyInfo\Type[]
*/
public function getTypes()
{
return $this->types;
}

/**
* Gets description.
*
* @return string
*/
public function getDescription()
{
return $this->description;
}

/**
* Sets description.
*
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}

/**
* Is readable?
*
* @return bool
*/
public function isReadable()
{
return $this->readable;
}

/**
* Sets readable.
*
* @param bool $readable
*/
public function setReadable($readable)
{
$this->readable = $readable;
}

/**
* Is writable?
*
* @return boolean
*/
public function isWritable()
{
return $this->writable;
}

/**
* Sets writable.
*
* @param boolean $writable
*/
public function setWritable($writable)
{
$this->writable = $writable;
}

/**
* Is required?
*
* @return boolean
*/
public function isRequired()
{
return $this->required;
}

/**
* Sets required.
*
* @param boolean $required
*/
public function setRequired($required)
{
$this->required = $required;
}

/**
* Returns the names of the properties that should be serialized.
*
* @return string[]
*/
public function __sleep()
{
return [
'name',
'types',
'description',
'readable',
'writable',
'required',
];
}
}
Loading