atol-online/src/Entities/CorrectionInfo.php

155 lines
4.4 KiB
PHP
Raw Normal View History

2021-12-03 12:09:14 +00:00
<?php
2021-12-03 12:09:14 +00:00
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types=1);
2021-12-03 12:09:14 +00:00
namespace AtolOnline\Entities;
use AtolOnline\Constraints;
use AtolOnline\Enums\CorrectionType;
2021-12-03 12:09:14 +00:00
use AtolOnline\Exceptions\{
EmptyCorrectionNumberException,
InvalidCorrectionDateException,};
2021-12-03 12:09:14 +00:00
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
2021-12-03 12:09:14 +00:00
use Exception;
use JetBrains\PhpStorm\{
ArrayShape};
2021-12-03 12:09:14 +00:00
/**
* Класс, описывающий данные коррекции
*
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 35
*/
final class CorrectionInfo extends Entity
2021-12-03 12:09:14 +00:00
{
/**
* @var DateTimeImmutable Дата документа основания для коррекции (1178)
2021-12-03 12:09:14 +00:00
*/
protected DateTimeImmutable $date;
2021-12-03 12:09:14 +00:00
/**
* Конструктор
*
* @param CorrectionType $type Тип коррекции (1173)
* @param DateTimeInterface|string $date Дата документа основания для коррекции (1178)
* @param string $number Номер документа основания для коррекции (1179)
2021-12-03 12:09:14 +00:00
* @throws InvalidCorrectionDateException
* @throws EmptyCorrectionNumberException
*/
public function __construct(
protected CorrectionType $type,
DateTimeInterface | string $date,
protected string $number,
) {
$this->setType($type)
->setDate($date)
->setNumber($number);
2021-12-03 12:09:14 +00:00
}
/**
* Возвращает тип коррекции
*
* @return CorrectionType|null
2021-12-03 12:09:14 +00:00
*/
public function getType(): ?CorrectionType
2021-12-03 12:09:14 +00:00
{
return $this->type;
}
/**
* Устанавливает тип коррекции
*
* @param CorrectionType $type
2021-12-03 12:09:14 +00:00
* @return $this
*/
public function setType(CorrectionType $type): self
2021-12-03 12:09:14 +00:00
{
$this->type = $type;
2021-12-03 12:09:14 +00:00
return $this;
}
/**
* Возвращает дату документа основания для коррекции
*
* @return DateTimeImmutable
2021-12-03 12:09:14 +00:00
*/
public function getDate(): DateTimeImmutable
2021-12-03 12:09:14 +00:00
{
return $this->date;
}
/**
* Устанавливает дату документа основания для коррекции
*
* @param DateTimeInterface|string $date Строковая дата в формате d.m.Y либо объект DateTime с датой
2021-12-03 12:09:14 +00:00
* @return $this
* @throws InvalidCorrectionDateException
*/
public function setDate(DateTimeInterface | string $date): self
2021-12-03 12:09:14 +00:00
{
try {
if (is_string($date)) {
$this->date = new DateTimeImmutable(trim($date));
} elseif ($date instanceof DateTime) {
$this->date = DateTimeImmutable::createFromMutable($date);
} elseif ($date instanceof DateTimeImmutable) {
$this->date = $date;
2021-12-03 12:09:14 +00:00
}
} catch (Exception $e) {
throw new InvalidCorrectionDateException($date, $e->getMessage());
}
return $this;
}
/**
* Возвращает установленный номер документа основания для коррекции
*
* @return string|null
*/
public function getNumber(): ?string
{
return $this->number;
}
/**
* Устанавливает номер документа основания для коррекции
*
* @param string $number
* @return $this
* @throws EmptyCorrectionNumberException
*/
public function setNumber(string $number): self
{
$number = trim($number);
empty($number) && throw new EmptyCorrectionNumberException();
$this->number = $number;
return $this;
}
/**
* @inheritDoc
*/
#[ArrayShape([
'type' => 'string',
'base_date' => 'string',
'base_number' => 'string',
])]
2021-12-03 12:09:14 +00:00
public function jsonSerialize(): array
{
return [
'type' => $this->getType(),
'base_date' => $this->getDate()->format(Constraints::CORRECTION_DATE_FORMAT),
2021-12-03 12:09:14 +00:00
'base_number' => $this->getNumber(),
];
}
}