PropertyMetadata.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 $groups;
  26. public $serializedName;
  27. public $type;
  28. public $xmlCollection = false;
  29. public $xmlCollectionInline = false;
  30. public $xmlEntryName;
  31. public $xmlKeyAttribute;
  32. public $xmlAttribute = false;
  33. public $xmlValue = false;
  34. public $xmlKeyValuePairs = false;
  35. public $getter;
  36. public $setter;
  37. public $inline = false;
  38. public $readOnly = false;
  39. public function setAccessor($type, $getter = null, $setter = null)
  40. {
  41. if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
  42. $class = $this->reflection->getDeclaringClass();
  43. if (empty($getter)) {
  44. if ($class->hasMethod('get'.$this->name) && $class->getMethod('get'.$this->name)->isPublic()) {
  45. $getter = 'get'.$this->name;
  46. } else if ($class->hasMethod('is'.$this->name) && $class->getMethod('is'.$this->name)->isPublic()) {
  47. $getter = 'is'.$this->name;
  48. } else {
  49. 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));
  50. }
  51. }
  52. if (empty($setter) && !$this->readOnly) {
  53. if ($class->hasMethod('set'.$this->name) && $class->getMethod('set'.$this->name)->isPublic()) {
  54. $setter = 'set'.$this->name;
  55. } else {
  56. 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));
  57. }
  58. }
  59. }
  60. $this->getter = $getter;
  61. $this->setter = $setter;
  62. }
  63. public function serialize()
  64. {
  65. return serialize(array(
  66. $this->sinceVersion,
  67. $this->untilVersion,
  68. $this->groups,
  69. $this->serializedName,
  70. $this->type,
  71. $this->xmlCollection,
  72. $this->xmlCollectionInline,
  73. $this->xmlEntryName,
  74. $this->xmlKeyAttribute,
  75. $this->xmlAttribute,
  76. $this->xmlValue,
  77. $this->xmlKeyValuePairs,
  78. $this->getter,
  79. $this->setter,
  80. $this->inline,
  81. $this->readOnly,
  82. parent::serialize(),
  83. ));
  84. }
  85. public function unserialize($str)
  86. {
  87. list(
  88. $this->sinceVersion,
  89. $this->untilVersion,
  90. $this->groups,
  91. $this->serializedName,
  92. $this->type,
  93. $this->xmlCollection,
  94. $this->xmlCollectionInline,
  95. $this->xmlEntryName,
  96. $this->xmlKeyAttribute,
  97. $this->xmlAttribute,
  98. $this->xmlValue,
  99. $this->xmlKeyValuePairs,
  100. $this->getter,
  101. $this->setter,
  102. $this->inline,
  103. $this->readOnly,
  104. $parentStr
  105. ) = unserialize($str);
  106. parent::unserialize($parentStr);
  107. }
  108. }