115 lines
3.2 KiB
PHP
115 lines
3.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace TorstenHettstedt\TimekeepingApi\Repositories;
|
|
|
|
use DateInterval;
|
|
use DateTimeInterface;
|
|
use Exception;
|
|
use PDO;
|
|
use PDOException;
|
|
use TorstenHettstedt\TimekeepingApi\Models\PeriodDesignationEnum;
|
|
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
|
|
|
|
/**
|
|
* @implements RepositoryReaderInterface<WorkingHoursView>
|
|
*/
|
|
abstract class AbstractWorkingHoursViewRepository implements RepositoryReaderInterface
|
|
{
|
|
|
|
protected const SQL_SELECT = '';
|
|
protected const SQL_WHERE = '';
|
|
|
|
protected PeriodDesignationEnum $periodDesignation;
|
|
|
|
/**
|
|
* WorkingHoursRepository constructor.
|
|
*
|
|
* @param PDO $database
|
|
*/
|
|
public function __construct(protected PDO $database)
|
|
{
|
|
$this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Gibt alle vorhandenen Einträge zurück
|
|
*
|
|
* @return WorkingHoursView[]
|
|
* @throws Exception
|
|
* @throws PDOException
|
|
*/
|
|
public function findAll(): array
|
|
{
|
|
$models = [];
|
|
$stmt = $this->database->prepare(static::SQL_SELECT);
|
|
$stmt->execute();
|
|
while ($row = $stmt->fetch()) {
|
|
$model = new WorkingHoursView(
|
|
$this->buildDateFromPeriod($row['period']),
|
|
$this->periodDesignation,
|
|
(int)$row['workingDays'],
|
|
$this->buildDateInterval($row['totalHours']),
|
|
$this->buildDateInterval($row['overtime'])
|
|
);
|
|
$models[] = $model;
|
|
}
|
|
return $models;
|
|
}
|
|
|
|
/**
|
|
* Gibt den Eintrag zurück, der durch die ID
|
|
*
|
|
* @param mixed $primary_key
|
|
*
|
|
* @return WorkingHoursView
|
|
* @throws RepositoryRecordNotFoundException
|
|
* @throws Exception
|
|
* @throws PDOException
|
|
*/
|
|
public function findByKey(mixed $primary_key): WorkingHoursView
|
|
{
|
|
$stmt = $this->database->prepare(static::SQL_SELECT . static::SQL_WHERE);
|
|
$stmt->bindParam(1, $primary_key);
|
|
$stmt->execute();
|
|
$row = $stmt->fetch();
|
|
if ($row === false) {
|
|
throw new RepositoryRecordNotFoundException();
|
|
}
|
|
return new WorkingHoursView(
|
|
$this->buildDateFromPeriod($row['period']),
|
|
$this->periodDesignation,
|
|
(int)$row['workingDays'],
|
|
$this->buildDateInterval($row['totalHours']),
|
|
$this->buildDateInterval($row['overtime'])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param string $time
|
|
*
|
|
* @return DateInterval
|
|
* @throws Exception
|
|
*/
|
|
protected function buildDateInterval(string $time): DateInterval
|
|
{
|
|
$time_array = explode(':', $time);
|
|
$hours = (int)$time_array[0];
|
|
$minutes = (int)$time_array[1];
|
|
$seconds = (int)$time_array[2];
|
|
$interval = new DateInterval(sprintf('PT%dH%dM%dS', abs($hours), abs($minutes), abs($seconds)));
|
|
if (($hours < 0 || ($minutes < 0) || ($seconds < 0))) {
|
|
$interval->invert = 1;
|
|
}
|
|
return $interval;
|
|
}
|
|
|
|
/**
|
|
* @param string $period
|
|
*
|
|
* @return DateTimeInterface
|
|
* @throws Exception
|
|
*/
|
|
abstract protected function buildDateFromPeriod(string $period): DateTimeInterface;
|
|
} |