api-erstellen #1

Merged
TorstenHettstedt merged 52 commits from api-erstellen into master 2021-04-07 13:00:46 +02:00
4 changed files with 240 additions and 1 deletions
Showing only changes of commit e6b9180432 - Show all commits
+2 -1
View File
@@ -19,7 +19,8 @@
"slim/slim": "^4.7.1", "slim/slim": "^4.7.1",
"vlucas/phpdotenv": "^4.2", "vlucas/phpdotenv": "^4.2",
"slim/psr7": "^1.3", "slim/psr7": "^1.3",
"jetbrains/phpstorm-attributes": "^1.0.0" "jetbrains/phpstorm-attributes": "^1.0.0",
"myclabs/php-enum": "^1.8.0"
}, },
"require-dev": { "require-dev": {
"phpstan/phpstan": "^0.12.80", "phpstan/phpstan": "^0.12.80",
+23
View File
@@ -0,0 +1,23 @@
<?php /** @noinspection PhpUnusedPrivateFieldInspection */
namespace TorstenHettstedt\TimekeepingApi\Models;
use MyCLabs\Enum\Enum;
/**
* Enum-Klasse für die unterstützten Periode-Definitionen.
* Der Wert der Periode ist ein Format-String für {@link https://secure.php.net/manual/en/datetime.format.php}.
*
* @extends Enum<string>
*
* @method static PeriodDesignationEnum WEEKLY()
* @method static PeriodDesignationEnum MONTHLY()
* @method static PeriodDesignationEnum YEARLY()
*/
class PeriodDesignationEnum extends Enum
{
private const WEEKLY = 'Y#W';
private const MONTHLY = 'Y-m';
private const YEARLY = 'Y';
}
+102
View File
@@ -0,0 +1,102 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Models;
use DateInterval;
use DateTimeInterface;
use JetBrains\PhpStorm\ArrayShape;
class WorkingHoursView implements ModelInterface
{
protected DateTimeInterface $period;
protected PeriodDesignationEnum $periodDesignation;
protected int $workingDays;
protected DateInterval $totalHours;
protected DateInterval $overtime;
/**
* WorkingHoursView constructor.
*
* @param DateTimeInterface $period
* @param PeriodDesignationEnum $periodDesignation
* @param int $workingDays
* @param DateInterval $totalHours
* @param DateInterval $overtime
*/
public function __construct(
DateTimeInterface $period,
PeriodDesignationEnum $periodDesignation,
int $workingDays,
DateInterval $totalHours,
DateInterval $overtime
) {
$this->period = $period;
$this->periodDesignation = $periodDesignation;
$this->workingDays = $workingDays;
$this->totalHours = $totalHours;
$this->overtime = $overtime;
}
/**
* @return DateTimeInterface
*/
public function getPeriod(): DateTimeInterface
{
return $this->period;
}
/**
* @return PeriodDesignationEnum
*/
public function getPeriodDesignation(): PeriodDesignationEnum
{
return $this->periodDesignation;
}
/**
* @return int
*/
public function getWorkingDays(): int
{
return $this->workingDays;
}
/**
* @return DateInterval
*/
public function getTotalHours(): DateInterval
{
return $this->totalHours;
}
/**
* @return DateInterval
*/
public function getOvertime(): DateInterval
{
return $this->overtime;
}
/**
* @inheritDoc
*
* @return array<string, mixed>
*/
#[ArrayShape(['period' => "string",
'periodDesignation' => "string",
'totalHours' => "string",
'workingDays' => "int",
'overtime' => "string"
])] public function jsonSerialize(): array
{
return [
'period' => $this->getPeriod()->format($this->getPeriodDesignation()->getValue()),
'periodDesignation' => strtolower($this->getPeriodDesignation()->getKey()),
'workingDays' => $this->getWorkingDays(),
'totalHours' => $this->getTotalHours()->format('%H:%I:%S'),
'overtime' => $this->getOvertime()->format('%H:%I:%S')
];
}
}
@@ -0,0 +1,113 @@
<?php
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Models;
use Codeception\Test\Unit;
use DateInterval;
use DateTime;
use DateTimeInterface;
use TorstenHettstedt\TimekeepingApi\Models\PeriodDesignationEnum;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
class WorkingHoursViewTest extends Unit
{
/**
* @return array[]
*/
public function workingHoursProvider(): array
{
return [
[
new DateTime('2020-11-30'),
PeriodDesignationEnum::WEEKLY(),
15,
new DateInterval("PT32H22M"),
new DateInterval("PT22M"),
[
'period' => '2020#49',
'periodDesignation' => 'weekly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '00:22:00'
],
],
[
new DateTime('2020-11-30'),
PeriodDesignationEnum::MONTHLY(),
15,
new DateInterval("PT32H22M"),
new DateInterval("PT22M"),
[
'period' => '2020-11',
'periodDesignation' => 'monthly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '00:22:00'
],
],
[
new DateTime('2020-11-30'),
PeriodDesignationEnum::YEARLY(),
15,
new DateInterval("PT32H22M"),
new DateInterval("PT22M"),
[
'period' => '2020',
'periodDesignation' => 'yearly',
'workingDays' => 15,
'totalHours' => '32:22:00',
'overtime' => '00:22:00'
],
],
];
}
/**
* @param DateTimeInterface $period
* @param PeriodDesignationEnum $periodDesignation
* @param int $workingDays
* @param DateInterval $totalHours
* @param DateInterval $overtime
* @param array<string, string> $should_json
*
* @dataProvider workingHoursProvider
*/
public function testWorkingHoursViewWithContent(
DateTimeInterface $period,
PeriodDesignationEnum $periodDesignation,
int $workingDays,
DateInterval $totalHours,
DateInterval $overtime,
array $should_json
): void
{
$obj = new WorkingHoursView($period, $periodDesignation, $workingDays, $totalHours, $overtime);
$this->assertNotNull($obj->getPeriod());
$this->assertInstanceOf(DateTimeInterface::class, $obj->getPeriod());
$this->assertEquals($period, $obj->getPeriod());
$this->assertNotNull($obj->getPeriodDesignation());
$this->assertInstanceOf(PeriodDesignationEnum::class, $obj->getPeriodDesignation());
$this->assertEquals($periodDesignation, $obj->getPeriodDesignation());
$this->assertNotNull($obj->getWorkingDays());
$this->assertIsInt($obj->getWorkingDays());
$this->assertEquals($workingDays, $obj->getWorkingDays());
$this->assertNotNull($obj->getTotalHours());
$this->assertInstanceOf(DateInterval::class, $obj->getTotalHours());
$this->assertEquals($totalHours, $obj->getTotalHours());
$this->assertNotNull($obj->getOvertime());
$this->assertInstanceOf(DateInterval::class, $obj->getOvertime());
$this->assertEquals($overtime, $obj->getOvertime());
$json = $obj->jsonSerialize();
$this->assertIsArray($json);
$this->assertEquals($should_json, $json);
}
}