PropertyMetadata.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Metadata\PropertyMetadata as BasePropertyMetadata;
  19. class PropertyMetadata extends BasePropertyMetadata
  20. {
  21. const ACCESS_TYPE_PROPERTY = 'property';
  22. const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
  23. public $sinceVersion;
  24. public $untilVersion;
  25. public $serializedName;
  26. public $type;
  27. public $xmlCollection = false;
  28. public $xmlCollectionInline = false;
  29. public $xmlEntryName;
  30. public $xmlKeyAttribute;
  31. public $xmlAttribute = false;
  32. public $xmlValue = false;
  33. public $getter;
  34. public $setter;
  35. public function setAccessor($type, $getter = null, $setter = null)
  36. {
  37. if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
  38. $class = $this->reflection->getDeclaringClass();
  39. if (empty($getter)) {
  40. if ($class->hasMethod('get'.$this->name) && $class->getMethod('get'.$this->name)->isPublic()) {
  41. $getter = 'get'.$this->name;
  42. } else if ($class->hasMethod('is'.$this->name) && $class->getMethod('is'.$this->name)->isPublic()) {
  43. $getter = 'is'.$this->name;
  44. } else {
  45. throw new \RuntimeException(sprintf('There is neither a public %s method, nor a public %s method in class %s. Please specify which public method should be used for retrieving the value of the property %s.', 'get'.ucfirst($this->name), 'is'.ucfirst($this->name), $this->class, $this->name));
  46. }
  47. }
  48. if (empty($setter)) {
  49. if ($class->hasMethod('set'.$this->name) && $class->getMethod('set'.$this->name)->isPublic()) {
  50. $setter = 'set'.$this->name;
  51. } else {
  52. throw new \RuntimeException(sprintf('There is no public %s method in class %s. Please specify which public method should be used for setting the value of the property %s.', 'set'.ucfirst($this->name), $this->class, $this->name));
  53. }
  54. }
  55. }
  56. $this->getter = $getter;
  57. $this->setter = $setter;
  58. }
  59. public function serialize()
  60. {
  61. return serialize(array(
  62. $this->sinceVersion,
  63. $this->untilVersion,
  64. $this->serializedName,
  65. $this->type,
  66. $this->xmlCollection,
  67. $this->xmlCollectionInline,
  68. $this->xmlEntryName,
  69. $this->xmlKeyAttribute,
  70. $this->xmlAttribute,
  71. $this->xmlValue,
  72. $this->getter,
  73. $this->setter,
  74. parent::serialize(),
  75. ));
  76. }
  77. public function unserialize($str)
  78. {
  79. list(
  80. $this->sinceVersion,
  81. $this->untilVersion,
  82. $this->serializedName,
  83. $this->type,
  84. $this->xmlCollection,
  85. $this->xmlCollectionInline,
  86. $this->xmlEntryName,
  87. $this->xmlKeyAttribute,
  88. $this->xmlAttribute,
  89. $this->xmlValue,
  90. $this->getter,
  91. $this->setter,
  92. $parentStr
  93. ) = unserialize($str);
  94. parent::unserialize($parentStr);
  95. }
  96. }