vendor/perfectneeds/content-multi-lang-bundle/Entity/Post.php line 10

Open in your IDE?
  1. <?php
  2. namespace PN\ContentBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5. * @ORM\MappedSuperclass
  6. */
  7. class Post {
  8. /**
  9. * @ORM\Column(name="content", type="json")
  10. */
  11. protected $content = [
  12. 'brief' => '',
  13. 'description' => '',
  14. ];
  15. /**
  16. * @ORM\ManyToMany(targetEntity="\PN\MediaBundle\Entity\Image", inversedBy="posts", cascade={"persist", "remove" })
  17. * @ORM\OrderBy({"id" = "ASC", "tarteb" = "ASC"})
  18. */
  19. protected $images;
  20. /**
  21. * Constructor
  22. */
  23. public function __construct() {
  24. $this->images = new \Doctrine\Common\Collections\ArrayCollection();
  25. $this->translations = new \Doctrine\Common\Collections\ArrayCollection();
  26. }
  27. /**
  28. * Get Main Image
  29. *
  30. * @return \PN\MediaBundle\Entity\Image
  31. */
  32. public function getMainImage() {
  33. return $this->getImages(array(\PN\MediaBundle\Entity\Image::TYPE_MAIN))->first();
  34. }
  35. /**
  36. * Get Image By Type
  37. *
  38. * @return \PN\MediaBundle\Entity\Image
  39. */
  40. public function getImageByType($type) {
  41. return $this->getImages(array($type))->first();
  42. }
  43. /**
  44. * Get images
  45. *
  46. * @return \Doctrine\Common\Collections\Collection
  47. */
  48. public function getImages($types = null) {
  49. if ($types) {
  50. return $this->images->filter(function(\PN\MediaBundle\Entity\Image $image) use ($types) {
  51. return in_array($image->getImageType(), $types);
  52. });
  53. } else {
  54. return $this->images;
  55. }
  56. }
  57. /**
  58. * Set content
  59. *
  60. * @param array $content
  61. *
  62. * @return Post
  63. */
  64. public function setContent($content) {
  65. $this->content = $content;
  66. return $this;
  67. }
  68. /**
  69. * Get content
  70. *
  71. * @return array
  72. */
  73. public function getContent() {
  74. return !$this->currentTranslation ? $this->content : $this->currentTranslation->getContent();
  75. }
  76. /**
  77. * Add image
  78. *
  79. * @param \PN\MediaBundle\Entity\Image $image
  80. *
  81. * @return Post
  82. */
  83. public function addImage(\PN\MediaBundle\Entity\Image $image) {
  84. $this->images[] = $image;
  85. return $this;
  86. }
  87. /**
  88. * Remove image
  89. *
  90. * @param \PN\MediaBundle\Entity\Image $image
  91. */
  92. public function removeImage(\PN\MediaBundle\Entity\Image $image) {
  93. $this->images->removeElement($image);
  94. }
  95. }