src/Entity/Recipe.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\RecipeRepository")
  8.  */
  9. class Recipe
  10. {
  11.     /**
  12.      * @ORM\Id()
  13.      * @ORM\GeneratedValue()
  14.      * @ORM\Column(type="integer")
  15.      */
  16.     private $id;
  17.     /**
  18.      * @ORM\Column(type="string", length=255)
  19.      */
  20.     private $title;
  21.     /**
  22.      * @ORM\Column(type="text")
  23.      */
  24.     private $resume;
  25.     /**
  26.      * @ORM\Column(type="text")
  27.      */
  28.     private $text;
  29.     /**
  30.      * @ORM\Column(type="string", length=3)
  31.      */
  32.     private $lang;
  33.     /**
  34.      * @ORM\Column(type="datetime")
  35.      */
  36.     private $creationDate;
  37.     /**
  38.      * @ORM\Column(type="datetime")
  39.      */
  40.     private $publicationDate;
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity="App\Entity\Image")
  43.      * @ORM\JoinColumn(onDelete="CASCADE")
  44.      */
  45.     private $image;
  46.     /**
  47.      * @ORM\Column(type="boolean")
  48.      */
  49.     private $active;
  50.     /**
  51.      * @ORM\Column(type="string", length=100, nullable=true)
  52.      */
  53.     private $metaTitle;
  54.     /**
  55.      * @ORM\Column(type="string", length=200, nullable=true)
  56.      */
  57.     private $metaDescription;
  58.     /**
  59.      * @ORM\Column(type="string", length=255, nullable=true)
  60.      */
  61.     private $author;
  62.     /**
  63.      * @ORM\ManyToMany(targetEntity="App\Entity\SuggestedGroup", mappedBy="recipes")
  64.      */
  65.     private $suggestedGroups;
  66.     public function __construct(){
  67.         $this->creationDate = new \DateTime();
  68.         $this->suggestedGroups = new ArrayCollection();
  69.     }
  70.     public function getId(): ?int
  71.     {
  72.         return $this->id;
  73.     }
  74.     public function getTitle(): ?string
  75.     {
  76.         return $this->title;
  77.     }
  78.     public function setTitle(string $title): self
  79.     {
  80.         $this->title $title;
  81.         return $this;
  82.     }
  83.     public function getResume(): ?string
  84.     {
  85.         return $this->resume;
  86.     }
  87.     public function setResume(string $resume): self
  88.     {
  89.         $this->resume$resume;
  90.         return $this;
  91.     }
  92.     public function getText(): ?string
  93.     {
  94.         return $this->text;
  95.     }
  96.     public function setText(string $text): self
  97.     {
  98.         $this->text $text;
  99.         return $this;
  100.     }
  101.     public function getLang(): ?string
  102.     {
  103.         return $this->lang;
  104.     }
  105.     public function setLang(string $lang): self
  106.     {
  107.         $this->lang $lang;
  108.         return $this;
  109.     }
  110.     public function getCreationDate(): ?\DateTimeInterface
  111.     {
  112.         return $this->creationDate;
  113.     }
  114.     public function setCreationDate(\DateTimeInterface $creationDate): self
  115.     {
  116.         $this->creationDate $creationDate;
  117.         return $this;
  118.     }
  119.     public function getPublicationDate(): ?\DateTimeInterface
  120.     {
  121.         return $this->publicationDate;
  122.     }
  123.     public function setPublicationDate(\DateTimeInterface $publicationDate): self
  124.     {
  125.         $this->publicationDate $publicationDate;
  126.         return $this;
  127.     }
  128.     public function getImage(): ?Image
  129.     {
  130.         return $this->image;
  131.     }
  132.     public function setImage(?Image $image): self
  133.     {
  134.         if(!empty($image))
  135.             $this->image $image;
  136.         return $this;
  137.     }
  138.     public function getDateSince(){
  139.         $time time() - $this->getPublicationDate()->getTimestamp(); // to get the time since that moment
  140.         $time = ($time<1)? $time;
  141.         $tokens = array (
  142.             31536000 => 'ans',
  143.             2592000 => 'mois',
  144.             604800 => 'semaine',
  145.             86400 => 'jour',
  146.             3600 => 'heure',
  147.             60 => 'minute',
  148.             => 'seconde'
  149.         );
  150.         foreach ($tokens as $unit => $text) {
  151.             if ($time $unit) continue;
  152.             $numberOfUnits floor($time $unit);
  153.             return $numberOfUnits.' '.$text.(($numberOfUnits>&& $text != 'ans' && $text != 'mois')?'s':'');
  154.         }
  155.     }
  156.     public function getUrlTitle(): ?string
  157.     {
  158.         $unwanted_array = array(    'Š'=>'S''š'=>'s''Ž'=>'Z''ž'=>'z''À'=>'A''Á'=>'A''Â'=>'A''Ã'=>'A''Ä'=>'A''Å'=>'A''Æ'=>'A''Ç'=>'C''È'=>'E''É'=>'E',
  159.             'Ê'=>'E''Ë'=>'E''Ì'=>'I''Í'=>'I''Î'=>'I''Ï'=>'I''Ñ'=>'N''Ò'=>'O''Ó'=>'O''Ô'=>'O''Õ'=>'O''Ö'=>'O''Ø'=>'O''Ù'=>'U',
  160.             'Ú'=>'U''Û'=>'U''Ü'=>'U''Ý'=>'Y''Þ'=>'B''ß'=>'Ss''à'=>'a''á'=>'a''â'=>'a''ã'=>'a''ä'=>'a''å'=>'a''æ'=>'a''ç'=>'c',
  161.             'è'=>'e''é'=>'e''ê'=>'e''ë'=>'e''ì'=>'i''í'=>'i''î'=>'i''ï'=>'i''ð'=>'o''ñ'=>'n''ò'=>'o''ó'=>'o''ô'=>'o''õ'=>'o',
  162.             'ö'=>'o''ø'=>'o''ù'=>'u''ú'=>'u''û'=>'u''ý'=>'y''þ'=>'b''ÿ'=>'y' );
  163.         $str strtr$this->title$unwanted_array );
  164.         return urlencode(str_replace(' ''-'strtolower($str)));
  165.     }
  166.     public function getActive(): ?bool
  167.     {
  168.         return $this->active;
  169.     }
  170.     public function setActive(bool $active): self
  171.     {
  172.         $this->active $active;
  173.         return $this;
  174.     }
  175.     public function getMetaTitle(): ?string
  176.     {
  177.         return $this->metaTitle;
  178.     }
  179.     public function setMetaTitle(string $metaTitle): self
  180.     {
  181.         $this->metaTitle $metaTitle;
  182.         return $this;
  183.     }
  184.     public function getMetaDescription(): ?string
  185.     {
  186.         return $this->metaDescription;
  187.     }
  188.     public function setMetaDescription(?string $metaDescription): self
  189.     {
  190.         $this->metaDescription $metaDescription;
  191.         return $this;
  192.     }
  193.     public function getAuthor(): ?string
  194.     {
  195.         //For legacy keeping the old ways
  196.         if(empty($this->author) && !empty($this->image)){
  197.             return $this->getImage()->getUser()->getDisplayName();
  198.         }
  199.         return $this->author;
  200.     }
  201.     public function setAuthor(?string $author): self
  202.     {
  203.         $this->author $author;
  204.         return $this;
  205.     }
  206.     public function __toString()
  207.     {
  208.         return $this->getId().'-'.$this->getTitle();
  209.     }
  210.     /**
  211.      * @return Collection|SuggestedGroup[]
  212.      */
  213.     public function getSuggestedGroups(): Collection
  214.     {
  215.         return $this->suggestedGroups;
  216.     }
  217.     public function addSuggestedGroup(SuggestedGroup $suggestedGroup): self
  218.     {
  219.         if (!$this->suggestedGroups->contains($suggestedGroup)) {
  220.             $this->suggestedGroups[] = $suggestedGroup;
  221.             $suggestedGroup->addRecipe($this);
  222.         }
  223.         return $this;
  224.     }
  225.     public function removeSuggestedGroup(SuggestedGroup $suggestedGroup): self
  226.     {
  227.         if ($this->suggestedGroups->contains($suggestedGroup)) {
  228.             $this->suggestedGroups->removeElement($suggestedGroup);
  229.             $suggestedGroup->removeRecipe($this);
  230.         }
  231.         return $this;
  232.     }
  233. }