<?php
namespace App\Entity\Training;
use _PHPStan_a3459023a\Nette\Utils\DateTime;
use ApiPlatform\Metadata\ApiResource;
use App\Traits\Actions;
use DateInterval;
use DatePeriod;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
#[ORM\Entity()]
#[ApiResource]
#[ORM\Table(name: 'hs_tc_training_scheduleevent')]
class Scheduleevent
{
use Actions;
#[ORM\ManyToOne(inversedBy: 'scheduleevents')]
#[ORM\JoinColumn(nullable: false)]
private ?Schedule $schedule = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $takesplaceOn = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255)]
private ?string $startAt = null;
#[ORM\Column(nullable: true)]
private ?string $length = null;
#[ORM\Column(nullable: true)]
private ?string $nature = null;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=true;
}
public function getTakesplaceOn(): ?\DateTimeInterface
{
return $this->takesplaceOn;
}
public function setTakesplaceOn(?\DateTimeInterface $takesplaceOn): void
{
$this->takesplaceOn = $takesplaceOn;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): void
{
$this->title = $title;
}
public function getSchedule(): ?Schedule
{
return $this->schedule;
}
public function setSchedule(?Schedule $schedule): self
{
$this->schedule = $schedule;
return $this;
}
public function getStartAt(): ?string
{
return $this->startAt;
}
public function setStartAt(?string $startAt): void
{
$this->startAt = $startAt;
}
public function getLength(): ?string
{
return $this->length;
}
public function setLength(?string $length): self
{
$this->length = $length;
return $this;
}
/**
* @throws \Exception
*/
function getLengthAsDateInterval()
{
// Regex pour capturer les heures et les minutes
$pattern = '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
if (preg_match($pattern, $this->getLength(), $matches)) {
$hours = isset($matches[1]) ? (int)$matches[1] : 0;
$minutes = isset($matches[2]) ? (int)$matches[2] : 0;
// Création de l'objet DateInterval
$intervalSpec = sprintf('PT%dH%dM', $hours, $minutes);
return new DateInterval($intervalSpec);
} else {
throw new InvalidArgumentException("Invalid time string format: $this->length");
}
}
function getLengthAsSeconds() {
return ($this->getLengthAsDateInterval()->y * 365 * 24 * 60 * 60) +
($this->getLengthAsDateInterval()->m * 30 * 24 * 60 * 60) +
($this->getLengthAsDateInterval()->d * 24 * 60 * 60) +
($this->getLengthAsDateInterval()->h * 60 * 60) +
($this->getLengthAsDateInterval()->i * 60) +
$this->getLengthAsDateInterval()->s;
}
/**
* @throws \Exception
*/
function getLengthAsStr()
{
return $this->getLengthAsDateInterval()->format('%hh%imin');
}
function getIntervalAsStr($interval)
{
return $interval->format('%h h %i min');
}
public function getNature(): ?string
{
return $this->nature;
}
public function setNature(?string $nature): void
{
$this->nature = $nature;
}
public function getStartAt100()
{
$dayAsSeconds = 11*60*60;
// $now= new \DateTime();
// $date1=DateTime::createFromFormat('d/m/Y H:i', $now->format('d/m/Y'.' 08:00'));
// $date2=DateTime::createFromFormat('d/m/Y H:i', $now->format('d/m/Y'.' '.$this->getStartAt()));
// return ($date2->diff($date1)->h*60*60+$date2->diff($date1)->i*60)/$dayAsSeconds*100;
$time=explode(':',$this->getStartAt());
$h=(intval($time[0])-8)*60*60;
$min=intval($time[1])*60;
return ($h+$min)/$dayAsSeconds*100;
//return $date1->format('H:i');
}
public function getLength100(): float
{
$dayAsSeconds = 11*60*60;
return $this->getLengthAsSeconds()/$dayAsSeconds*100;
}
public function getStartAtPx()
{
$infos=explode(":",$this->getStartAt());
return intval($infos[0])-8+($infos[1] == "30" ? 0.5 : ($infos[1] == "15" ? 0.25 : 0));
}
public function getLengthAtPx()
{
$pattern = '/^(?:(\d+)h)?(?:\s*(\d+)min)?$/';
preg_match($pattern, $this->getLength(), $matches);
$hours = isset($matches[1]) ? (int)$matches[1] : 0;
$minutes = isset($matches[2]) ? (int)$matches[2] : 0;
return $hours+$minutes/60;
}
public function getEndAt()
{
$startAt=\DateTime::createFromFormat('d/m/Y H:i',(new \DateTime('now'))->format('d/m/Y')." ".$this->getStartAt());
$endAt=$startAt->add($this->getLengthAsDateInterval());
return $endAt->format('H:i');
}
public function getDay()
{
return $this->takesplaceOn->format('N');
}
}