BlogPost.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace JMS\Serializer\Tests\Fixtures;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use JMS\Serializer\Annotation\Groups;
  5. use JMS\Serializer\Annotation\SerializedName;
  6. use JMS\Serializer\Annotation\Type;
  7. use JMS\Serializer\Annotation\XmlAttribute;
  8. use JMS\Serializer\Annotation\XmlElement;
  9. use JMS\Serializer\Annotation\XmlList;
  10. use JMS\Serializer\Annotation\XmlMap;
  11. use JMS\Serializer\Annotation\XmlNamespace;
  12. use JMS\Serializer\Annotation\XmlRoot;
  13. use PhpCollection\Map;
  14. use PhpCollection\Sequence;
  15. /**
  16. * @XmlRoot("blog-post")
  17. * @XmlNamespace(uri="http://example.com/namespace")
  18. * @XmlNamespace(uri="http://schemas.google.com/g/2005", prefix="gd")
  19. * @XmlNamespace(uri="http://www.w3.org/2005/Atom", prefix="atom")
  20. * @XmlNamespace(uri="http://purl.org/dc/elements/1.1/", prefix="dc")
  21. */
  22. class BlogPost
  23. {
  24. /**
  25. * @Type("string")
  26. * @XmlElement(cdata=false)
  27. * @Groups({"comments","post"})
  28. */
  29. private $id = 'what_a_nice_id';
  30. /**
  31. * @Type("string")
  32. * @Groups({"comments","post"})
  33. * @XmlElement(namespace="http://purl.org/dc/elements/1.1/");
  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("bool")
  50. * @SerializedName("is_reviewed")
  51. * @XmlAttribute
  52. * @Groups({"post"})
  53. */
  54. private $reviewed;
  55. /**
  56. * @Type("string")
  57. * @XmlAttribute(namespace="http://schemas.google.com/g/2005")
  58. * @Groups({"post"})
  59. */
  60. private $etag;
  61. /**
  62. * @Type("ArrayCollection<JMS\Serializer\Tests\Fixtures\Comment>")
  63. * @XmlList(inline=true, entry="comment")
  64. * @Groups({"comments"})
  65. */
  66. private $comments;
  67. /**
  68. * @Type("PhpCollection\Sequence<JMS\Serializer\Tests\Fixtures\Comment>")
  69. * @XmlList(inline=true, entry="comment2")
  70. * @Groups({"comments"})
  71. */
  72. private $comments2;
  73. /**
  74. * @Type("PhpCollection\Map<string,string>")
  75. * @XmlMap(keyAttribute = "key")
  76. */
  77. private $metadata;
  78. /**
  79. * @Type("JMS\Serializer\Tests\Fixtures\Author")
  80. * @Groups({"post"})
  81. * @XmlElement(namespace="http://www.w3.org/2005/Atom")
  82. */
  83. private $author;
  84. /**
  85. * @Type("JMS\Serializer\Tests\Fixtures\Publisher")
  86. */
  87. private $publisher;
  88. /**
  89. * @Type("array<JMS\Serializer\Tests\Fixtures\Tag>")
  90. * @XmlList(inline=true, entry="tag", namespace="http://purl.org/dc/elements/1.1/");
  91. */
  92. private $tag;
  93. public function __construct($title, Author $author, \DateTime $createdAt, Publisher $publisher)
  94. {
  95. $this->title = $title;
  96. $this->author = $author;
  97. $this->publisher = $publisher;
  98. $this->published = false;
  99. $this->reviewed = false;
  100. $this->comments = new ArrayCollection();
  101. $this->comments2 = new Sequence();
  102. $this->metadata = new Map();
  103. $this->metadata->set('foo', 'bar');
  104. $this->createdAt = $createdAt;
  105. $this->etag = sha1($this->createdAt->format(\DateTime::ISO8601));
  106. }
  107. public function setPublished()
  108. {
  109. $this->published = true;
  110. }
  111. public function getMetadata()
  112. {
  113. return $this->metadata;
  114. }
  115. public function addComment(Comment $comment)
  116. {
  117. $this->comments->add($comment);
  118. $this->comments2->add($comment);
  119. }
  120. public function addTag(Tag $tag)
  121. {
  122. $this->tag[] = $tag;
  123. }
  124. }