api-erstellen #1
@@ -0,0 +1,123 @@
|
||||
<?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 PDO $database;
|
||||
protected PeriodDesignationEnum $periodDesignation;
|
||||
|
||||
/**
|
||||
* WorkingHoursRepository constructor.
|
||||
*
|
||||
* @param PDO $database
|
||||
*/
|
||||
public function __construct(PDO $database)
|
||||
{
|
||||
$this->database = $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) {
|
||||
$interval->invert = 1;
|
||||
}
|
||||
if ($minutes < 0) {
|
||||
$interval->invert = 1;
|
||||
}
|
||||
if ($seconds < 0) {
|
||||
$interval->invert = 1;
|
||||
}
|
||||
return $interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $period
|
||||
*
|
||||
* @return DateTimeInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
abstract protected function buildDateFromPeriod(string $period): DateTimeInterface;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Repositories;
|
||||
|
||||
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use PDO;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\PeriodDesignationEnum;
|
||||
|
||||
class WorkingHoursMonthlyViewRepository extends AbstractWorkingHoursViewRepository
|
||||
{
|
||||
|
||||
protected const SQL_SELECT = <<<SQL
|
||||
select
|
||||
"Monat" as "period",
|
||||
"Gesamtarbeitszeit" as "totalHours",
|
||||
"Arbeitstage" as "workingDays",
|
||||
"Überstunden" as "overtime"
|
||||
from "Arbeitszeiten - Monat"
|
||||
SQL;
|
||||
protected const SQL_WHERE = ' where "Monat" = ?';
|
||||
|
||||
protected PDO $database;
|
||||
protected PeriodDesignationEnum $periodDesignation;
|
||||
|
||||
public function __construct(PDO $database)
|
||||
{
|
||||
parent::__construct($database);
|
||||
$this->periodDesignation = PeriodDesignationEnum::MONTHLY();
|
||||
}
|
||||
|
||||
protected function buildDateFromPeriod(string $period): DateTimeInterface
|
||||
{
|
||||
return new DateTime($period . '-01');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Repositories;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use PDO;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\PeriodDesignationEnum;
|
||||
|
||||
class WorkingHoursWeeklyViewRepository extends AbstractWorkingHoursViewRepository
|
||||
{
|
||||
|
||||
protected const SQL_SELECT = <<<SQL
|
||||
select
|
||||
"Woche" as "period",
|
||||
"Gesamtarbeitszeit" as "totalHours",
|
||||
"Arbeitstage" as "workingDays",
|
||||
"Überstunden" as "overtime"
|
||||
from "Arbeitszeiten - Woche"
|
||||
SQL;
|
||||
protected const SQL_WHERE = ' where "Woche" = ?';
|
||||
|
||||
protected PDO $database;
|
||||
protected PeriodDesignationEnum $periodDesignation;
|
||||
|
||||
public function __construct(PDO $database)
|
||||
{
|
||||
parent::__construct($database);
|
||||
$this->periodDesignation = PeriodDesignationEnum::WEEKLY();
|
||||
}
|
||||
|
||||
protected function buildDateFromPeriod(string $period): DateTimeInterface
|
||||
{
|
||||
$time_array = explode('#', $period);
|
||||
$year = (int)$time_array[0];
|
||||
$week = (int)$time_array[1];
|
||||
$date = new DateTime($year. '-01-01');
|
||||
$date->add(new DateInterval(sprintf('P%dW', $week)));
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Repositories;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\PeriodDesignationEnum;
|
||||
|
||||
class WorkingHoursYearlyViewRepository extends AbstractWorkingHoursViewRepository
|
||||
{
|
||||
|
||||
protected const SQL_SELECT = <<<SQL
|
||||
select
|
||||
"Jahr" as "period",
|
||||
"Gesamtarbeitszeit" as "totalHours",
|
||||
"Arbeitstage" as "workingDays",
|
||||
"Überstunden" as "overtime"
|
||||
from "Arbeitszeiten - Jahr"
|
||||
SQL;
|
||||
protected const SQL_WHERE = ' where "Jahr" = ?';
|
||||
|
||||
protected PDO $database;
|
||||
protected PeriodDesignationEnum $periodDesignation;
|
||||
|
||||
public function __construct(PDO $database)
|
||||
{
|
||||
parent::__construct($database);
|
||||
$this->periodDesignation = PeriodDesignationEnum::YEARLY();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $period
|
||||
*
|
||||
* @return DateTimeInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function buildDateFromPeriod(string $period): DateTimeInterface
|
||||
{
|
||||
return new DateTime($period . '-01-01');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user