Replacing public properties by getters and setters

This commit is contained in:
Cédric Belin
2018-11-08 18:00:19 +01:00
parent ded2f54c96
commit c0653faeb7
9 changed files with 423 additions and 97 deletions

6
.vscode/tasks.json vendored
View File

@ -31,6 +31,12 @@
"label": "robo : upgrade", "label": "robo : upgrade",
"problemMatcher": [], "problemMatcher": [],
"type": "shell" "type": "shell"
},
{
"command": "robo watch",
"label": "robo : watch",
"problemMatcher": [],
"type": "shell"
} }
] ]
} }

View File

@ -13,27 +13,22 @@ class PayloadCommit implements \JsonSerializable {
/** /**
* @var PayloadUser|null The person who authored the commit. * @var PayloadUser|null The person who authored the commit.
*/ */
public $author; private $author;
/** /**
* @var PayloadUser|null The person who committed the code. * @var PayloadUser|null The person who committed the code.
*/ */
public $committer; private $committer;
/** /**
* @var string The commit hash. * @var string The commit hash.
*/ */
public $id = ''; private $id;
/** /**
* @var string The commit message. * @var string The commit message.
*/ */
public $message = ''; private $message;
/**
* @var PayloadCommitVerification|null The GPG verification of this commit.
*/
public $verification;
/** /**
* @var \DateTime|null The commit date. * @var \DateTime|null The commit date.
@ -45,21 +40,65 @@ class PayloadCommit implements \JsonSerializable {
*/ */
private $url; private $url;
/**
* @var PayloadCommitVerification|null The GPG verification of this commit.
*/
private $verification;
/**
* Creates a new payload commit.
* @param string $id The commit hash.
* @param string $message The commit message.
*/
function __construct(string $id, string $message) {
$this->id = $id;
$this->setMessage($message);
}
/** /**
* Creates a new commit from the specified JSON map. * Creates a new commit from the specified JSON map.
* @param object $map A JSON map representing a commit. * @param object $map A JSON map representing a commit.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $map): self { static function fromJson(object $map): self {
return new static([ return (new static(isset($map->id) && is_string($map->id) ? $map->id : '', isset($map->message) && is_string($map->message) ? $map->message : ''))
'author' => isset($map->author) && is_object($map->author) ? PayloadUser::fromJson($map->author) : null, ->setAuthor(isset($map->author) && is_object($map->author) ? PayloadUser::fromJson($map->author) : null)
'committer' => isset($map->committer) && is_object($map->committer) ? PayloadUser::fromJson($map->committer) : null, ->setCommitter(isset($map->committer) && is_object($map->committer) ? PayloadUser::fromJson($map->committer) : null)
'id' => isset($map->id) && is_string($map->id) ? $map->id : '', ->setTimestamp(isset($map->timestamp) && is_string($map->timestamp) ? $map->timestamp : null)
'message' => isset($map->message) && is_string($map->message) ? $map->message : '', ->setUrl(isset($map->url) && is_string($map->url) ? $map->url : null)
'timestamp' => isset($map->timestamp) && is_string($map->timestamp) ? $map->timestamp : null, ->setVerification(isset($map->verification) && is_object($map->verification) ? PayloadCommitVerification::fromJson($map->verification) : null);
'url' => isset($map->url) && is_string($map->url) ? $map->url : null, }
'verification' => isset($map->verification) && is_object($map->verification) ? PayloadCommitVerification::fromJson($map->verification) : null
]); /**
* Gets the person who authored the commit.
* @return PayloadUser|null The person who authored the commit.
*/
function getAuthor(): ?PayloadUser {
return $this->author;
}
/**
* Gets the person who committed the code.
* @return PayloadUser|null The person who committed the code.
*/
function getCommitter(): ?PayloadUser {
return $this->committer;
}
/**
* Gets the commit hash.
* @return string The commit hash.
*/
function getId(): string {
return $this->id;
}
/**
* Gets the commit message.
* @return string The commit message.
*/
function getMessage(): string {
return $this->message;
} }
/** /**
@ -78,22 +117,60 @@ class PayloadCommit implements \JsonSerializable {
return $this->url; return $this->url;
} }
/**
* Gets the GPG verification of this commit.
* @return UriInterface|null The GPG verification of this commit.
*/
function getVerification(): ?PayloadCommitVerification {
return $this->verification;
}
/** /**
* Converts this object to a map in JSON format. * Converts this object to a map in JSON format.
* @return \stdClass The map in JSON format corresponding to this object. * @return \stdClass The map in JSON format corresponding to this object.
*/ */
function jsonSerialize(): \stdClass { function jsonSerialize(): \stdClass {
return (object) [ return (object) [
'author', 'author' => ($author = $this->getAuthor()) ? $author->jsonSerialize() : null,
'committer', 'committer' => ($committer = $this->getCommitter()) ? $committer->jsonSerialize() : null,
'id', 'id' => $this->getId(),
'message', 'message' => $this->getMessage(),
'timestamp' => ($date = $this->getTimestamp()) ? $date->format('c') : null, 'timestamp' => ($date = $this->getTimestamp()) ? $date->format('c') : null,
'url' => ($url = $this->getUrl()) ? (string) $url : null, 'url' => ($url = $this->getUrl()) ? (string) $url : null,
'verification' 'verification' => ($verification = $this->getVerification()) ? $verification->jsonSerialize() : null
]; ];
} }
/**
* Sets the person who authored the commit.
* @param PayloadUser|null $value The new author.
* @return $this This instance.
*/
function setAuthor(?PayloadUser $value): self {
$this->author = $value;
return $this;
}
/**
* Sets the person who committed the code.
* @param PayloadUser|null $value The new committer.
* @return $this This instance.
*/
function setCommitter(?PayloadUser $value): self {
$this->committer = $value;
return $this;
}
/**
* Sets the commit message.
* @param string $value The new message.
* @return $this This instance.
*/
function setMessage(string $value): self {
$this->message = $value;
return $this;
}
/** /**
* Sets the commit date. * Sets the commit date.
* @param \DateTime|string|null $value The new commit date. * @param \DateTime|string|null $value The new commit date.
@ -113,4 +190,14 @@ class PayloadCommit implements \JsonSerializable {
$this->url = is_string($value) ? new Uri($value) : $value; $this->url = is_string($value) ? new Uri($value) : $value;
return $this; return $this;
} }
/**
* Sets the commit message.
* @param PayloadCommitVerification|null $value The new message.
* @return $this This instance.
*/
function setVerification(?PayloadCommitVerification $value): self {
$this->verification = $value;
return $this;
}
} }

