Skip to content
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

Moves all state to dedicated context class #47

Merged
merged 7 commits into from
Feb 23, 2013
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
19 changes: 0 additions & 19 deletions .jms.yml

This file was deleted.

11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This document details change between individual versions.

For instructions on how to upgrade from one version to another, please see the dedicated UPGRADING document.

0.12 (tbd)
----------
- adds, and exposes SerializationContext/DeserializationContext

0.11 (2013-01-29)
-----------------
Initial Release
19 changes: 19 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
From 0.11 to 0.12
=================

- GraphNavigator::detachObject has been removed, you can directly use Context::stopVisiting instead.
- VisitorInterface::getNavigator was deprecated, instead use Context::accept
- Serializer::setGroups, Serializer::setExclusionStrategy and Serializer::setVersion were removed, these settings must
now be passed as part of a new Context object.

Before:

$serializer->setVersion(1);
$serializer->serialize($data, 'json');

After:

$serializer->serialize($data, 'json', SerializationContext::create()->setVersion(1));

- All visit??? methods of the VisitorInterface, now require a third argument, the Context; the context is for example
passed as an additional argument to handler, exclusion strategies, and also available in event listeners.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.11-dev"
"dev-master": "0.12-dev"
}
}
}
12 changes: 0 additions & 12 deletions src/JMS/Serializer/AbstractVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,11 @@ abstract class AbstractVisitor implements VisitorInterface
{
protected $namingStrategy;

private $serializeNull = false;

public function __construct(PropertyNamingStrategyInterface $namingStrategy)
{
$this->namingStrategy = $namingStrategy;
}

public function setSerializeNull($serializeNull)
{
$this->serializeNull = (bool) $serializeNull;
}

public function shouldSerializeNull()
{
return $this->serializeNull;
}

public function getNamingStrategy()
{
return $this->namingStrategy;
Expand Down
128 changes: 128 additions & 0 deletions src/JMS/Serializer/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace JMS\Serializer;

use JMS\Serializer\Exclusion\ExclusionStrategyInterface;
use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
use JMS\Serializer\Exclusion\VersionExclusionStrategy;
use PhpCollection\Map;

abstract class Context
{
/**
* @var \PhpCollection\Map
*/
public $attributes;

private $format;

/** @var VisitorInterface */
private $visitor;

/** @var GraphNavigator */
private $navigator;

/** @var ExclusionStrategyInterface */
private $exclusionStrategy;

/** @var boolean */
private $serializeNull;

public function __construct()
{
$this->attributes = new Map();
}

public function initialize($format, VisitorInterface $visitor, GraphNavigator $navigator)
{
$this->format = $format;
$this->visitor = $visitor;
$this->navigator = $navigator;
}

public function accept($data, array $type)
{
return $this->navigator->accept($data, $type, $this);
}

public function getVisitor()
{
return $this->visitor;
}

public function getNavigator()
{
return $this->navigator;
}

public function getExclusionStrategy()
{
return $this->exclusionStrategy;
}

public function setAttribute($key, $value)
{
$this->attributes->set($key, $value);

return $this;
}

public function setExclusionStrategy(ExclusionStrategyInterface $strategy)
{
$this->exclusionStrategy = $strategy;

return $this;
}

/**
* @param integer $version
*/
public function setVersion($version)
{
if (null === $version) {
$this->exclusionStrategy = null;

return $this;
}

$this->exclusionStrategy = new VersionExclusionStrategy($version);

return $this;
}

/**
* @param null|array $groups
*/
public function setGroups($groups)
{
if ( ! $groups) {
$this->exclusionStrategy = null;

return $this;
}

$this->exclusionStrategy = new GroupsExclusionStrategy((array) $groups);

return $this;
}

public function setSerializeNull($bool)
{
$this->serializeNull = (boolean) $bool;

return $this;
}

public function shouldSerializeNull()
{
return $this->serializeNull;
}

public function getFormat()
{
return $this->format;
}

abstract public function getDepth();
abstract public function getDirection();
}
37 changes: 37 additions & 0 deletions src/JMS/Serializer/DeserializationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace JMS\Serializer;

class DeserializationContext extends Context
{
private $depth = 0;

public static function create()
{
return new self();
}

public function getDirection()
{
return GraphNavigator::DIRECTION_DESERIALIZATION;
}

public function getDepth()
{
return $this->depth;
}

public function increaseDepth()
{
$this->depth += 1;
}

public function decreaseDepth()
{
if ($this->depth <= 0) {
throw new \LogicException('Depth cannot be smaller than zero.');
}

$this->depth -= 1;
}
}
15 changes: 10 additions & 5 deletions src/JMS/Serializer/EventDispatcher/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@

namespace JMS\Serializer\EventDispatcher;

use JMS\Serializer\VisitorInterface;
use JMS\Serializer\Context;

class Event
{
protected $type;
private $object;
private $visitor;
private $context;

public function __construct(VisitorInterface $visitor, $object, array $type)
public function __construct(Context $context, $object, array $type)
{
$this->visitor = $visitor;
$this->context = $context;
$this->object = $object;
$this->type = $type;
}

public function getVisitor()
{
return $this->visitor;
return $this->context->getVisitor();
}

public function getContext()
{
return $this->context;
}

public function getType()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace JMS\Serializer\EventDispatcher\Subscriber;

use JMS\Serializer\EventDispatcher\Event;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\Exception\ValidationFailedException;
use Symfony\Component\Validator\ValidatorInterface;

class SymfonyValidatorSubscriber implements EventSubscriberInterface
{
private $validator;

public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}

public static function getSubscribedEvents()
{
return array(
array('event' => 'serializer.post_deserialize', 'method' => 'onPostDeserialize'),
);
}

public function onPostDeserialize(Event $event)
{
$context = $event->getContext();

if ($context->getDepth() > 0) {
return;
}

$validator = $this->validator;
$context->attributes->get('validation_groups')->map(
function(array $groups) use ($event, $validator) {
$list = $validator->validate($event->getObject(), $groups);

if ($list->count() > 0) {
throw new ValidationFailedException($list);
}
}
);
}
}
22 changes: 22 additions & 0 deletions src/JMS/Serializer/Exception/ValidationFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace JMS\Serializer\Exception;

use Symfony\Component\Validator\ConstraintViolationList;

class ValidationFailedException extends RuntimeException
{
private $list;

public function __construct(ConstraintViolationList $list)
{
parent::__construct(sprintf('Validation failed with %d error(s).', count($list)));

$this->list = $list;
}

public function getConstraintViolationList()
{
return $this->list;
}
}
10 changes: 5 additions & 5 deletions src/JMS/Serializer/Exclusion/ExclusionStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\NavigatorContext;
use JMS\Serializer\Context;

/**
* Interface for exclusion strategies.
Expand All @@ -33,19 +33,19 @@ interface ExclusionStrategyInterface
* Whether the class should be skipped.
*
* @param ClassMetadata $metadata
* @param NavigatorContext $navigatorContext
* @param Context $navigatorContext
*
* @return boolean
*/
public function shouldSkipClass(ClassMetadata $metadata, NavigatorContext $navigatorContext);
public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext);

/**
* Whether the property should be skipped.
*
* @param PropertyMetadata $property
* @param NavigatorContext $navigatorContext
* @param Context $navigatorContext
*
* @return boolean
*/
public function shouldSkipProperty(PropertyMetadata $property, NavigatorContext $navigatorContext);
public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext);
}
Loading