mirror of
https://github.com/sitelease/sugar-cube-client.git
synced 2025-10-29 11:02:30 +01:00
Created three new model classes
+ Created a Branch model class for storing branch data + Created a Tag model class for storing tag data + Created an Owner model class for storing owner data for branches
This commit is contained in:
135
src/Model/Branch.php
Normal file
135
src/Model/Branch.php
Normal file
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Model;
|
||||
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
|
||||
use Gitea\Model\PayloadCommit;
|
||||
|
||||
use \InvalidArgumentException;
|
||||
|
||||
use Gitea\Model\Abstracts\AbstractApiModel;
|
||||
|
||||
/** Represents a Gitea branch. */
|
||||
class Branch extends AbstractApiModel {
|
||||
|
||||
/** @var string The branch's name */
|
||||
private $name = '';
|
||||
|
||||
/** @var PayloadCommit|null The commit connected to this branch */
|
||||
private $commit;
|
||||
|
||||
/** @var string True if the branch is protected */
|
||||
private $protected = true;
|
||||
|
||||
/** @var string True if the user can push to this branch */
|
||||
private $userCanPush = false;
|
||||
|
||||
/** @var string True if the user can merge this branch */
|
||||
private $userCanMerge = false;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new branch
|
||||
* @param object $giteaClient The Gitea client that originally made the request for this object's data
|
||||
* @param object $apiRequester The Api requester that created this object
|
||||
* @param string $name The branch name
|
||||
*/
|
||||
function __construct(object $giteaClient, object $apiRequester, ...$args) {
|
||||
$this->setGiteaClient($giteaClient);
|
||||
$this->setApiRequester($apiRequester);
|
||||
if (count($args) >= 1) {
|
||||
$name = $args[0];
|
||||
if (!is_string($name)) {
|
||||
$argumentType = gettype($name);
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires the 4th parameter to be of the string type, but a \"$argumentType\" was passed in");
|
||||
}
|
||||
$this->setName($name);
|
||||
} else {
|
||||
$numArgs = func_num_args();
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires 4 parameters but only $numArgs were passed in");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new branch from the specified JSON map.
|
||||
* @param object $giteaClient The Gitea client that originally made the request for this object's data
|
||||
* @param object $apiRequester The Api requester that created this object
|
||||
* @param object $map A JSON map representing an branch.
|
||||
* @return static The instance corresponding to the specified JSON map.
|
||||
*/
|
||||
static function fromJson(object $giteaClient, object $apiRequester, object $map): self {
|
||||
return (
|
||||
new static(
|
||||
$giteaClient,
|
||||
$apiRequester,
|
||||
isset($map->name) && is_string($map->name) ? $map->name : ''
|
||||
)
|
||||
)
|
||||
->setCommit(isset($map->commit) && is_object($map->commit) ? PayloadCommit::fromJson($giteaClient, $apiRequester, $map->commit) : null)
|
||||
->setProtected(isset($map->protected) && is_bool($map->protected) ? $map->protected : true)
|
||||
->setCanUserPush(isset($map->user_can_push) && is_bool($map->user_can_push) ? $map->user_can_push : false)
|
||||
->setUserCanMerge(isset($map->user_can_merge) && is_bool($map->user_can_merge) ? $map->user_can_merge : false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) [
|
||||
'name' => $this->getName(),
|
||||
'commit' => ($commit = $this->getCommit()) ? $commit->jsonSerialize() : null,
|
||||
'protected' => $this->getProtected(),
|
||||
'user_can_push' => $this->getCanUserPush(),
|
||||
'user_can_merge' => $this->getUserCanMerge()
|
||||
];
|
||||
}
|
||||
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): self {
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCommit(): ?PayloadCommit {
|
||||
return $this->commit;
|
||||
}
|
||||
|
||||
public function setCommit(?PayloadCommit $object): self {
|
||||
$this->commit = $object;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getProtected() {
|
||||
return $this->protected;
|
||||
}
|
||||
|
||||
public function setProtected(bool $boolean): self {
|
||||
$this->protected = $boolean;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCanUserPush() {
|
||||
return $this->canUserPush;
|
||||
}
|
||||
|
||||
public function setCanUserPush(bool $boolean): self {
|
||||
$this->canUserPush = $boolean;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUserCanMerge() {
|
||||
return $this->userCanMerge;
|
||||
}
|
||||
|
||||
public function setUserCanMerge(bool $boolean): self {
|
||||
$this->userCanMerge = $boolean;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
223
src/Model/Owner.php
Normal file
223
src/Model/Owner.php
Normal file
@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Model;
|
||||
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
|
||||
use Gitea\Model\Abstracts\AbstractApiModel;
|
||||
|
||||
/** Represents a Gitea owner. */
|
||||
class Owner extends AbstractApiModel {
|
||||
|
||||
/** @var int The owner identifier. */
|
||||
private $id;
|
||||
|
||||
/** @var string The name of the Gitea account. */
|
||||
private $login;
|
||||
|
||||
/** @var string The full name. */
|
||||
private $fullName = '';
|
||||
|
||||
/** @var string The mail address. */
|
||||
private $email = '';
|
||||
|
||||
/** @var UriInterface|null The URL to the owner's avatar. */
|
||||
private $avatarUrl;
|
||||
|
||||
/** @var string The owner locale. */
|
||||
private $language = '';
|
||||
|
||||
/** @var boolean If the owner is an admin */
|
||||
private $isAdmin = false;
|
||||
|
||||
/** @var string The username of the account or organization */
|
||||
private $username = '';
|
||||
|
||||
/**
|
||||
* Creates a new owner.
|
||||
* @param object $giteaClient The Gitea client that originally made the request for this object's data
|
||||
* @param object $apiRequester The Api requester that created this object
|
||||
* @param int $id The owner identifier.
|
||||
* @param string $login The name of the Gitea account.
|
||||
*/
|
||||
function __construct(object $giteaClient, object $apiRequester, ...$args) {
|
||||
$this->setGiteaClient($giteaClient);
|
||||
$this->setApiRequester($apiRequester);
|
||||
if (count($args) >= 2) {
|
||||
$id = $args[0];
|
||||
$login = $args[1];
|
||||
if (!is_int($id)) {
|
||||
$argumentType = gettype($id);
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires the 3rd parameter to be of the integer type, but a \"$argumentType\" was passed in");
|
||||
}
|
||||
if (!is_string($login)) {
|
||||
$argumentType = gettype($login);
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires the 4th parameter to be of the string type, but a \"$argumentType\" was passed in");
|
||||
}
|
||||
$this->id = $id;
|
||||
$this->setLogin($login);
|
||||
} else {
|
||||
$numArgs = func_num_args();
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires 4 parameters but only $numArgs were passed in");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new owner from the specified JSON map.
|
||||
* @param object $giteaClient The Gitea client that originally made the request for this object's data
|
||||
* @param object $apiRequester The Api requester that created this object
|
||||
* @param object $map A JSON map representing a owner.
|
||||
* @return static The instance corresponding to the specified JSON map.
|
||||
*/
|
||||
static function fromJson(object $giteaClient, object $apiRequester, object $map): self {
|
||||
return (
|
||||
new static(
|
||||
$giteaClient,
|
||||
$apiRequester,
|
||||
isset($map->id) && is_int($map->id) ? $map->id : -1,
|
||||
isset($map->login) && is_string($map->login) ? $map->login : ''
|
||||
)
|
||||
)
|
||||
->setAvatarUrl(isset($map->avatar_url) && is_string($map->avatar_url) ? new Uri($map->avatar_url) : null)
|
||||
->setEmail(isset($map->email) && is_string($map->email) ? mb_strtolower($map->email) : '')
|
||||
->setFullName(isset($map->full_name) && is_string($map->full_name) ? $map->full_name : '')
|
||||
->setLanguage(isset($map->language) && is_string($map->language) ? $map->language : '')
|
||||
->setIsAdmin(isset($map->is_admin) && is_bool($map->is_admin) ? $map->is_admin : false)
|
||||
->setUsername(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) [
|
||||
'id' => $this->getId(),
|
||||
'login' => $this->getLogin(),
|
||||
'full_name' => $this->getFullName(),
|
||||
'email' => $this->getEmail(),
|
||||
'avatar_url' => ($url = $this->getAvatarUrl()) ? (string) $url : null,
|
||||
'language' => $this->getLanguage(),
|
||||
'is_admin' => $this->getIsAdmin(),
|
||||
'username' => $this->getUsername()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the owner identifier.
|
||||
* @return int The owner identifier.
|
||||
*/
|
||||
function getId(): int {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the Gitea account.
|
||||
* @return string The name of the Gitea account.
|
||||
*/
|
||||
function getLogin(): string {
|
||||
return $this->login;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the full name.
|
||||
* @return string The full name.
|
||||
*/
|
||||
function getFullName(): string {
|
||||
return $this->fullName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the mail address.
|
||||
* @return string The mail address.
|
||||
*/
|
||||
function getEmail(): string {
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the URL of the avatar image.
|
||||
* @return UriInterface|null The URL of the avatar image.
|
||||
*/
|
||||
function getAvatarUrl(): ?UriInterface {
|
||||
return $this->avatarUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the URL of the avatar image.
|
||||
* @param UriInterface|null $value The new avatar URL.
|
||||
* @return $this This instance.
|
||||
*/
|
||||
function setAvatarUrl(?UriInterface $value): self {
|
||||
$this->avatarUrl = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the owner locale.
|
||||
* @return string The owner locale.
|
||||
*/
|
||||
function getLanguage(): string {
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the owner locale.
|
||||
* @param string $value The new owner locale.
|
||||
* @return $this This instance.
|
||||
*/
|
||||
function setLanguage(string $value): self {
|
||||
$this->language = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIsAdmin(): boolean {
|
||||
return $this->isAdmin;
|
||||
}
|
||||
|
||||
public function setIsAdmin($boolean): self {
|
||||
$this->isAdmin = $boolean;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUsername(): string {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function setUsername($string): self {
|
||||
$this->username = $string;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
157
src/Model/Tag.php
Normal file
157
src/Model/Tag.php
Normal file
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace Gitea\Model;
|
||||
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
|
||||
use \InvalidArgumentException;
|
||||
|
||||
use Gitea\Model\Abstracts\AbstractApiModel;
|
||||
|
||||
/** Represents a Gitea tag. */
|
||||
class Tag extends AbstractApiModel {
|
||||
|
||||
/** @var int The tag identifier. */
|
||||
private $id = -1;
|
||||
|
||||
/** @var string The tag's name. */
|
||||
private $name = '';
|
||||
|
||||
/** @var string The tarball URL for the tag */
|
||||
private $tarballURL;
|
||||
|
||||
/** @var string The zipball URL for the tag */
|
||||
private $zipballURL;
|
||||
|
||||
/** @var int The commit information for the tag */
|
||||
private $commit = [
|
||||
"sha" => "",
|
||||
"url" => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* Creates a new tag
|
||||
* @param object $giteaClient The Gitea client that originally made the request for this object's data
|
||||
* @param object $apiRequester The Api requester that created this object
|
||||
* @param int $id The tag identifier
|
||||
* @param string $name The tag name
|
||||
*/
|
||||
function __construct(object $giteaClient, object $apiRequester, ...$args) {
|
||||
$this->setGiteaClient($giteaClient);
|
||||
$this->setApiRequester($apiRequester);
|
||||
if (count($args) >= 2) {
|
||||
$id = $args[0];
|
||||
$name = $args[1];
|
||||
if (!is_int($id)) {
|
||||
$argumentType = gettype($id);
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires the 3rd parameter to be of the integer type, but a \"$argumentType\" was passed in");
|
||||
}
|
||||
if (!is_string($name)) {
|
||||
$argumentType = gettype($name);
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires the 4th parameter to be of the string type, but a \"$argumentType\" was passed in");
|
||||
}
|
||||
$this->id = $id;
|
||||
$this->setName($name);
|
||||
} else {
|
||||
$numArgs = func_num_args();
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires 4 parameters but only $numArgs were passed in");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new tag from the specified JSON map.
|
||||
* @param object $giteaClient The Gitea client that originally made the request for this object's data
|
||||
* @param object $apiRequester The Api requester that created this object
|
||||
* @param object $map A JSON map representing an tag.
|
||||
* @return static The instance corresponding to the specified JSON map.
|
||||
*/
|
||||
static function fromJson(object $giteaClient, object $apiRequester, object $map): self {
|
||||
$newTag = new static(
|
||||
$giteaClient,
|
||||
$apiRequester,
|
||||
isset($map->id) && is_int($map->id) ? $map->id : -1,
|
||||
isset($map->name) && is_string($map->name) ? $map->name : ''
|
||||
);
|
||||
$newTag->setTarballURL(isset($map->tarball_url) && is_string($map->tarball_url) ? new Uri($map->tarball_url) : null);
|
||||
$newTag->setZipballURL(isset($map->zipball_url) && is_string($map->zipball_url) ? new Uri($map->zipball_url) : null);
|
||||
if (isset($map->commit)) {
|
||||
$newTag->setCommitSha(isset($map->commit->sha) && is_string($map->commit->sha) ? $map->commit->sha : "");
|
||||
$newTag->setCommitUrl(isset($map->commit->url) && is_string($map->commit->url) ? new Uri($map->commit->url) : null);
|
||||
}
|
||||
return $newTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) [
|
||||
'id' => $this->getId(),
|
||||
'name' => $this->getName(),
|
||||
'tarball_url' => ($url = $this->getTarballURL()) ? (string) $url : null,
|
||||
'zipball_url' => ($url = $this->getZipballURL()) ? (string) $url : null,
|
||||
'commit' => [
|
||||
"sha" => $this->getCommitSha(),
|
||||
"url" => ($url = $this->getCommitUrl()) ? (string) $url : null
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tag identifier.
|
||||
* @return int The tag identifier.
|
||||
*/
|
||||
function getId(): int {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name): self {
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTarballURL(): ?UriInterface {
|
||||
return $this->tarballURL;
|
||||
}
|
||||
|
||||
public function setTarballURL(?UriInterface $url): self {
|
||||
$this->tarballURL = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getZipballURL(): ?UriInterface {
|
||||
return $this->zipballURL;
|
||||
}
|
||||
|
||||
public function setZipballURL(?UriInterface $url): self {
|
||||
$this->zipballURL = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCommitSha(): string {
|
||||
$commit = $this->commit;
|
||||
return $commit["sha"];
|
||||
}
|
||||
|
||||
public function setCommitSha(string $string): self {
|
||||
$this->commit["sha"] = $string;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCommitUrl(): ?uri {
|
||||
$commit = $this->commit;
|
||||
return $commit["url"];
|
||||
}
|
||||
|
||||
public function setCommitUrl(?uri $url): self {
|
||||
$this->commit["url"] = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user