Skip to content

Commit

Permalink
Replace DateTime with DateTimeInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-netFantom committed Jul 27, 2023
1 parent 435ca4e commit ddffa61
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ $paymentParametersArray = $robokassa->getPaymentParameters(new InvoiceOptions(
],
sno: Sno::osn
),
expirationDate: (new DateTime('2030-01-01 10:20:30', new DateTimeZone('+3'))),
expirationDate: (new DateTimeImmutable())->add(new DateInterval('PT48H')),
email: 'user@email.com',
outSumCurrency: OutSumCurrency::USD,
userIP: '127.0.0.1',
Expand Down
13 changes: 7 additions & 6 deletions src/Options/InvoiceOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace netFantom\RobokassaApi\Options;

use DateTime;
use DateTimeInterface;
use netFantom\RobokassaApi\Params\Option\{Culture, OutSumCurrency, Receipt};

class InvoiceOptions
Expand Down Expand Up @@ -32,17 +32,18 @@ class InvoiceOptions
* ROBOKASSA и в Электронной квитанции, которую мы выдаём клиенту после успешного платежа.
* Корректность отображения зависит от необязательного параметра {@see self::$encoding}
* @param Receipt|string|null $receipt Данные для фискализации
* @param DateTime|string|null $expirationDate <pre>
* @param DateTimeInterface|string|null $expirationDate <pre>
* Срок действия счета. Этот параметр необходим, чтобы запретить пользователю
* оплату позже указанной магазином даты при выставлении счета.
*
* <b>Дата и время передаются в формате по стандарту ISO 8601:</b>
* <i>YYYY-MM-DDThh:mm:ss.fffffff;ZZZZZ</i>
* * (РЕКОМЕНДУЮ передавать <b>new DateTime(...)</b> для автоматического форматирования)
* * (РЕКОМЕНДУЮ передавать <b>new DateTimeImmutable(...)</b> для автоматического форматирования)
*
* <b>Например:</b>
* <i>2010-02-11T16:07:11.6973153+03:00</i>
* * (ИЛИ new <b>DateTime('2010-02-11 16:07:11')</b> для автоматического форматирования)
* * (ИЛИ <b>new DateTimeImmutable('2010-02-11 16:07:11', new DateTimeZone('+3'))</b> для автоматического форматирования)
* * (ИЛИ <b>(new DateTimeImmutable())->add(new DateInterval('PT48H'))</b> чтобы задать время жизни счета 48 часов)
*
* <i>Формат содержит параметры:
* YYYY — Год, 4 цифры
Expand Down Expand Up @@ -114,7 +115,7 @@ public function __construct(
public readonly int|null $invId,
public readonly string $description,
public readonly Receipt|string|null $receipt = null,
DateTime|string|null $expirationDate = null,
DateTimeInterface|string|null $expirationDate = null,
public readonly ?string $email = null,
public readonly ?OutSumCurrency $outSumCurrency = null,
public readonly ?string $userIP = null,
Expand All @@ -124,7 +125,7 @@ public function __construct(
public readonly ?Culture $culture = null,
public readonly ?string $signatureValue = null,
) {
if ($expirationDate instanceof DateTime) {
if ($expirationDate instanceof DateTimeInterface) {
$this->expirationDate = $expirationDate->format("Y-m-d\TH:i:s.0000000P");
} else {
$this->expirationDate = $expirationDate;
Expand Down
8 changes: 5 additions & 3 deletions tests/InvoiceOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace tests;

use DateTime;
use DateInterval;
use DateTimeImmutable;
use DateTimeZone;
use netFantom\RobokassaApi\Options\InvoiceOptions;
use netFantom\RobokassaApi\RobokassaApi;
Expand All @@ -24,9 +25,10 @@ public function testExpirationDate(): void
outSum: 10.00,
invId: 1,
description: 'Description',
expirationDate: (new DateTime('2030-01-01 10:20:30', new DateTimeZone('+3'))),
expirationDate: (new DateTimeImmutable('2030-01-01 10:20:30', new DateTimeZone('+3')))
->add(new DateInterval('PT48H')),
);
$this->assertEquals('2030-01-01T10:20:30.0000000+03:00', $invoiceOptions->expirationDate);
$this->assertEquals('2030-01-03T10:20:30.0000000+03:00', $invoiceOptions->expirationDate);
}

public function testParameters(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/RobokassaApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace tests;

use ArgumentCountError;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Http\Discovery\Psr18Client;
use netFantom\RobokassaApi\Exceptions\{MissingRequestFactory, MissingStreamFactory, TooLongSmsMessageException};
Expand Down Expand Up @@ -41,7 +41,7 @@ public function testExpirationDate(): void
outSum: 10.00,
invId: 1,
description: 'Description',
expirationDate: (new DateTime('2030-01-01 10:20:30', new DateTimeZone('+3'))),
expirationDate: (new DateTimeImmutable('2030-01-01 10:20:30', new DateTimeZone('+3'))),
userParameters: [
'user_id' => 1,
'email' => 'user@example.com',
Expand Down

0 comments on commit ddffa61

Please sign in to comment.