From 67669af6ac7553fdb1f741ab5d4d5c33101ee58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Sat, 24 Nov 2018 22:43:17 +0100 Subject: [PATCH] Added the `TrackedTime` model --- .semver | 6 +- CHANGELOG.md | 6 ++ example/main.php | 1 - lib/models/TrackedTime.php | 159 +++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 lib/models/TrackedTime.php diff --git a/.semver b/.semver index 798fe47..32abc21 100644 --- a/.semver +++ b/.semver @@ -1,6 +1,6 @@ --- :major: 0 -:minor: 1 -:patch: 1 +:minor: 2 +:patch: 0 :special: '' -:metadata: '' +:metadata: '' \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 1204cf5..6e482ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/example/main.php b/example/main.php index 89aecfc..bf3342a 100644 --- a/example/main.php +++ b/example/main.php @@ -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); } diff --git a/lib/models/TrackedTime.php b/lib/models/TrackedTime.php new file mode 100644 index 0000000..9f2f7ea --- /dev/null +++ b/lib/models/TrackedTime.php @@ -0,0 +1,159 @@ +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; + } +}