View File

@ -10,22 +10,29 @@ class PayloadCommitVerification implements \JsonSerializable {
/** /**
* @var bool Value indicating whether the verification has succeeded. * @var bool Value indicating whether the verification has succeeded.
*/ */
public $isVerified = false; private $isVerified = false;
/** /**
* @var string A custom message sent with the verification request. * @var string A custom message sent with the verification request.
*/ */
public $payload = ''; private $payload = '';
/** /**
* @var string A message providing details about the verification. * @var string A message providing details about the verification.
*/ */
public $reason = ''; private $reason = '';
/** /**
* @var string The signing key used for the verification. * @var string The signing key used for the verification.
*/ */
public $signature = ''; private $signature = '';
/**
* Creates a new verification of a payload commit.
*/
function __construct(bool $isVerified = false) {
// TODO $this->setVerified($isVerified);
}
/** /**
* Creates a new commit from the specified JSON map. * Creates a new commit from the specified JSON map.
@ -41,16 +48,48 @@ class PayloadCommitVerification implements \JsonSerializable {
]); ]);
} }
/**
* Gets the custom message sent with the verification request.
* @return string The custom message sent with the verification request.
*/
function getPayload(): string {
return $this->payload;
}
/**
* Gets the message providing details about the verification.
* @return string The message providing details about the verification.
*/
function getReason(): string {
return $this->reason;
}
/**
* Gets the signing key used for the verification.
* @return string The signing key used for the verification.
*/
function getSignature(): string {
return $this->signature;
}
/**
* TODO
* @return bool
*/
function isVerified(): bool {
return $this->isVerified;
}
/** /**
* Converts this object to a map in JSON format. * Converts this object to a map in JSON format.
* @return \stdClass The map in JSON format corresponding to this object. * @return \stdClass The map in JSON format corresponding to this object.
*/ */
function jsonSerialize(): \stdClass { function jsonSerialize(): \stdClass {
return (object) [ return (object) [
'payload', 'payload' => $this->getPayload(),
'reason', 'reason' => $this->getReason(),
'signature', 'signature' => $this->getSignature(),
'verified' => 'isVerified' 'verified' => $this->isVerified()
]; ];
} }
} }

