VirtualPropertyMetadata.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. class VirtualPropertyMetadata extends PropertyMetadata
  19. {
  20. public function __construct($class, $methodName)
  21. {
  22. if ('get' === substr($methodName, 0, 3)) {
  23. $fieldName = lcfirst(substr($methodName, 3));
  24. } else {
  25. $fieldName = $methodName;
  26. }
  27. $this->class = $class;
  28. $this->name = $fieldName;
  29. $this->getter = $methodName;
  30. $this->readOnly = true;
  31. }
  32. public function getValue($obj)
  33. {
  34. return $obj->{$this->getter}();
  35. }
  36. public function setValue($obj, $value)
  37. {
  38. return false;
  39. }
  40. public function setAccessor($type, $getter = null, $setter = null)
  41. {
  42. return false;
  43. }
  44. public function serialize()
  45. {
  46. return serialize(array(
  47. $this->sinceVersion,
  48. $this->untilVersion,
  49. $this->groups,
  50. $this->serializedName,
  51. $this->type,
  52. $this->xmlCollection,
  53. $this->xmlCollectionInline,
  54. $this->xmlEntryName,
  55. $this->xmlKeyAttribute,
  56. $this->xmlAttribute,
  57. $this->xmlValue,
  58. $this->xmlKeyValuePairs,
  59. $this->getter,
  60. $this->setter,
  61. $this->inline,
  62. $this->readOnly,
  63. $this->class,
  64. $this->name
  65. ));
  66. }
  67. public function unserialize($str)
  68. {
  69. list(
  70. $this->sinceVersion,
  71. $this->untilVersion,
  72. $this->groups,
  73. $this->serializedName,
  74. $this->type,
  75. $this->xmlCollection,
  76. $this->xmlCollectionInline,
  77. $this->xmlEntryName,
  78. $this->xmlKeyAttribute,
  79. $this->xmlAttribute,
  80. $this->xmlValue,
  81. $this->xmlKeyValuePairs,
  82. $this->getter,
  83. $this->setter,
  84. $this->inline,
  85. $this->readOnly,
  86. $this->class,
  87. $this->name
  88. ) = unserialize($str);
  89. }
  90. }