BlogPost.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\XmlNamespace;
  23. use JMS\Serializer\Annotation\XmlAttribute;
  24. use JMS\Serializer\Annotation\XmlList;
  25. use JMS\Serializer\Annotation\XmlElement;
  26. use JMS\Serializer\Annotation\Groups;
  27. use Doctrine\Common\Collections\ArrayCollection;
  28. use PhpCollection\Map;
  29. use PhpCollection\Sequence;
  30. /**
  31. * @XmlRoot("blog-post")
  32. * @XmlNamespace(uri="http://example.com/namespace")
  33. * @XmlNamespace(uri="http://schemas.google.com/g/2005", prefix="gd")
  34. * @XmlNamespace(uri="http://www.w3.org/2005/Atom", prefix="atom")
  35. * @XmlNamespace(uri="http://purl.org/dc/elements/1.1/", prefix="dc")
  36. */
  37. class BlogPost
  38. {
  39. /**
  40. * @Type("string")
  41. * @XmlElement(cdata=false)
  42. * @Groups({"comments","post"})
  43. */
  44. private $id = 'what_a_nice_id';
  45. /**
  46. * @Type("string")
  47. * @Groups({"comments","post"})
  48. * @XmlElement(namespace="http://purl.org/dc/elements/1.1/");
  49. */
  50. private $title;
  51. /**
  52. * @Type("DateTime")
  53. * @XmlAttribute
  54. */
  55. private $createdAt;
  56. /**
  57. * @Type("boolean")
  58. * @SerializedName("is_published")
  59. * @XmlAttribute
  60. * @Groups({"post"})
  61. */
  62. private $published;
  63. /**
  64. * @Type("string")
  65. * @XmlAttribute(namespace="http://schemas.google.com/g/2005")
  66. * @Groups({"post"})
  67. */
  68. private $etag;
  69. /**
  70. * @Type("ArrayCollection<JMS\Serializer\Tests\Fixtures\Comment>")
  71. * @XmlList(inline=true, entry="comment")
  72. * @Groups({"comments"})
  73. */
  74. private $comments;
  75. /**
  76. * @Type("PhpCollection\Sequence<JMS\Serializer\Tests\Fixtures\Comment>")
  77. * @XmlList(inline=true, entry="comment2")
  78. * @Groups({"comments"})
  79. */
  80. private $comments2;
  81. /**
  82. * @Type("PhpCollection\Map<string,string>")
  83. * @XmlMap(keyAttribute = "key")
  84. */
  85. private $metadata;
  86. /**
  87. * @Type("JMS\Serializer\Tests\Fixtures\Author")
  88. * @Groups({"post"})
  89. * @XmlElement(namespace="http://www.w3.org/2005/Atom")
  90. */
  91. private $author;
  92. /**
  93. * @Type("JMS\Serializer\Tests\Fixtures\Publisher")
  94. */
  95. private $publisher;
  96. public function __construct($title, Author $author, \DateTime $createdAt, Publisher $publisher)
  97. {
  98. $this->title = $title;
  99. $this->author = $author;
  100. $this->publisher = $publisher;
  101. $this->published = false;
  102. $this->comments = new ArrayCollection();
  103. $this->comments2 = new Sequence();
  104. $this->metadata = new Map();
  105. $this->metadata->set('foo', 'bar');
  106. $this->createdAt = $createdAt;
  107. $this->etag = sha1($this->createdAt->format(\DateTime::ISO8601));
  108. }
  109. public function setPublished()
  110. {
  111. $this->published = true;
  112. }
  113. public function getMetadata()
  114. {
  115. return $this->metadata;
  116. }
  117. public function addComment(Comment $comment)
  118. {
  119. $this->comments->add($comment);
  120. $this->comments2->add($comment);
  121. }
  122. }