ClassMetadata.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Metadata;
  18. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  19. use Metadata\MergeableInterface;
  20. use Metadata\MethodMetadata;
  21. use Metadata\MergeableClassMetadata;
  22. class ClassMetadata extends MergeableClassMetadata
  23. {
  24. public $preSerializeMethods = array();
  25. public $postSerializeMethods = array();
  26. public $postDeserializeMethods = array();
  27. public $xmlRootName;
  28. public function addPreSerializeMethod(MethodMetadata $method)
  29. {
  30. $this->preSerializeMethods[] = $method;
  31. }
  32. public function addPostSerializeMethod(MethodMetadata $method)
  33. {
  34. $this->postSerializeMethods[] = $method;
  35. }
  36. public function addPostDeserializeMethod(MethodMetadata $method)
  37. {
  38. $this->postDeserializeMethods[] = $method;
  39. }
  40. public function merge(MergeableInterface $object)
  41. {
  42. if (!$object instanceof ClassMetadata) {
  43. throw new InvalidArgumentException('$object must be an instance of ClassMetadata.');
  44. }
  45. parent::merge($object);
  46. $this->preSerializeMethods = array_merge($this->preSerializeMethods, $object->preSerializeMethods);
  47. $this->postSerializeMethods = array_merge($this->postSerializeMethods, $object->postSerializeMethods);
  48. $this->postDeserializeMethods = array_merge($this->postDeserializeMethods, $object->postDeserializeMethods);
  49. $this->xmlRootName = $object->xmlRootName;
  50. }
  51. public function serialize()
  52. {
  53. return serialize(array(
  54. $this->preSerializeMethods,
  55. $this->postSerializeMethods,
  56. $this->postDeserializeMethods,
  57. $this->xmlRootName,
  58. parent::serialize(),
  59. ));
  60. }
  61. public function unserialize($str)
  62. {
  63. list(
  64. $this->preSerializeMethods,
  65. $this->postSerializeMethods,
  66. $this->postDeserializeMethods,
  67. $this->xmlRootName,
  68. $parentStr
  69. ) = unserialize($str);
  70. parent::unserialize($parentStr);
  71. }
  72. }