Porting the models to JsonSerializable interface

This commit is contained in:
Cédric Belin
2018-11-03 11:46:18 +01:00
parent a5f84778cd
commit a28d8e05a6
5 changed files with 299 additions and 148 deletions

View File

@ -8,45 +8,18 @@ use Psr\Http\Message\{UriInterface};
/**
* Represents a Gitea push event.
* @property \ArrayObject $commits The revision commits.
* @property UriInterface|null $compareUrl The URL for comparing the revisions.
*/
class PushEvent {
class PushEvent implements \JsonSerializable {
/**
* @var string The hash of the new Git revision.
*/
public $after = '';
private $after = '';
/**
* @var string The hash of the previous Git revision.
*/
public $before = '';
/**
* @var User|null The user who pushed the commits.
*/
public $pusher;
/**
* @var string The Git reference.
*/
public $ref = '';
/**
* @var Repository|null The repository where the commits were pushed.
*/
public $repository;
/**
* @var string The secret used to validate this event.
*/
public $secret = '';
/**
* @var User|null The user who sent this event.
*/
public $sender;
private $before = '';
/**
* @var \ArrayObject The revision commits.
@ -58,6 +31,31 @@ class PushEvent {
*/
private $compareUrl;
/**
* @var User|null The user who pushed the commits.
*/
private $pusher;
/**
* @var string The Git reference.
*/
private $ref = '';
/**
* @var Repository|null The repository where the commits were pushed.
*/
private $repository;
/**
* @var string The secret used to validate this event.
*/
private $secret = '';
/**
* @var User|null The user who sent this event.
*/
private $sender;
/**
* Creates a new event.
* @param array $config Name-value pairs that will be used to initialize the object properties.
@ -86,20 +84,19 @@ class PushEvent {
}
/**
* Returns the list of fields that should be returned by default.
* @return array The list of field names or field definitions.
* Gets the hash of the new Git revision.
* @return string The hash of the new Git revision.
*/
function fields(): array {
return [
'after',
'before',
'compare_url' => function(self $model) { return ($url = $model->getCompareUrl()) ? (string) $url : null; },
'commits',
'pusher',
'ref',
'repository',
'sender'
];
function getAfter(): string {
return $this->after;
}
/**
* Gets the hash of the new previous revision.
* @return string The hash of the previous Git revision.
*/
function getBefore(): string {
return $this->before;
}
/**
@ -118,6 +115,59 @@ class PushEvent {
return $this->compareUrl;
}
/**
* Gets the Git reference.
* @return string The Git reference.
*/
function getRef(): string {
return $this->ref;
}
/**
* Gets the secret used to validate this event.
* @return string The secret used to validate this event.
*/
function getSecret(): string {
return $this->secret;
}
/**
* 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) [
'after' => $this->getAfter(),
'before' => $this->getBefore(),
'compare_url' => ($url = $this->getCompareUrl()) ? (string) $url : null,
'commits' => array_map(function(PayloadCommit $commit) { return $commit->jsonSerialize(); }, $this->getCommits()->getArrayCopy()),
'pusher',
'ref' => $this->getRef(),
'repository',
'sender'
];
}
/**
* Sets the hash of the new Git revision.
* @param string $value The hash of the new Git revision.
* @return $this This instance.
*/
function setAfter(string $value): self {
$this->after = $value;
return $this;
}
/**
* Sets the hash of the new previous revision.
* @param string $value The hash of the new previous revision.
* @return $this This instance.
*/
function setBefore(string $value): self {
$this->before = $value;
return $this;
}
/**
* Sets the revision commits.
* @param PayloadCommit[] $values The revision commits.
@ -137,4 +187,24 @@ class PushEvent {
$this->compareUrl = is_string($value) ? new Uri($value) : $value;
return $this;
}
/**
* Sets the Git reference.
* @param string $value The new Git reference.
* @return $this This instance.
*/
function setRef(string $value): self {
$this->ref = $value;
return $this;
}
/**
* Sets the secret used to validate this event.
* @param string $value The new secret used to validate this event.
* @return $this This instance.
*/
function setSecret(string $value): self {
$this->secret = $value;
return $this;
}
}

View File

@ -7,10 +7,8 @@ use Psr\Http\Message\{UriInterface};
/**
* Represents a commit.
* @property \DateTime|null $timestamp The commit date.
* @property UriInterface|null $url The URL to the commit's history.
*/
class PayloadCommit {
class PayloadCommit implements \JsonSerializable {
/**
* @var PayloadUser|null The person who authored the commit.
@ -64,22 +62,6 @@ class PayloadCommit {
]);
}
/**
* Returns the list of fields that should be returned by default.
* @return array The list of field names or field definitions.
*/
function fields(): array {
return [
'author',
'committer',
'id',
'message',
'timestamp' => function(PayloadCommit $model) { return ($date = $model->getTimestamp()) ? $date->format('c') : null; },
'url' => function(self $model) { return ($url = $model->getUrl()) ? (string) $url : null; },
'verification'
];
}
/**
* Gets the commit date.
* @return \DateTime|null The commit date.
@ -96,6 +78,22 @@ class PayloadCommit {
return $this->url;
}
/**
* 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) [
'author',
'committer',
'id',
'message',
'timestamp' => ($date = $this->getTimestamp()) ? $date->format('c') : null,
'url' => ($url = $this->getUrl()) ? (string) $url : null,
'verification'
];
}
/**
* Sets the commit date.
* @param \DateTime|string|null $value The new commit date.

View File

@ -5,7 +5,7 @@ namespace Gitea\Models;
/**
* Represents the GPG verification of a commit.
*/
class PayloadCommitVerification {
class PayloadCommitVerification implements \JsonSerializable {
/**
* @var bool Value indicating whether the verification has succeeded.
@ -42,11 +42,11 @@ class PayloadCommitVerification {
}
/**
* Returns the list of fields that should be returned by default.
* @return array The list of field names or field definitions.
* Converts this object to a map in JSON format.
* @return \stdClass The map in JSON format corresponding to this object.
*/
function fields(): array {
return [
function jsonSerialize(): \stdClass {
return (object) [
'payload',
'reason',
'signature',

View File

@ -7,14 +7,8 @@ use Psr\Http\Message\{UriInterface};
/**
* Represents a repository.
* @property UriInterface|null $cloneUrl The HTTP-based URL for cloning this repository.
* @property \DateTime|null $createdAt The date the repository was created.
* @property UriInterface|null $htmlUrl The Gitea URL of this repository.
* @property UriInterface|null $sshUrl The SSH-based URL for cloning this repository.
* @property \DateTime|null $updatedAt The date the repository was updated.
* @property UriInterface|null $website The URL of the repository website.
*/
class Repository {
class Repository implements \JsonSerializable {
/**
* @var string The name of the default branch.
@ -164,38 +158,6 @@ class Repository {
]);
}
/**
* Returns the list of fields that should be returned by default.
* @return array The list of field names or field definitions.
*/
function fields(): array {
return [
'clone_url' => function(self $model) { return ($url = $model->getCloneUrl()) ? (string) $url : null; },
'created_at' => function(Repository $model) { return ($date = $model->getCreatedAt()) ? $date->format('c') : null; },
'default_branch' => 'defaultBranch',
'description',
'empty' => 'isEmpty',
'fork' => 'isFork',
'forks_count' => 'forksCount',
'full_name' => 'fullName',
'html_url' => function(self $model) { return ($url = $model->getHtmlUrl()) ? (string) $url : null; },
'id',
'mirror' => 'isMirror',
'name',
'open_issues_count' => 'openIssuesCount',
'owner',
'parent',
'permissions',
'private' => 'isPrivate',
'size',
'ssh_url' => function(self $model) { return ($url = $model->getSshUrl()) ? (string) $url : null; },
'stars_count' => 'starsCount',
'updated_at' => function(Repository $model) { return ($date = $model->getUpdatedAt()) ? $date->format('c') : null; },
'watchers_count' => 'watchersCount',
'website' => function(self $model) { return ($url = $model->getWebsite()) ? (string) $url : null; },
];
}
/**
* Gets the HTTP-based URL for cloning this repository.
* @return UriInterface|null The HTTP-based URL for cloning this repository.
@ -244,6 +206,38 @@ class Repository {
return $this->website;
}
/**
* 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) [
'clone_url' => ($url = $this->getCloneUrl()) ? (string) $url : null,
'created_at' => ($date = $this->getCreatedAt()) ? $date->format('c') : null,
'default_branch' => 'defaultBranch',
'description',
'empty' => 'isEmpty',
'fork' => 'isFork',
'forks_count' => 'forksCount',
'full_name' => 'fullName',
'html_url' => ($url = $this->getHtmlUrl()) ? (string) $url : null,
'id',
'mirror' => 'isMirror',
'name',
'open_issues_count' => 'openIssuesCount',
'owner',
'parent',
'permissions',
'private' => 'isPrivate',
'size',
'ssh_url' => ($url = $this->getSshUrl()) ? (string) $url : null,
'stars_count' => 'starsCount',
'updated_at' => ($date = $this->getUpdatedAt()) ? $date->format('c') : null,
'watchers_count' => 'watchersCount',
'website' => ($url = $this->getWebsite()) ? (string) $url : null,
];
}
/**
* Sets the HTTP-based URL for cloning this repository.
* @param UriInterface|string|null $value The new URL for cloning this repository.

View File

@ -7,40 +7,39 @@ use Psr\Http\Message\{UriInterface};
/**
* Represents a Gitea user.
* @property UriInterface|null $avatarUrl The URL of the avatar image.
*/
class User {
/**
* @var string The mail address.
*/
public $email = '';
/**
* @var string The full name.
*/
public $fullName = '';
/**
* @var int The user identifier.
*/
public $id = -1;
/**
* @var string The user locale.
*/
public $language = '';
/**
* @var string The name of the Gitea account.
*/
public $login = '';
class User implements \JsonSerializable {
/**
* @var UriInterface|null The URL to the user's avatar.
*/
private $avatarUrl;
/**
* @var string The mail address.
*/
private $email = '';
/**
* @var string The full name.
*/
private $fullName = '';
/**
* @var int The user identifier.
*/
private $id = -1;
/**
* @var string The user locale.
*/
private $language = '';
/**
* @var string The name of the Gitea account.
*/
private $login = '';
/**
* Creates a new user from the specified JSON map.
* @param object $map A JSON map representing a user.
@ -57,21 +56,6 @@ class User {
]);
}
/**
* Returns the list of fields that should be returned by default.
* @return array The list of field names or field definitions.
*/
function fields(): array {
return [
'avatar_url' => function(self $model) { return ($url = $model->getAvatarUrl()) ? (string) $url : null; },
'email',
'full_name' => 'fullName',
'id',
'language',
'login'
];
}
/**
* Gets the URL of the avatar image.
* @return UriInterface|null The URL of the avatar image.
@ -80,6 +64,61 @@ class User {
return $this->avatarUrl;
}
/**
* Gets the mail address.
* @return string The mail address.
*/
function getEmail(): string {
return $this->email;
}
/**
* Gets the full name.
* @return string The full name.
*/
function getFullName(): string {
return $this->fullName;
}
/**
* Gets the user identifier.
* @return int The user identifier.
*/
function getId(): int {
return $this->id;
}
/**
* Gets the user locale.
* @return string The user locale.
*/
function getLanguage(): string {
return $this->language;
}
/**
* Gets the name of the Gitea account.
* @return string The name of the Gitea account.
*/
function getLogin(): string {
return $this->login;
}
/**
* 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) [
'avatar_url' => ($url = $this->getAvatarUrl()) ? (string) $url : null,
'email' => $this->getEmail(),
'full_name' => $this->getFullName(),
'id' => $this->getId(),
'language' => $this->getLanguage(),
'login' => $this->getLogin()
];
}
/**
* Sets the URL of the avatar image.
* @param UriInterface|string|null $value The new avatar URL.
@ -89,4 +128,54 @@ class User {
$this->avatarUrl = is_string($value) ? new Uri($value) : $value;
return $this;
}
/**
* Sets the mail address.
* @param string $value The new mail address.
* @return $this This instance.
*/
function setEmail(string $value): self {
$this->email = $value;
return $this;
}
/**
* Sets the full name.
* @param string $value The new full name.
* @return $this This instance.
*/
function setFullName(string $value): self {
$this->fullName = $value;
return $this;
}
/**
* Sets the user identifier.
* @param int $value The new user identifier.
* @return $this This instance.
*/
function setId(int $value): self {
$this->id = $value;
return $this;
}
/**
* Sets the user locale.
* @param string $value The new user locale.
* @return $this This instance.
*/
function setLanguage(string $value): self {
$this->language = $value;
return $this;
}
/**
* Sets the name of the Gitea account.
* @param string $value The new Gitea account.
* @return $this This instance.
*/
function setLogin(string $value): self {
$this->login = $value;
return $this;
}
}