Added new models

This commit is contained in:
Cédric Belin
2018-11-02 00:04:16 +01:00
parent 61625a700e
commit a5f84778cd
5 changed files with 503 additions and 0 deletions

306
lib/models/Repository.php Normal file
View File

@ -0,0 +1,306 @@
<?php
declare(strict_types=1);
namespace Gitea\Models;
use GuzzleHttp\Psr7\{Uri};
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 {
/**
* @var string The name of the default branch.
*/
public $defaultBranch = '';
/**
* @var string The repository description.
*/
public $description = '';
/**
* @var int The number of forks of this repository.
*/
public $forksCount = 0;
/**
* @var string The full name.
*/
public $fullName = '';
/**
* @var int The repository identifier.
*/
public $id = -1;
/**
* @var bool Value indicating whether this repository is empty.
*/
public $isEmpty = false;
/**
* @var bool Value indicating whether this repository is a fork.
*/
public $isFork = false;
/**
* @var bool Value indicating whether this repository is a mirror.
*/
public $isMirror = false;
/**
* @var bool Value indicating whether this repository is private.
*/
public $isPrivate = false;
/**
* @var string The repository name.
*/
public $name = '';
/**
* @var int The number of open issues of this repository.
*/
public $openIssuesCount = 0;
/**
* @var User|null The repository owner.
*/
public $owner;
/**
* @var Repository|null The parent repository, if this repository is a fork or a mirror.
*/
public $parent;
/**
* @var Permission|null The repository permissions.
*/
public $permissions;
/**
* @var int The repository size, in kilobytes.
*/
public $size = 0;
/**
* @var int The number of stars of this repository.
*/
public $starsCount = 0;
/**
* @var int The number of watchers of this repository.
*/
public $watchersCount = 0;
/**
* @var UriInterface|null The HTTP-based URL for cloning this repository.
*/
private $cloneUrl;
/**
* @var \DateTime|null The date the repository was created.
*/
private $createdAt;
/**
* @var UriInterface|null The Gitea URL of this repository.
*/
private $htmlUrl;
/**
* @var UriInterface|null The SSH-based URL for cloning this repository.
*/
private $sshUrl;
/**
* @var \DateTime|null The date the repository was updated.
*/
private $updatedAt;
/**
* @var UriInterface|null The URL of the repository website.
*/
private $website;
/**
* Creates a new repository from the specified JSON map.
* @param object $map A JSON map representing a repository.
* @return static The instance corresponding to the specified JSON map.
*/
static function fromJson(object $map): self {
return new static([
'cloneUrl' => isset($map->clone_url) && is_string($map->clone_url) ? new Uri($map->clone_url) : null,
'createdAt' => isset($map->created_at) && is_string($map->created_at) ? new \DateTime($map->created_at) : null,
'defaultBranch' => isset($map->default_branch) && is_string($map->default_branch) ? $map->default_branch : '',
'description' => isset($map->description) && is_string($map->description) ? $map->description : '',
'forksCount' => isset($map->forks_count) && is_int($map->forks_count) ? $map->forks_count : 0,
'fullName' => isset($map->full_name) && is_string($map->full_name) ? $map->full_name : '',
'htmlUrl' => isset($map->html_url) && is_string($map->html_url) ? new Uri($map->html_url) : null,
'id' => isset($map->id) && is_int($map->id) ? $map->id : -1,
'isEmpty' => isset($map->empty) && is_bool($map->empty) ? $map->empty : false,
'isFork' => isset($map->fork) && is_bool($map->fork) ? $map->fork : false,
'isMirror' => isset($map->mirror) && is_bool($map->mirror) ? $map->mirror : false,
'isPrivate' => isset($map->private) && is_bool($map->private) ? $map->private : false,
'name' => isset($map->name) && is_string($map->name) ? $map->name : '',
'openIssuesCount' => isset($map->open_issues_count) && is_int($map->open_issues_count) ? $map->open_issues_count : 0,
'owner' => isset($map->owner) && is_object($map->owner) ? User::fromJson($map->owner) : null,
'parent' => isset($map->parent) && is_object($map->parent) ? Repository::fromJson($map->parent) : null,
'permissions' => isset($map->permissions) && is_object($map->permissions) ? Permission::fromJson($map->permissions) : null,
'size' => isset($map->size) && is_int($map->size) ? $map->size : 0,
'sshUrl' => isset($map->ssh_url) && is_string($map->ssh_url) ? new Uri($map->ssh_url) : null,
'starsCount' => isset($map->stars_count) && is_int($map->stars_count) ? $map->stars_count : 0,
'updatedAt' => isset($map->updated_at) && is_string($map->updated_at) ? new \DateTime($map->updated_at) : null,
'watchersCount' => isset($map->watchers_count) && is_int($map->watchers_count) ? $map->watchers_count : 0,
'website' => isset($map->website) && is_string($map->website) ? new Uri($map->website) : null,
]);
}
/**
* 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.
*/
function getCloneUrl(): ?UriInterface {
return $this->cloneUrl;
}
/**
* Gets the date the repository was created.
* @return \DateTime|null The date the repository was created.
*/
function getCreatedAt(): ?\DateTime {
return $this->createdAt;
}
/**
* Gets the Gitea URL of this repository.
* @return UriInterface|null The Gitea URL of this repository.
*/
function getHtmlUrl(): ?UriInterface {
return $this->htmlUrl;
}
/**
* Gets the SSH-based URL for cloning this repository.
* @return UriInterface|null The SSH-based URL for cloning this repository.
*/
function getSshUrl(): ?UriInterface {
return $this->sshUrl;
}
/**
* Gets the date the repository was updated.
* @return \DateTime|null The date the repository was updated.
*/
function getUpdatedAt(): ?\DateTime {
return $this->updatedAt;
}
/**
* Gets the URL of the repository website.
* @return UriInterface|null The URL of the repository website.
*/
function getWebsite(): ?UriInterface {
return $this->website;
}
/**
* Sets the HTTP-based URL for cloning this repository.
* @param UriInterface|string|null $value The new URL for cloning this repository.
* @return $this This instance.
*/
function setCloneUrl($value): self {
$this->cloneUrl = is_string($value) ? new Uri($value) : $value;
return $this;
}
/**
* Sets the date the repository 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 Gitea URL of this repository.
* @param UriInterface|string|null $value The new Gitea URL.
* @return $this This instance.
*/
function setHtmlUrl($value): self {
$this->htmlUrl = is_string($value) ? new Uri($value) : $value;
return $this;
}
/**
* Sets the SSH-based URL for cloning this repository.
* @param UriInterface|string|null $value The new URL for cloning this repository.
* @return $this This instance.
*/
function setSshlUrl($value): self {
$this->sshUrl = is_string($value) ? new Uri($value) : $value;
return $this;
}
/**
* Sets the date the repository was updated.
* @param \DateTime|string|null $value The new date of update.
* @return $this This instance.
*/
function setUpdatedAt($value): self {
$this->updatedAt = is_string($value) ? new \DateTime($value) : $value;
return $this;
}
/**
* Sets the URL of the repository website.
* @param UriInterface|string|null $value The new repository website.
* @return $this This instance.
*/
function setWebsite($value): self {
$this->website = is_string($value) ? new Uri($value) : $value;
return $this;
}
}

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Gitea\Models;
/**
* Warps the version of the Gitea server.
*/
class ServerVersion {
/**
* @var string The version number.
*/
public $version = '';
/**
* Creates a new server version from the specified JSON map.
* @param object $map A JSON map representing a server version.
* @return static The instance corresponding to the specified JSON map.
*/
static function fromJson(object $map): self {
return new static([
'version' => isset($map->version) && is_string($map->version) ? $map->version : ''
]);
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Gitea\Models;
use Enum\{EnumTrait};
/**
* Defines the state of a Gitea status.
*/
final class StatusState {
use EnumTrait;
/**
* @var string The status is an error.
*/
const ERROR = 'error';
/**
* @var string The status is a failure.
*/
const FAILURE = 'failure';
/**
* @var string The status is a pending.
*/
const PENDING = 'pending';
/**
* @var string The status is a success.
*/
const SUCCESS = 'success';
/**
* @var string The status is a warning.
*/
const WARNING = 'warning';
}

43
lib/models/Team.php Normal file
View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Gitea\Models;
/**
* Represents a team in an organization.
*/
class Team {
/**
* @var string The team description.
*/
public $description = '';
/**
* @var int The team identifier.
*/
public $id = -1;
/**
* @var string The team name.
*/
public $name = '';
/**
* @var string The team permission.
*/
public $permission = TeamPermission::NONE;
/**
* Creates a new user from the specified JSON map.
* @param object $map A JSON map representing a user.
* @return static The instance corresponding to the specified JSON map.
*/
static function fromJson(object $map): self {
return new static([
'description' => isset($map->description) && is_string($map->description) ? $map->description : '',
'id' => isset($map->id) && is_int($map->id) ? $map->id : -1,
'name' => isset($map->name) && is_string($map->name) ? $map->name : '',
'permission' => isset($map->permission) && TeamPermission::isDefined($map->permission) ? $map->permission : TeamPermission::NONE
]);
}
}

92
lib/models/User.php Normal file
View File

@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Gitea\Models;
use GuzzleHttp\Psr7\{Uri};
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 = '';
/**
* @var UriInterface|null The URL to the user's avatar.
*/
private $avatarUrl;
/**
* Creates a new user from the specified JSON map.
* @param object $map A JSON map representing a user.
* @return static The instance corresponding to the specified JSON map.
*/
static function fromJson(object $map): self {
return new static([
'avatarUrl' => 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) : '',
'fullName' => isset($map->full_name) && is_string($map->full_name) ? $map->full_name : '',
'id' => isset($map->id) && is_int($map->id) ? $map->id : -1,
'language' => isset($map->language) && is_string($map->language) ? $map->language : '',
'login' => isset($map->login) && is_string($map->login) ? $map->login : ''
]);
}
/**
* 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.
*/
function getAvatarUrl(): ?UriInterface {
return $this->avatarUrl;
}
/**
* Sets the URL of the avatar image.
* @param UriInterface|string|null $value The new avatar URL.
* @return $this This instance.
*/
function setAvatarUrl($value): self {
$this->avatarUrl = is_string($value) ? new Uri($value) : $value;
return $this;
}
}