<?php
namespace App\Entity\Formation;
use App\Entity\Schoolyear;
use App\Repository\Formation\CategoryRepository;
use App\Service\SchoolyearService;
use App\Traits\Actions;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
/**
* Category
*/
#[ORM\Table(name: 'hs_tc_formation_category')]
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
class Category implements \Stringable
{
use Actions;
/**
* @var string
*/
#[ORM\Column(name: 'name', type: 'string', length: 255)]
private $name;
/**
* @var string
*/
#[ORM\Column(name: 'slug', type: 'string', length: 255)]
private $slug;
#[ORM\OneToMany(targetEntity: Activity::class, mappedBy: 'category', cascade: ['persist'], orphanRemoval: true)]
private ?Collection $activites = null;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->activites = new ArrayCollection();
$this->createAt=new \DateTime('now');
$this->published=true;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $name
* @return Category
*/
public function setName($name)
{
$this->name = $name;
$this->setSlug($this->slugify($name));
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* @param string $slug
* @return Category
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
function __toString(): string
{
return $this->name;
}
/**
* Add activite
*
*
* @return Category
*/
public function addActivity(Activity $activite)
{
$activite->setCategory($this);
$this->activites[] = $activite;
return $this;
}
/**
* Remove activite
*/
public function removeActivity(Activity $activite)
{
$this->activites->removeElement($activite);
}
/**
* Get activites
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getActivites()
{
return $this->activites;
}
public function getLatestFeaturedActivities(int $limit = 4): Collection
{
$featuredActivities = $this->activites->filter(fn(Activity $activity) => ($activity->isFeatured()));
// Sort featured activities by creation date (newest first)
$featuredActivities = $featuredActivities->toArray();
usort($featuredActivities, fn(Activity $a, Activity $b) => $b->getCreateAt() <=> $a->getCreateAt());
// Return a limited number of featured activities
return new ArrayCollection(array_slice($featuredActivities, 0, $limit));
}
public function slugify($title)
{
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', (string) $title);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^\-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
}