Skip to content

Commit

Permalink
Date/time objects in service responses & entities not immutable (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomzyakov authored Oct 23, 2020
1 parent 97e55cb commit 91f5955
Show file tree
Hide file tree
Showing 25 changed files with 279 additions and 269 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher
- `Avtocod\B2BApi\Exceptions\*`
- `Avtocod\B2BApi\Responses\Entities\*`
- `Avtocod\B2BApi\Responses\*Response`
- Date/time objects in service responses & entities not immutable (`DateTimeImmutable` instead `DateTime`)

### Fixed

Expand Down
5 changes: 3 additions & 2 deletions src/DateTimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Avtocod\B2BApi;

use DateTime;
use DateTimeInterface;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -37,11 +38,11 @@ public static function createFromIso8601Zulu(string $time): DateTime
/**
* Convert DateTime object into string, using ISO8601 (zulu) format.
*
* @param DateTime $date_time
* @param DateTimeInterface $date_time
*
* @return string E.g.: '2017-01-05T16:45:23.000Z'
*/
public static function toIso8601Zulu(DateTime $date_time): string
public static function toIso8601Zulu(DateTimeInterface $date_time): string
{
return $date_time->format('Y-m-d\\TH:i:s.v\\Z');
}
Expand Down
36 changes: 18 additions & 18 deletions src/Responses/DevTokenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Avtocod\B2BApi\Responses;

use DateTime;
use DateTimeImmutable;
use Tarampampam\Wrappers\Json;
use Avtocod\B2BApi\DateTimeFactory;
use Avtocod\B2BApi\Exceptions\BadResponseException;
Expand Down Expand Up @@ -34,7 +34,7 @@ class DevTokenResponse implements ResponseInterface
protected $password_hash;

/**
* @var DateTime
* @var DateTimeImmutable
*/
protected $date;

Expand Down Expand Up @@ -76,24 +76,24 @@ class DevTokenResponse implements ResponseInterface
/**
* Create a new response instance.
*
* @param string $raw_response
* @param string $user
* @param string $password
* @param string $password_hash
* @param DateTime $date
* @param int $stamp
* @param int $age
* @param string $salt
* @param string $salted_pass_hash
* @param string $raw_token
* @param string $token
* @param string $header
* @param string $raw_response
* @param string $user
* @param string $password
* @param string $password_hash
* @param DateTimeImmutable $date
* @param int $stamp
* @param int $age
* @param string $salt
* @param string $salted_pass_hash
* @param string $raw_token
* @param string $token
* @param string $header
*/
private function __construct(string $raw_response,
string $user,
string $password,
string $password_hash,
DateTime $date,
DateTimeImmutable $date,
int $stamp,
int $age,
string $salt,
Expand Down Expand Up @@ -142,7 +142,7 @@ public static function fromHttpResponse(HttpResponseInterface $response): self
$as_array['user'],
$as_array['pass'],
$as_array['pass_hash'],
DateTimeFactory::createFromIso8601Zulu($as_array['date']),
DateTimeImmutable::createFromMutable(DateTimeFactory::createFromIso8601Zulu($as_array['date'])),
$as_array['stamp'],
$as_array['age'],
$as_array['salt'],
Expand Down Expand Up @@ -178,9 +178,9 @@ public function getPasswordHash(): string
}

/**
* @return DateTime
* @return DateTimeImmutable
*/
public function getDate(): DateTime
public function getDate(): DateTimeImmutable
{
return $this->date;
}
Expand Down
36 changes: 18 additions & 18 deletions src/Responses/Entities/Balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Avtocod\B2BApi\Responses\Entities;

use DateTime;
use DateTimeImmutable;
use Avtocod\B2BApi\DateTimeFactory;

class Balance implements CanCreateSelfFromArrayInterface
Expand Down Expand Up @@ -50,33 +50,33 @@ class Balance implements CanCreateSelfFromArrayInterface
protected $quote_use;

/**
* @var DateTime|null
* @var DateTimeImmutable|null
*/
protected $created_at;

/**
* @var DateTime|null
* @var DateTimeImmutable|null
*/
protected $updated_at;

/**
* Create a new balance instance.
*
* @param string $report_type_uid Report type unique identifier
* @param string $balance_type Balance entry type (e.g.: `DAY`, `MONTH`, `TOTAL`)
* @param int $quote_init Initial quota value
* @param int $quote_up Added quota value
* @param int $quote_use Used quota value
* @param DateTime|null $created_at Balance entry created at
* @param DateTime|null $updated_at Last changes was made at
* @param string $report_type_uid Report type unique identifier
* @param string $balance_type Balance entry type (e.g.: `DAY`, `MONTH`, `TOTAL`)
* @param int $quote_init Initial quota value
* @param int $quote_up Added quota value
* @param int $quote_use Used quota value
* @param DateTimeImmutable|null $created_at Balance entry created at
* @param DateTimeImmutable|null $updated_at Last changes was made at
*/
public function __construct(string $report_type_uid,
string $balance_type,
int $quote_init,
int $quote_up,
int $quote_use,
?DateTime $created_at,
?DateTime $updated_at)
?DateTimeImmutable $created_at,
?DateTimeImmutable $updated_at)
{
$this->report_type_uid = $report_type_uid;
$this->balance_type = $balance_type;
Expand All @@ -99,10 +99,10 @@ public static function fromArray(array $data): self
$data['quote_up'],
$data['quote_use'],
isset($data['created_at'])
? DateTimeFactory::createFromIso8601Zulu($data['created_at'])
? DateTimeImmutable::createFromMutable(DateTimeFactory::createFromIso8601Zulu($data['created_at']))
: null,
isset($data['updated_at'])
? DateTimeFactory::createFromIso8601Zulu($data['updated_at'])
? DateTimeImmutable::createFromMutable(DateTimeFactory::createFromIso8601Zulu($data['updated_at']))
: null
);
}
Expand Down Expand Up @@ -160,19 +160,19 @@ public function getQuoteUse(): int
/**
* Get created at date/time.
*
* @return DateTime|null
* @return DateTimeImmutable|null
*/
public function getCreatedAt(): ?DateTime
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->created_at;
}

