forked from FriendsOfSymfony/FOSRestBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
992 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\Context\Adapter; | ||
|
||
use FOS\RestBundle\Context\Context; | ||
use JMS\Serializer\Context as JMSContext; | ||
use JMS\Serializer\DeserializationContext as JMSDeserializationContext; | ||
use JMS\Serializer\SerializationContext as JMSSerializationContext; | ||
|
||
/** | ||
* BC FOSRestBundle < 2.0. | ||
* | ||
* @author Ener-Getick <egetick@gmail.com> | ||
* | ||
* @internal | ||
* | ||
* @todo Remove this in 2.0 | ||
*/ | ||
final class JMSContextAdapter | ||
{ | ||
public static function convertSerializationContext(Context $context) | ||
{ | ||
$newContext = JMSSerializationContext::create(); | ||
self::fillContext($context, $newContext); | ||
|
||
return $newContext; | ||
} | ||
|
||
public static function convertDeserializationContext(Context $context) | ||
{ | ||
$newContext = JMSDeserializationContext::create(); | ||
if (null !== $context->getMaxDepth()) { | ||
for ($i = 0; $i < $context->getMaxDepth(); ++$i) { | ||
$newContext->increaseDepth(); | ||
} | ||
} | ||
|
||
self::fillContext($context, $newContext); | ||
|
||
return $newContext; | ||
} | ||
|
||
/** | ||
* Fill a jms context. | ||
* | ||
* @param Context $context | ||
* @param JMSContext $newContext | ||
* | ||
* @return JMSContext | ||
*/ | ||
private static function fillContext(Context $context, JMSContext $newContext) | ||
{ | ||
foreach ($context->getAttributes() as $key => $value) { | ||
$newContext->attributes->set($key, $value); | ||
} | ||
|
||
if (null !== $context->getVersion()) { | ||
$newContext->setVersion($context->getVersion()); | ||
} | ||
$groups = $context->getGroups(); | ||
if (!empty($groups)) { | ||
$newContext->setGroups($context->getGroups()); | ||
} | ||
if (null !== $context->getMaxDepth()) { | ||
$newContext->enableMaxDepthChecks(); | ||
} | ||
if (null !== $context->getSerializeNull()) { | ||
$newContext->setSerializeNull($context->getSerializeNull()); | ||
} | ||
|
||
return $newContext; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\Context; | ||
|
||
/** | ||
* Stores the serialization or deserialization context (groups, version, ...). | ||
* | ||
* @author Ener-Getick <egetick@gmail.com> | ||
*/ | ||
final class Context | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $attributes = array(); | ||
/** | ||
* @var int|null | ||
*/ | ||
private $version; | ||
/** | ||
* @var array | ||
*/ | ||
private $groups = array(); | ||
/** | ||
* @var int | ||
*/ | ||
private $maxDepth; | ||
/** | ||
* @var bool | ||
*/ | ||
private $serializeNull; | ||
|
||
/** | ||
* Sets an attribute. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* | ||
* @return self | ||
*/ | ||
public function setAttribute($key, $value) | ||
{ | ||
$this->attributes[$key] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Checks if contains a normalization attribute. | ||
* | ||
* @param string $key | ||
* | ||
* @return bool | ||
*/ | ||
public function hasAttribute($key) | ||
{ | ||
return isset($this->attributes[$key]); | ||
} | ||
|
||
/** | ||
* Gets an attribute. | ||
* | ||
* @param string $key | ||
* | ||
* @return mixed | ||
*/ | ||
public function getAttribute($key) | ||
{ | ||
if (isset($this->attributes[$key])) { | ||
return $this->attributes[$key]; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the attributes. | ||
* | ||
* @return array | ||
*/ | ||
public function getAttributes() | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
/** | ||
* Sets the normalization version. | ||
* | ||
* @param int|null $version | ||
* | ||
* @return self | ||
*/ | ||
public function setVersion($version) | ||
{ | ||
$this->version = $version; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the normalization version. | ||
* | ||
* @return int|null | ||
*/ | ||
public function getVersion() | ||
{ | ||
return $this->version; | ||
} | ||
|
||
/** | ||
* Adds a normalization group. | ||
* | ||
* @param string $group | ||
* | ||
* @return self | ||
*/ | ||
public function addGroup($group) | ||
{ | ||
if (!in_array($group, $this->groups)) { | ||
$this->groups[] = $group; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Adds normalization groups. | ||
* | ||
* @param string[] $groups | ||
* | ||
* @return self | ||
*/ | ||
public function addGroups(array $groups) | ||
{ | ||
foreach ($groups as $group) { | ||
$this->addGroup($group); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the normalization groups. | ||
* | ||
* @return array | ||
*/ | ||
public function getGroups() | ||
{ | ||
return $this->groups; | ||
} | ||
|
||
/** | ||
* Sets the normalization max depth. | ||
* | ||
* @param int|null $depth | ||
* | ||
* @return self | ||
*/ | ||
public function setMaxDepth($maxDepth) | ||
{ | ||
$this->maxDepth = $maxDepth; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the normalization max depth. | ||
* | ||
* @return int|null | ||
*/ | ||
public function getMaxDepth() | ||
{ | ||
return $this->maxDepth; | ||
} | ||
|
||
/** | ||
* Sets serialize null. | ||
* | ||
* @param bool|null $serializeNull | ||
* | ||
* @return self | ||
*/ | ||
public function setSerializeNull($serializeNull) | ||
{ | ||
$this->serializeNull = $serializeNull; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets serialize null. | ||
* | ||
* @return bool|null | ||
*/ | ||
public function getSerializeNull() | ||
{ | ||
return $this->serializeNull; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.