Added the TrackedTime model

This commit is contained in:
Cédric Belin
2018-11-24 22:43:17 +01:00
parent 45cb20e939
commit 67669af6ac
4 changed files with 168 additions and 4 deletions

View File

@ -1,6 +1,6 @@
---
:major: 0
:minor: 1
:patch: 1
:minor: 2
:patch: 0
:special: ''
:metadata: ''
:metadata: ''

View File

@ -1,5 +1,11 @@
# Changelog
## Version [0.2.0](https://github.com/sab-international/gitea.php/compare/v0.1.1...v0.2.0)
- Added `__toString()` methods to the model classes.
- Added the `TrackedTime` model.
- Added a user guide based on [MkDocs](http://www.mkdocs.org).
- Updated the package dependencies.
## Version [0.1.1](https://github.com/sab-international/gitea.php/compare/v0.1.0...v0.1.1)
- Fixed [issue #1](https://github.com/sab-international/gitea.php/issues/1): the `PushEvent::jsonSerialize()` method returns `"TODO"` strings.

View File

@ -13,6 +13,5 @@ function main(): PushEvent {
$data = json_decode((string) file_get_contents('php://input'));
if (!is_object($data)) throw new UnexpectedValueException('Invalid payload data.');
return PushEvent::fromJson($data);
}

159
lib/models/TrackedTime.php Normal file
View File

@ -0,0 +1,159 @@
<?php
declare(strict_types=1);
namespace Gitea\Models;
/**
* Represents the worked time for an issue or pull request.
*/
class TrackedTime implements \JsonSerializable {
/**
* @var \DateTime|null The date the entry was created.
*/
private $createdAt;
/**
* @var int The entry identifier.
*/
private $id;
/**
* @var int The identifier of the associated issue or pull request.
*/
private $issueId = -1;
/**
* @var int The elapsed time, in seconds.
*/
private $time;
/**
* @var int The identifier of the initiating user.
*/
private $userId = -1;
/**
* Creates a new entry.
* @param int $id The entry identifier.
* @param int $time The elapsed time, in seconds.
*/
function __construct(int $id, int $time) {
$this->id = $id;
$this->setTime($time);
}
/**
* Returns a string representation of this object.
* @return string The string representation of this object.
*/
function __toString(): string {
$json = json_encode($this, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
return static::class." $json";
}
/**
* Creates a new entry from the specified JSON map.
* @param object $map A JSON map representing a entry.
* @return static The instance corresponding to the specified JSON map.
*/
static function fromJson(object $map): self {
return (new static(isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->time) && is_int($map->time) ? $map->time : 0))
->setCreatedAt(isset($map->created) && is_string($map->created) ? $map->created : null)
->setIssueId(isset($map->issue_id) && is_int($map->issue_id) ? $map->issue_id : -1)
->setUserId(isset($map->user_id) && is_int($map->user_id) ? $map->user_id : -1);
}
/**
* Gets the date the entry was created.
* @return \DateTime|null The date the entry was created.
*/
function getCreatedAt(): ?\DateTime {
return $this->createdAt;
}
/**
* Gets the entry identifier.
* @return int The entry identifier.
*/
function getId(): int {
return $this->id;
}
/**
* Gets the identifier of the associated issue or pull request.
* @return int The identifier of the associated issue or pull request.
*/
function getIssueId(): int {
return $this->issueId;
}
/**
* Gets the elapsed time, in seconds.
* @return int The elapsed time, in seconds.
*/
function getTime(): int {
return $this->time;
}
/**
* Gets the identifier of the initiating user.
* @return int The identifier of the initiating user.
*/
function getUserId(): int {
return $this->userId;
}
/**
* Converts this object to a map in JSON format.
* @return \stdClass The map in JSON format corresponding to this object.
*/
function jsonSerialize(): \stdClass {
return (object) [
'created' => ($date = $this->getCreatedAt()) ? $date->format('c') : null,
'id' => $this->getId(),
'issue_id' => $this->getIssueId(),
'time' => $this->getTime(),
'user_id' => $this->getUserId()
];
}
/**
* Sets the date the entry was created.
* @param \DateTime|string|null $value The new date of creation.
* @return $this This instance.
*/
function setCreatedAt($value): self {
$this->createdAt = is_string($value) ? new \DateTime($value) : $value;
return $this;
}
/**
* Sets the identifier of the associated issue or pull request.
* @param int $value The new issue identifier.
* @return $this This instance.
*/
function setIssueId(int $value): self {
$this->issueId = $value;
return $this;
}
/**
* Sets the elapsed time, in seconds.
* @param int $value The new elapsed time, in seconds.
* @return $this This instance.
*/
function setTime(int $value): self {
$this->time = $value;
return $this;
}
/**
* Sets the identifier of the initiating user.
* @param int $value The new user identifier.
* @return $this This instance.
*/
function setUserId(int $value): self {
$this->userId = $value;
return $this;
}
}