BlogPost.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Doctrine;
  18. use JMS\Serializer\Annotation\SerializedName;
  19. use JMS\Serializer\Annotation\XmlRoot;
  20. use JMS\Serializer\Annotation\XmlAttribute;
  21. use JMS\Serializer\Annotation\XmlList;
  22. use JMS\Serializer\Annotation\Groups;
  23. use JMS\Serializer\Annotation\Type;
  24. use Doctrine\Common\Collections\ArrayCollection;
  25. use Doctrine\ORM\Mapping as ORM;
  26. /**
  27. * @ORM\Entity
  28. * @XmlRoot("blog-post")
  29. */
  30. class BlogPost
  31. {
  32. /**
  33. * @ORM\Id @ORM\Column(type="integer")
  34. */
  35. protected $id;
  36. /**
  37. * @ORM\Column(type="string")
  38. * @Groups({"comments","post"})
  39. */
  40. private $title;
  41. /**
  42. * @ORM\Column(type="some_custom_type")
  43. */
  44. protected $slug;
  45. /**
  46. * @ORM\Column(type="datetime")
  47. * @XmlAttribute
  48. */
  49. private $createdAt;
  50. /**
  51. * @ORM\Column(type="boolean")
  52. * @Type("integer")
  53. * This boolean to integer conversion is one of the few changes between this
  54. * and the standard BlogPost class. It's used to test the override behavior
  55. * of the DoctrineTypeDriver so notice it, but please don't change it.
  56. *
  57. * @SerializedName("is_published")
  58. * @Groups({"post"})
  59. * @XmlAttribute
  60. */
  61. private $published;
  62. /**
  63. * @ORM\OneToMany(targetEntity="Comment", mappedBy="blogPost")
  64. * @XmlList(inline=true, entry="comment")
  65. * @Groups({"comments"})
  66. */
  67. private $comments;
  68. /**
  69. * @ORM\OneToOne(targetEntity="Author")
  70. * @Groups({"post"})
  71. */
  72. private $author;
  73. public function __construct($title, Author $author, \DateTime $createdAt)
  74. {
  75. $this->title = $title;
  76. $this->author = $author;
  77. $this->published = false;
  78. $this->comments = new ArrayCollection();
  79. $this->createdAt = $createdAt;
  80. }
  81. public function setPublished()
  82. {
  83. $this->published = true;
  84. }
  85. public function addComment(Comment $comment)
  86. {
  87. $this->comments->add($comment);
  88. }
  89. }