src/Entity/Formation/Category.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Formation;
  3. use App\Entity\Schoolyear;
  4. use App\Repository\Formation\CategoryRepository;
  5. use App\Service\SchoolyearService;
  6. use App\Traits\Actions;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Doctrine\Common\Collections\Collection;
  10. /**
  11.  * Category
  12.  */
  13. #[ORM\Table(name'hs_tc_formation_category')]
  14. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  15. class Category implements \Stringable
  16. {
  17.     use Actions;
  18.     /**
  19.      * @var string
  20.      */
  21.     #[ORM\Column(name'name'type'string'length255)]
  22.     private $name;
  23.     /**
  24.      * @var string
  25.      */
  26.     #[ORM\Column(name'slug'type'string'length255)]
  27.     private $slug;
  28.     #[ORM\OneToMany(targetEntityActivity::class, mappedBy'category'cascade: ['persist'], orphanRemovaltrue)]
  29.     private ?Collection $activites null;
  30.     public function __construct()
  31.     {
  32.         // date_default_timezone_set('Africa/Casablanca');
  33.         $this->activites = new ArrayCollection();
  34.         $this->createAt=new \DateTime('now');
  35.         $this->published=true;
  36.     }
  37.     /**
  38.      * Get id
  39.      *
  40.      * @return integer 
  41.      */
  42.     public function getId()
  43.     {
  44.         return $this->id;
  45.     }
  46.     /**
  47.      * Set nom
  48.      *
  49.      * @param string $name
  50.      * @return Category
  51.      */
  52.     public function setName($name)
  53.     {
  54.         $this->name $name;
  55.         $this->setSlug($this->slugify($name));
  56.         return $this;
  57.     }
  58.     /**
  59.      * Get name
  60.      *
  61.      * @return string 
  62.      */
  63.     public function getName()
  64.     {
  65.         return $this->name;
  66.     }
  67.     /**
  68.      * @return string
  69.      */
  70.     public function getSlug()
  71.     {
  72.         return $this->slug;
  73.     }
  74.     /**
  75.      * @param string $slug
  76.      * @return Category
  77.      */
  78.     public function setSlug($slug)
  79.     {
  80.         $this->slug $slug;
  81.         return $this;
  82.     }
  83.     function __toString(): string
  84.     {
  85.         return $this->name;
  86.     }
  87.     /**
  88.      * Add activite
  89.      *
  90.      *
  91.      * @return Category
  92.      */
  93.     public function addActivity(Activity $activite)
  94.     {
  95.         $activite->setCategory($this);
  96.         $this->activites[] = $activite;
  97.         return $this;
  98.     }
  99.     /**
  100.      * Remove activite
  101.      */
  102.     public function removeActivity(Activity $activite)
  103.     {
  104.         $this->activites->removeElement($activite);
  105.     }
  106.     /**
  107.      * Get activites
  108.      *
  109.      * @return \Doctrine\Common\Collections\Collection
  110.      */
  111.     public function getActivites()
  112.     {
  113.         return $this->activites;
  114.     }
  115.     public function getLatestFeaturedActivities(int $limit 4): Collection
  116.     {
  117.         $featuredActivities $this->activites->filter(fn(Activity $activity) => ($activity->isFeatured()));
  118.         // Sort featured activities by creation date (newest first)
  119.         $featuredActivities $featuredActivities->toArray();
  120.         usort($featuredActivities, fn(Activity $aActivity $b) => $b->getCreateAt() <=> $a->getCreateAt());
  121.         // Return a limited number of featured activities
  122.         return new ArrayCollection(array_slice($featuredActivities0$limit));
  123.     }
  124.     public function slugify($title)
  125.     {
  126.         // replace non letter or digits by -
  127.         $text preg_replace('~[^\pL\d]+~u''-', (string) $title);
  128.         // transliterate
  129.         $text iconv('utf-8''us-ascii//TRANSLIT'$text);
  130.         // remove unwanted characters
  131.         $text preg_replace('~[^\-\w]+~'''$text);
  132.         // trim
  133.         $text trim($text'-');
  134.         // remove duplicate -
  135.         $text preg_replace('~-+~''-'$text);
  136.         // lowercase
  137.         $text strtolower($text);
  138.         if (empty($text)) {
  139.             return 'n-a';
  140.         }
  141.         return $text;
  142.     }
  143. }