partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit f3474b932ccbe912e7597ea2a4e3035874b819c5
parent aea9c9a0f096bcb5824c78b3c01a647ebe210696
Author: Felicitus <felicitus@felicitus.org>
Date:   Mon,  8 Jun 2015 21:54:20 +0200

Added partial doctrine object updates via REST

Diffstat:
Mapp/config/config.yml | 9+++++++++
Asrc/PartKeepr/DoctrineReflectionBundle/Services/InitializedObjectConstructor.php | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/app/config/config.yml b/app/config/config.yml @@ -7,6 +7,7 @@ fos_rest: format_listener: true param_fetcher_listener: force view: + exception_wrapper_handler: PartKeepr\DoctrineReflectionBundle\Exception\ExceptionWrapperHandler default_engine: php formats: json: true @@ -81,5 +82,13 @@ doctrine: twig: exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction' +services: + jms_serializer.object_constructor: + alias: jms_serializer.initialized_object_constructor + public: false + jms_serializer.initialized_object_constructor: + class: PartKeepr\DoctrineReflectionBundle\Services\InitializedObjectConstructor + arguments: ["@jms_serializer.doctrine_object_constructor"] + parameters: jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy \ No newline at end of file diff --git a/src/PartKeepr/DoctrineReflectionBundle/Services/InitializedObjectConstructor.php b/src/PartKeepr/DoctrineReflectionBundle/Services/InitializedObjectConstructor.php @@ -0,0 +1,61 @@ +<?php + +/* + * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace PartKeepr\DoctrineReflectionBundle\Services; + +use JMS\Serializer\VisitorInterface; +use JMS\Serializer\Metadata\ClassMetadata; +use JMS\Serializer\DeserializationContext; +use JMS\Serializer\Construction\ObjectConstructorInterface; + +/** + * Object constructor that allows deserialization into already constructed + * objects passed through the deserialization context + */ +class InitializedObjectConstructor implements ObjectConstructorInterface +{ + private $fallbackConstructor; + + /** + * Constructor. + * + * @param ObjectConstructorInterface $fallbackConstructor Fallback object constructor + */ + public function __construct(ObjectConstructorInterface $fallbackConstructor) + { + $this->fallbackConstructor = $fallbackConstructor; + } + + /** + * {@inheritdoc} + */ + public function construct( + VisitorInterface $visitor, + ClassMetadata $metadata, + $data, + array $type, + DeserializationContext $context + ) { + if ($context->attributes->containsKey('target') && $context->getDepth() === 1) { + return $context->attributes->get('target')->get(); + } + + return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context); + } + +}