/**
* Get last changes date/time.
*
* @return DateTime|null
* @return DateTimeImmutable|null
*/
public function getUpdatedAt(): ?DateTime
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updated_at;
}
Expand Down
50 changes: 25 additions & 25 deletions src/Responses/Entities/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Avtocod\B2BApi\Responses\Entities;

use DateTime;
use DateTimeImmutable;
use Avtocod\B2BApi\DateTimeFactory;

class Domain implements CanCreateSelfFromArrayInterface
Expand Down Expand Up @@ -40,7 +40,7 @@ class Domain implements CanCreateSelfFromArrayInterface
protected $tags;

/**
* @var DateTime
* @var DateTimeImmutable
*/
protected $created_at;

Expand All @@ -50,7 +50,7 @@ class Domain implements CanCreateSelfFromArrayInterface
protected $created_by;

/**
* @var DateTime
* @var DateTimeImmutable
*/
protected $updated_at;

Expand All @@ -60,12 +60,12 @@ class Domain implements CanCreateSelfFromArrayInterface
protected $updated_by;

/**
* @var DateTime
* @var DateTimeImmutable
*/
protected $active_from;

/**
* @var DateTime
* @var DateTimeImmutable
*/
protected $active_to;

Expand All @@ -88,12 +88,12 @@ class Domain implements CanCreateSelfFromArrayInterface
* @param string $state Domain state (e.g.: `DRAFT`, `ACTIVE`, `BANNED`)
* @param array<string>|null $roles Domain roles (optional)
* @param array<string> $tags Additional domain tags
* @param DateTime $created_at Domain created at
* @param DateTimeImmutable $created_at Domain created at
* @param string $created_by Domain creator
* @param DateTime $updated_at Last changes was made at
* @param DateTimeImmutable $updated_at Last changes was made at
* @param string $updated_by Last changes was made by
* @param DateTime $active_from Active from
* @param DateTime $active_to Active to
* @param DateTimeImmutable $active_from Active from
* @param DateTimeImmutable $active_to Active to
* @param int|null $id Internal database identifier (optional, only for administrators)
* @param bool|null $deleted Is deleted flag (optional, only for administrators)
*/
Expand All @@ -103,12 +103,12 @@ public function __construct(string $uid,
string $state,
?array $roles,
array $tags,
DateTime $created_at,
DateTimeImmutable $created_at,
string $created_by,
DateTime $updated_at,
DateTimeImmutable $updated_at,
string $updated_by,
DateTime $active_from,
DateTime $active_to,
DateTimeImmutable $active_from,
DateTimeImmutable $active_to,
?int $id,
?bool $deleted)
{
Expand Down Expand Up @@ -142,12 +142,12 @@ public static function fromArray(array $data): self
? \array_filter(\explode(',', $data['roles']))
: null,
\array_filter(\explode(',', $data['tags'])),
DateTimeFactory::createFromIso8601Zulu($data['created_at']),
DateTimeImmutable::createFromMutable(DateTimeFactory::createFromIso8601Zulu($data['created_at'])),
$data['created_by'],
DateTimeFactory::createFromIso8601Zulu($data['updated_at']),
DateTimeImmutable::createFromMutable(DateTimeFactory::createFromIso8601Zulu($data['updated_at'])),
$data['updated_by'],
DateTimeFactory::createFromIso8601Zulu($data['active_from']),
DateTimeFactory::createFromIso8601Zulu($data['active_to']),
DateTimeImmutable::createFromMutable(DateTimeFactory::createFromIso8601Zulu($data['active_from'])),
DateTimeImmutable::createFromMutable(DateTimeFactory::createFromIso8601Zulu($data['active_to'])),
$data['id'] ?? null,
$data['deleted'] ?? null
);
Expand Down Expand Up @@ -216,9 +216,9 @@ public function getTags(): array
/**
* Get created at date/time.
*
* @return DateTime
* @return DateTimeImmutable
*/
public function getCreatedAt(): DateTime
public function getCreatedAt(): DateTimeImmutable
{
return $this->created_at;
}
Expand All @@ -236,9 +236,9 @@ public function getCreatedBy(): string
/**
* Get last changes date/time.
*
* @return DateTime
* @return DateTimeImmutable
*/
public function getUpdatedAt(): DateTime
public function getUpdatedAt(): DateTimeImmutable
{
return $this->updated_at;
}
Expand All @@ -256,19 +256,19 @@ public function getUpdatedBy(): string
/**
* Get active from date/time.
*
* @return DateTime
* @return DateTimeImmutable
*/
public function getActiveFrom(): DateTime
public function getActiveFrom(): DateTimeImmutable
{
return $this->active_from;
}

/**
* Get active to date/time.
*
* @return DateTime
* @return DateTimeImmutable
*/
public function getActiveTo(): DateTime
public function getActiveTo(): DateTimeImmutable
{
return $this->active_to;
}
Expand Down
Loading

0 comments on commit 91f5955

Please sign in to comment.