BlogPost.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Tests\Fixtures;
  18. use JMS\Serializer\Annotation\Type;
  19. use JMS\Serializer\Annotation\SerializedName;
  20. use JMS\Serializer\Annotation\XmlMap;
  21. use JMS\Serializer\Annotation\XmlRoot;
  22. use JMS\Serializer\Annotation\XmlAttribute;
  23. use JMS\Serializer\Annotation\XmlList;
  24. use JMS\Serializer\Annotation\Groups;
  25. use Doctrine\Common\Collections\ArrayCollection;
  26. use PhpCollection\Map;
  27. use PhpCollection\Sequence;
  28. /** @XmlRoot("blog-post") */
  29. class BlogPost
  30. {
  31. /**
  32. * @Type("string")
  33. * @Groups({"comments","post"})
  34. */
  35. private $title;
  36. /**
  37. * @Type("DateTime")
  38. * @XmlAttribute
  39. */
  40. private $createdAt;
  41. /**
  42. * @Type("boolean")
  43. * @SerializedName("is_published")
  44. * @XmlAttribute
  45. * @Groups({"post"})
  46. */
  47. private $published;
  48. /**
  49. * @Type("ArrayCollection<JMS\Serializer\Tests\Fixtures\Comment>")
  50. * @XmlList(inline=true, entry="comment")
  51. * @Groups({"comments"})
  52. */
  53. private $comments;
  54. /**
  55. * @Type("PhpCollection\Sequence<JMS\Serializer\Tests\Fixtures\Comment>")
  56. * @XmlList(inline=true, entry="comment2")
  57. * @Groups({"comments"})
  58. */
  59. private $comments2;
  60. /**
  61. * @Type("PhpCollection\Map<string,string>")
  62. * @XmlMap(keyAttribute = "key")
  63. */
  64. private $metadata;
  65. /**
  66. * @Type("JMS\Serializer\Tests\Fixtures\Author")
  67. * @Groups({"post"})
  68. */
  69. private $author;
  70. public function __construct($title, Author $author, \DateTime $createdAt)
  71. {
  72. $this->title = $title;
  73. $this->author = $author;
  74. $this->published = false;
  75. $this->comments = new ArrayCollection();
  76. $this->comments2 = new Sequence();
  77. $this->metadata = new Map();
  78. $this->metadata->set('foo', 'bar');
  79. $this->createdAt = $createdAt;
  80. }
  81. public function setPublished()
  82. {
  83. $this->published = true;
  84. }
  85. public function getMetadata()
  86. {
  87. return $this->metadata;
  88. }
  89. public function addComment(Comment $comment)
  90. {
  91. $this->comments->add($comment);
  92. $this->comments2->add($comment);
  93. }
  94. }