Forked from paulferrett/SideLoadJsonSerializationVisitor.php
Created
March 29, 2014 23:30
-
-
Save gbirke/9864801 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
/* | |
* Copyright 2014 Paul Ferrett <[email protected]> | |
* | |
* 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. | |
*/ | |
use JMS\Serializer\Context; | |
use JMS\Serializer\JsonSerializationVisitor; | |
use JMS\Serializer\Metadata\ClassMetadata; | |
class SideLoadJsonSerializationVisitor extends JsonSerializationVisitor { | |
private $sideData = array(); | |
private $sideDataIds = array(); | |
public function getRoot() { | |
$root = parent::getRoot(); | |
if (!is_array($root) || !$root) { | |
return $root; | |
} | |
return $root + $this->sideData; | |
} | |
public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) { | |
$rs = parent::endVisitingObject($metadata, $data, $type, $context); | |
if ($context->getDepth() === 0) { | |
return $rs; | |
} | |
return $this->addSideData($rs, $metadata, $data, $type, $context); | |
} | |
protected function addSideData($rs, ClassMetadata $metadata, $data, array $type, Context $context) { | |
$class = $this->getSideDataClass($metadata, $data, $type); | |
$id = $this->getSideDataId($metadata, $data); | |
if (!$class || !$id) { | |
return $rs; | |
} | |
if (!isset($this->sideDataIds[$class])) { | |
$this->sideDataIds[$class] = array(); | |
$this->sideData[$class] = array(); | |
} else if (in_array($id, $this->sideDataIds[$class])) { | |
return $id; | |
} | |
$this->sideDataIds[$class][] = $id; | |
$this->sideData[$class][] = $rs; | |
return $id; | |
} | |
protected function getSideDataClass(ClassMetadata $metadata, $data, array $type) { | |
$className = get_class($data); | |
if (strpos($className, '\\') !== false) { | |
$className = substr($className, strrpos($className, '\\') + 1); | |
} | |
return preg_replace(array('/(s|x)$/', '/y$/'), array('$1e', 'ie'), lcfirst($className)) . 's'; | |
} | |
protected function getSideDataId(ClassMetadata $metadata, $data) { | |
if (!method_exists($data, 'getId')) { | |
return null; | |
} | |
return $data->getId(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment