Refactor Tag models to php7 code (#548)

* Refactor models to php7 code

* return model from create method fix
This commit is contained in:
Radoje Albijanic 2019-01-22 18:38:44 +01:00 committed by Tobias Nyholm
parent 259d91cb75
commit 272033dff2
8 changed files with 54 additions and 179 deletions

View File

@ -22,10 +22,6 @@ final class CountryResponse implements ApiResponse
* @var array [locale => data[]] * @var array [locale => data[]]
*/ */
private $countries; private $countries;
/**
* @var string
*/
private $tag; private $tag;
private function __construct() private function __construct()

View File

@ -18,31 +18,21 @@ use Mailgun\Model\ApiResponse;
*/ */
final class DeleteResponse implements ApiResponse final class DeleteResponse implements ApiResponse
{ {
/**
* @var string
*/
private $message; private $message;
/** private function __construct()
* @param string $message
*/
private function __construct($message)
{ {
$this->message = $message;
} }
/** public static function create(array $data): self
* @return DeleteResponse
*/
public static function create(array $data)
{ {
return new self($data['message']); $model = new self();
$model->message = $data['message'] ?? '';
return $model;
} }
/** public function getMessage(): string
* @return string
*/
public function getMessage()
{ {
return $this->message; return $this->message;
} }

View File

@ -22,10 +22,6 @@ final class DeviceResponse implements ApiResponse
* @var array [name => data[]] * @var array [name => data[]]
*/ */
private $devices; private $devices;
/**
* @var string
*/
private $tag; private $tag;
private function __construct() private function __construct()

View File

@ -11,9 +11,9 @@ declare(strict_types=1);
namespace Mailgun\Model\Tag; namespace Mailgun\Model\Tag;
use Mailgun\Model\ApiResponse;
use Mailgun\Model\PaginationResponse; use Mailgun\Model\PaginationResponse;
use Mailgun\Model\PagingProvider; use Mailgun\Model\PagingProvider;
use Mailgun\Model\ApiResponse;
/** /**
* @author Tobias Nyholm <tobias.nyholm@gmail.com> * @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -27,32 +27,29 @@ final class IndexResponse implements ApiResponse, PagingProvider
*/ */
private $items; private $items;
/** private function __construct()
* @param Tag[] $items
*/
private function __construct(array $items, array $paging)
{ {
$this->items = $items;
$this->paging = $paging;
} }
/** public static function create(array $data): self
* @return IndexResponse
*/
public static function create(array $data)
{ {
$items = []; $items = [];
foreach ($data['items'] as $item) { foreach ($data['items'] as $item) {
$items[] = Tag::create($item); $items[] = Tag::create($item);
} }
return new self($items, $data['paging']); $model = new self();
$model->items = $items;
$model->paging = $data['paging'];
return $model;
} }
/** /**
* @return Tag[] * @return Tag[]
*/ */
public function getItems() public function getItems(): array
{ {
return $this->items; return $this->items;
} }

View File

@ -22,10 +22,6 @@ final class ProviderResponse implements ApiResponse
* @var array [name => data[]] * @var array [name => data[]]
*/ */
private $providers; private $providers;
/**
* @var string
*/
private $tag; private $tag;
private function __construct() private function __construct()

View File

@ -18,110 +18,57 @@ use Mailgun\Model\ApiResponse;
*/ */
final class StatisticsResponse implements ApiResponse final class StatisticsResponse implements ApiResponse
{ {
/**
* @var string
*/
private $tag; private $tag;
/**
* @var string
*/
private $description; private $description;
/**
* @var string
*/
private $resolution; private $resolution;
/**
* @var \DateTime
*/
private $start; private $start;
/**
* @var \DateTime
*/
private $end; private $end;
/**
* @var array
*/
private $stats; private $stats;
/** private function __construct()
* @param string $tag
* @param string $description
* @param string $resolution
*/
private function __construct($tag, $description, \DateTime $start, \DateTime $end, $resolution, array $stats)
{ {
$this->tag = $tag;
$this->description = $description;
$this->resolution = $resolution;
$this->start = $start;
$this->end = $end;
$this->stats = $stats;
} }
/** public static function create(array $data): self
* @return StatisticsResponse
*/
public static function create(array $data)
{ {
return new self( $model = new self();
$data['tag'],
$data['description'], $model->tag = $data['tag'] ?? '';
new \DateTime($data['start']), $model->description = $data['description'] ?? '';
new \DateTime($data['end']), $model->resolution = $data['resolution'] ?? null;
$data['resolution'], $model->stats = $data['stats'] ?? [];
$data['stats'] $model->start = isset($data['start']) ? new \DateTimeImmutable($data['start']) : null;
); $model->end = isset($data['end']) ? new \DateTimeImmutable($data['end']) : null;
return $model;
} }
/** public function getTag(): string
* @return string
*/
public function getTag()
{ {
return $this->tag; return $this->tag;
} }
/** public function getDescription(): string
* @return string
*/
public function getDescription()
{ {
return $this->description; return $this->description;
} }
/** public function getResolution(): ?string
* @return string
*/
public function getResolution()
{ {
return $this->resolution; return $this->resolution;
} }
/** public function getStart(): ?\DateTimeImmutable
* @return \DateTime
*/
public function getStart()
{ {
return $this->start; return $this->start;
} }
/** public function getEnd(): ?\DateTimeImmutable
* @return \DateTime
*/
public function getEnd()
{ {
return $this->end; return $this->end;
} }
/** public function getStats(): array
* @return array
*/
public function getStats()
{ {
return $this->stats; return $this->stats;
} }

View File

@ -13,79 +13,43 @@ namespace Mailgun\Model\Tag;
class Tag class Tag
{ {
/**
* @var string
*/
private $tag; private $tag;
/**
* @var string
*/
private $description; private $description;
/**
* @var \DateTime
*/
private $firstSeen; private $firstSeen;
/**
* @var \DateTime
*/
private $lastSeen; private $lastSeen;
/** private function __construct()
* @param string $tag
* @param string $description
* @param \DateTime $firstSeen
* @param \DateTime $lastSeen
*/
private function __construct($tag, $description, \DateTime $firstSeen = null, \DateTime $lastSeen = null)
{ {
$this->tag = $tag;
$this->description = $description;
$this->firstSeen = $firstSeen;
$this->lastSeen = $lastSeen;
} }
/** public static function create(array $data): self
* @return Tag
*/
public static function create(array $data)
{ {
$firstSeen = isset($data['first-seen']) ? new \DateTime($data['first-seen']) : null; $model = new self();
$lastSeen = isset($data['last-seen']) ? new \DateTime($data['last-seen']) : null;
return new self($data['tag'], $data['description'], $firstSeen, $lastSeen); $model->tag = $data['tag'] ?? '';
$model->description = $data['description'] ?? '';
$model->firstSeen = isset($data['first-seen']) ? new \DateTimeImmutable($data['first-seen']) : null;
$model->lastSeen = isset($data['last-seen']) ? new \DateTimeImmutable($data['last-seen']) : null;
return $model;
} }
/** public function getTag(): string
* @return string
*/
public function getTag()
{ {
return $this->tag; return $this->tag;
} }
/** public function getDescription(): string
* @return string
*/
public function getDescription()
{ {
return $this->description; return $this->description;
} }
/** public function getFirstSeen(): ?\DateTimeImmutable
* @return \DateTime
*/
public function getFirstSeen()
{ {
return $this->firstSeen; return $this->firstSeen;
} }
/** public function getLastSeen(): ?\DateTimeImmutable
* @return \DateTime
*/
public function getLastSeen()
{ {
return $this->lastSeen; return $this->lastSeen;
} }

View File

@ -18,31 +18,20 @@ use Mailgun\Model\ApiResponse;
*/ */
final class UpdateResponse implements ApiResponse final class UpdateResponse implements ApiResponse
{ {
/**
* @var string
*/
private $message; private $message;
/** private function __construct()
* @param string $message
*/
private function __construct($message)
{ {
$this->message = $message;
} }
/** public static function create(array $data): self
* @return UpdateResponse
*/
public static function create(array $data)
{ {
return new self($data['message']); $model = new self();
$model->message = $data['message'] ?? '';
} }
/** public function getMessage(): string
* @return string
*/
public function getMessage()
{ {
return $this->message; return $this->message;
} }