PropertyMetadata.php 4.7 KB

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