PropertyMetadata.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Serializer\TypeParser;
  19. use Metadata\PropertyMetadata as BasePropertyMetadata;
  20. class PropertyMetadata extends BasePropertyMetadata
  21. {
  22. const ACCESS_TYPE_PROPERTY = 'property';
  23. const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
  24. public $sinceVersion;
  25. public $untilVersion;
  26. public $groups;
  27. public $serializedName;
  28. public $type;
  29. public $xmlCollection = false;
  30. public $xmlCollectionInline = false;
  31. public $xmlEntryName;
  32. public $xmlKeyAttribute;
  33. public $xmlAttribute = false;
  34. public $xmlValue = false;
  35. public $xmlKeyValuePairs = false;
  36. public $getter;
  37. public $setter;
  38. public $inline = false;
  39. public $readOnly = false;
  40. public $xmlAttributeMap = false;
  41. private static $typeParser;
  42. public function setAccessor($type, $getter = null, $setter = null)
  43. {
  44. if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
  45. $class = $this->reflection->getDeclaringClass();
  46. if (empty($getter)) {
  47. if ($class->hasMethod('get'.$this->name) && $class->getMethod('get'.$this->name)->isPublic()) {
  48. $getter = 'get'.$this->name;
  49. } else if ($class->hasMethod('is'.$this->name) && $class->getMethod('is'.$this->name)->isPublic()) {
  50. $getter = 'is'.$this->name;
  51. } else {
  52. 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));
  53. }
  54. }
  55. if (empty($setter) && !$this->readOnly) {
  56. if ($class->hasMethod('set'.$this->name) && $class->getMethod('set'.$this->name)->isPublic()) {
  57. $setter = 'set'.$this->name;
  58. } else {
  59. 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));
  60. }
  61. }
  62. }
  63. $this->getter = $getter;
  64. $this->setter = $setter;
  65. }
  66. public function setType($type)
  67. {
  68. if (null === self::$typeParser) {
  69. self::$typeParser = new TypeParser();
  70. }
  71. $this->type = self::$typeParser->parse($type);
  72. }
  73. public function serialize()
  74. {
  75. return serialize(array(
  76. $this->sinceVersion,
  77. $this->untilVersion,
  78. $this->groups,
  79. $this->serializedName,
  80. $this->type,
  81. $this->xmlCollection,
  82. $this->xmlCollectionInline,
  83. $this->xmlEntryName,
  84. $this->xmlKeyAttribute,
  85. $this->xmlAttribute,
  86. $this->xmlValue,
  87. $this->xmlKeyValuePairs,
  88. $this->getter,
  89. $this->setter,
  90. $this->inline,
  91. $this->readOnly,
  92. $this->xmlAttributeMap,
  93. parent::serialize(),
  94. ));
  95. }
  96. public function unserialize($str)
  97. {
  98. list(
  99. $this->sinceVersion,
  100. $this->untilVersion,
  101. $this->groups,
  102. $this->serializedName,
  103. $this->type,
  104. $this->xmlCollection,
  105. $this->xmlCollectionInline,
  106. $this->xmlEntryName,
  107. $this->xmlKeyAttribute,
  108. $this->xmlAttribute,
  109. $this->xmlValue,
  110. $this->xmlKeyValuePairs,
  111. $this->getter,
  112. $this->setter,
  113. $this->inline,
  114. $this->readOnly,
  115. $this->xmlAttributeMap,
  116. $parentStr
  117. ) = unserialize($str);
  118. parent::unserialize($parentStr);
  119. }
  120. }