View File

@ -5,22 +5,22 @@ namespace Gitea\Models;
/** /**
* Represents the author or committer of a commit. * Represents the author or committer of a commit.
*/ */
class PayloadUser { class PayloadUser implements \JsonSerializable {
/** /**
* @var string The mail address. * @var string The mail address.
*/ */
public $email = ''; private $email = '';
/** /**
* @var string The full name. * @var string The full name.
*/ */
public $name = ''; private $name = '';
/** /**
* @var string The name of the Gitea account. * @var string The name of the Gitea account.
*/ */
public $username = ''; private $username = '';
/** /**
* Creates a new user from the specified JSON map. * Creates a new user from the specified JSON map.
@ -34,4 +34,21 @@ class PayloadUser {
'username' => isset($map->username) && is_string($map->username) ? $map->username : '' 'username' => isset($map->username) && is_string($map->username) ? $map->username : ''
]); ]);
} }
/**
* 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'
];
}
} }

View File

@ -5,22 +5,32 @@ namespace Gitea\Models;
/** /**
* Represents a set of permissions. * Represents a set of permissions.
*/ */
class Permission { class Permission implements \JsonSerializable {
/** /**
* @var bool Value indicating whether administrator access is allowed. * @var bool Value indicating whether administrator access is allowed.
*/ */
public $admin = false; private $admin;
/** /**
* @var bool Value indicating whether pull is allowed. * @var bool Value indicating whether pull is allowed.
*/ */
public $pull = false; private $pull;
/** /**
* @var bool Value indicating whether push is allowed. * @var bool Value indicating whether push is allowed.
*/ */
public $push = false; private $push;
/**
* Creates a new permission.
* @param bool $admin Value indicating whether administrator access is allowed.
* @param bool $pull Value indicating whether pull is allowed.
* @param bool $push Value indicating whether push is allowed.
*/
function __construct(bool $admin = false, bool $pull = false, bool $push = false) {
$this->setAdmin($admin)->setPull($pull)->setPush($push);
}
/** /**
* Creates a new user from the specified JSON map. * Creates a new user from the specified JSON map.
@ -28,10 +38,76 @@ class Permission {
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $map): self { static function fromJson(object $map): self {
return new static([ return new static(
'admin' => isset($map->admin) && is_bool($map->admin) ? $map->admin : false, isset($map->admin) && is_bool($map->admin) ? $map->admin : false,
'pull' => isset($map->pull) && is_bool($map->pull) ? $map->pull : false, isset($map->pull) && is_bool($map->pull) ? $map->pull : false,
'push' => isset($map->push) && is_bool($map->push) ? $map->push : false isset($map->push) && is_bool($map->push) ? $map->push : false
]); );
}
/**
* Gets a value indicating whether administrator access is allowed.
* @return bool `true` if administrator access is allowed, otherwise `false`.
*/
function getAdmin(): bool {
return $this->admin;
}
/**
* Gets a value indicating whether pull is allowed.
* @return bool `true` if pull is allowed, otherwise `false`.
*/
function getPull(): bool {
return $this->pull;
}
/**
* Gets a value indicating whether push is allowed.
* @return bool `true` if push is allowed, otherwise `false`.
*/
function getPush(): bool {
return $this->push;
}
/**
* 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) [
'admin' => $this->getAdmin(),
'pull' => $this->getPull(),
'push' => $this->getPush()
];
}
/**
* Sets a value indicating whether administrator access is allowed.
* @param bool $value `true` to allow administrator access, otherwise `false`.
* @return $this This instance.
*/
function setAdmin(bool $value): self {
$this->admin = $value;
return $this;
}
/**
* Sets a value indicating whether pull is allowed.
* @param bool $value `true` to allow pull, otherwise `false`.
* @return $this This instance.
*/
function setPull(bool $value): self {
$this->pull = $value;
return $this;
}
/**
* Sets a value indicating whether push is allowed.
* @param bool $value `true` to allow push, otherwise `false`.
* @return $this This instance.
*/
function setPush(bool $value): self {
$this->push = $value;
return $this;
} }
} }

View File

@ -13,87 +13,87 @@ class Repository implements \JsonSerializable {
/** /**
* @var string The name of the default branch. * @var string The name of the default branch.
*/ */
public $defaultBranch = ''; private $defaultBranch = '';
/** /**
* @var string The repository description. * @var string The repository description.
*/ */
public $description = ''; private $description = '';
/** /**
* @var int The number of forks of this repository. * @var int The number of forks of this repository.
*/ */
public $forksCount = 0; private $forksCount = 0;
/** /**
* @var string The full name. * @var string The full name.
*/ */
public $fullName = ''; private $fullName = '';
/** /**
* @var int The repository identifier. * @var int The repository identifier.
*/ */
public $id = -1; private $id = -1;
/** /**
* @var bool Value indicating whether this repository is empty. * @var bool Value indicating whether this repository is empty.
*/ */
public $isEmpty = false; private $isEmpty = false;
/** /**
* @var bool Value indicating whether this repository is a fork. * @var bool Value indicating whether this repository is a fork.
*/ */
public $isFork = false; private $isFork = false;
/** /**
* @var bool Value indicating whether this repository is a mirror. * @var bool Value indicating whether this repository is a mirror.
*/ */
public $isMirror = false; private $isMirror = false;
/** /**
* @var bool Value indicating whether this repository is private. * @var bool Value indicating whether this repository is private.
*/ */
public $isPrivate = false; private $isPrivate = false;
/** /**
* @var string The repository name. * @var string The repository name.
*/ */
public $name = ''; private $name = '';
/** /**
* @var int The number of open issues of this repository. * @var int The number of open issues of this repository.
*/ */
public $openIssuesCount = 0; private $openIssuesCount = 0;
/** /**
* @var User|null The repository owner. * @var User|null The repository owner.
*/ */
public $owner; private $owner;
/** /**
* @var Repository|null The parent repository, if this repository is a fork or a mirror. * @var Repository|null The parent repository, if this repository is a fork or a mirror.
*/ */
public $parent; private $parent;
/** /**
* @var Permission|null The repository permissions. * @var Permission|null The repository permissions.
*/ */
public $permissions; private $permissions;
/** /**
* @var int The repository size, in kilobytes. * @var int The repository size, in kilobytes.
*/ */
public $size = 0; private $size = 0;
/** /**
* @var int The number of stars of this repository. * @var int The number of stars of this repository.
*/ */
public $starsCount = 0; private $starsCount = 0;
/** /**
* @var int The number of watchers of this repository. * @var int The number of watchers of this repository.
*/ */
public $watchersCount = 0; private $watchersCount = 0;
/** /**
* @var UriInterface|null The HTTP-based URL for cloning this repository. * @var UriInterface|null The HTTP-based URL for cloning this repository.

View File

@ -5,12 +5,20 @@ namespace Gitea\Models;
/** /**
* Warps the version of the Gitea server. * Warps the version of the Gitea server.
*/ */
class ServerVersion { class ServerVersion implements \JsonSerializable {
/** /**
* @var string The version number. * @var string The version number.
*/ */
public $version = ''; private $version;
/**
* Creates a new server version.
* @param string $version The version number.
*/
function __construct(string $version) {
$this->version = $version;
}
/** /**
* Creates a new server version from the specified JSON map. * Creates a new server version from the specified JSON map.
@ -18,8 +26,22 @@ class ServerVersion {
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $map): self { static function fromJson(object $map): self {
return new static([ return new static(isset($map->version) && is_string($map->version) ? $map->version : '');
'version' => isset($map->version) && is_string($map->version) ? $map->version : '' }
]);
/**
* Gets the version number.
* @return string The version number.
*/
function getVersion(): string {
return $this->version;
}
/**
* 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) ['version' => $this->getVersion()];
} }
} }

View File

@ -5,27 +5,37 @@ namespace Gitea\Models;
/** /**
* Represents a team in an organization. * Represents a team in an organization.
*/ */
class Team { class Team implements \JsonSerializable {
/** /**
* @var string The team description. * @var string The team description.
*/ */
public $description = ''; private $description = '';
/** /**
* @var int The team identifier. * @var int The team identifier.
*/ */
public $id = -1; private $id;
/** /**
* @var string The team name. * @var string The team name.
*/ */
public $name = ''; private $name;
/** /**
* @var string The team permission. * @var string The team permission.
*/ */
public $permission = TeamPermission::NONE; private $permission = TeamPermission::NONE;
/**
* Creates a new team.
* @param int $id The team identifier.
* @param string $name The team name.
*/
function __construct(int $id, string $name = '') {
$this->id = $id;
$this->setName($name);
}
/** /**
* Creates a new user from the specified JSON map. * Creates a new user from the specified JSON map.
@ -33,11 +43,83 @@ class Team {
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $map): self { static function fromJson(object $map): self {
return new static([ return (new static(isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->name) && is_string($map->name) ? $map->name : ''))
'description' => isset($map->description) && is_string($map->description) ? $map->description : '', ->setDescription(isset($map->description) && is_string($map->description) ? $map->description : '')
'id' => isset($map->id) && is_int($map->id) ? $map->id : -1, ->setPermission(isset($map->permission) && is_string($map->permission) ? $map->permission : TeamPermission::NONE);
'name' => isset($map->name) && is_string($map->name) ? $map->name : '', }
'permission' => isset($map->permission) && TeamPermission::isDefined($map->permission) ? $map->permission : TeamPermission::NONE
]); /**
* Gets the team description.
* @return string The team description.
*/
function getDescription(): string {
return $this->description;
}
/**
* Gets the team identifier.
* @return int The team identifier.
*/
function getId(): int {
return $this->id;
}
/**
* Gets the team name.
* @return string The team name.
*/
function getName(): string {
return $this->name;
}
/**
* Gets the team permission.
* @return string The team permission.
*/
function getPermission(): string {
return $this->permission;
}
/**
* 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) [
'description' => $this->getDescription(),
'id' => $this->getId(),
'name' => $this->getName(),
'permission' => $this->getPermission()
];
}
/**
* Sets the team description.
* @param string $value The new description.
* @return $this This instance.
*/
function setDescription(string $value): self {
$this->description = $value;
return $this;
}
/**
* Sets the team name.
* @param string $value The new name.
* @return $this This instance.
*/
function setName(string $value): self {
$this->name = $value;
return $this;
}
/**
* Sets the team permission.
* @param string $value The new permission.
* @return $this This instance.
*/
function setPermission(string $value): self {
$this->permission = TeamPermission::isDefined($value) ? $value : TeamPermission::NONE;
return $this;
} }
} }

View File

@ -28,7 +28,7 @@ class User implements \JsonSerializable {
/** /**
* @var int The user identifier. * @var int The user identifier.
*/ */
private $id = -1; private $id;
/** /**
* @var string The user locale. * @var string The user locale.
@ -38,7 +38,17 @@ class User implements \JsonSerializable {
/** /**
* @var string The name of the Gitea account. * @var string The name of the Gitea account.
*/ */
private $login = ''; private $login;
/**
* Creates a new user.
* @param int $id The user identifier.
* @param string $login The name of the Gitea account.
*/
function __construct(int $id, string $login) {
$this->id = $id;
$this->setLogin($login);
}
/** /**
* Creates a new user from the specified JSON map. * Creates a new user from the specified JSON map.
@ -46,14 +56,11 @@ class User implements \JsonSerializable {
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $map): self { static function fromJson(object $map): self {
return new static([ return (new static(isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->login) && is_string($map->login) ? $map->login : ''))
'avatarUrl' => isset($map->avatar_url) && is_string($map->avatar_url) ? $map->avatar_url : null, ->setAvatarUrl(isset($map->avatar_url) && is_string($map->avatar_url) ? $map->avatar_url : null)
'email' => isset($map->email) && is_string($map->email) ? mb_strtolower($map->email) : '', ->setEmail(isset($map->email) && is_string($map->email) ? mb_strtolower($map->email) : '')
'fullName' => isset($map->full_name) && is_string($map->full_name) ? $map->full_name : '', ->setFullName(isset($map->full_name) && is_string($map->full_name) ? $map->full_name : '')
'id' => isset($map->id) && is_int($map->id) ? $map->id : -1, ->setLanguage(isset($map->language) && is_string($map->language) ? $map->language : '');
'language' => isset($map->language) && is_string($map->language) ? $map->language : '',
'login' => isset($map->login) && is_string($map->login) ? $map->login : ''
]);
} }
/** /**
@ -149,16 +156,6 @@ class User implements \JsonSerializable {
return $this; 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. * Sets the user locale.
* @param string $value The new user locale. * @param string $value The new user locale.