Added the payload models

This commit is contained in:
Cédric Belin
2018-11-01 23:28:31 +01:00
parent cadb3a774c
commit 55b0fa7951
3 changed files with 249 additions and 0 deletions

View File

@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
namespace yii\gitea\models;
use GuzzleHttp\Psr7\{Uri};
use Psr\Http\Message\{UriInterface};
use yii\base\{Model};
/**
* Represents a commit.
* @property \DateTime|null $timestamp The commit date.
* @property UriInterface|null $url The URL to the commit's history.
*/
class PayloadCommit extends Model {
/**
* @var PayloadUser|null The person who authored the commit.
*/
public $author;
/**
* @var PayloadUser|null The person who committed the code.
*/
public $committer;
/**
* @var string The commit hash.
*/
public $id = '';
/**
* @var string The commit message.
*/
public $message = '';
/**
* @var PayloadCommitVerification|null The GPG verification of this commit.
*/
public $verification;
/**
* @var \DateTime|null The commit date.
*/
private $timestamp;
/**
* @var UriInterface|null The URL to the commit's history.
*/
private $url;
/**
* Creates a new commit from the specified JSON map.
* @param object $map A JSON map representing a commit.
* @return static The instance corresponding to the specified JSON map.
*/
static function fromJson(object $map): self {
return new static([
'author' => 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,
'id' => isset($map->id) && is_string($map->id) ? $map->id : '',
'message' => isset($map->message) && is_string($map->message) ? $map->message : '',
'timestamp' => isset($map->timestamp) && is_string($map->timestamp) ? $map->timestamp : 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
]);
}
/**
* 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.
*/
function getTimestamp(): ?\DateTime {
return $this->timestamp;
}
/**
* Gets the URL to the commit's history.
* @return UriInterface|null The URL to the commit's history.
*/
function getUrl(): ?UriInterface {
return $this->url;
}
/**
* Returns the validation rules for attributes.
* @return array[] The validation rules.
*/
function rules(): array {
return [
[['id', 'message'], 'trim']
];
}
/**
* Sets the commit date.
* @param \DateTime|string|null $value The new commit date.
* @return $this This instance.
*/
function setTimestamp($value): self {
$this->timestamp = is_string($value) ? new \DateTime($value) : $value;
return $this;
}
/**
* Sets the URL to the commit's history.
* @param UriInterface|string|null $value The new commit URL.
* @return $this This instance.
*/
function setUrl($value): self {
$this->url = is_string($value) ? new Uri($value) : $value;
return $this;
}
}

View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace yii\gitea\models;
use yii\base\{Model};
/**
* Represents the GPG verification of a commit.
*/
class PayloadCommitVerification extends Model {
/**
* @var bool Value indicating whether the verification has succeeded.
*/
public $isVerified = false;
/**
* @var string A custom message sent with the verification request.
*/
public $payload = '';
/**
* @var string A message providing details about the verification.
*/
public $reason = '';
/**
* @var string The signing key used for the verification.
*/
public $signature = '';
/**
* Creates a new commit from the specified JSON map.
* @param object $map A JSON map representing a commit.
* @return static The instance corresponding to the specified JSON map.
*/
static function fromJson(object $map): self {
return new static([
'isVerified' => isset($map->verified) && is_bool($map->verified) ? $map->verified : false,
'payload' => isset($map->payload) && is_string($map->payload) ? $map->payload : '',
'reason' => isset($map->reason) && is_string($map->reason) ? $map->reason : '',
'signature' => isset($map->signature) && is_string($map->signature) ? $map->signature : ''
]);
}
/**
* 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 [
'payload',
'reason',
'signature',
'verified' => 'isVerified'
];
}
/**
* Returns the validation rules for attributes.
* @return array[] The validation rules.
*/
function rules(): array {
return [
[['payload', 'reason', 'signature'], 'trim'],
['isVerified', 'boolean', 'falseValue' => false, 'trueValue' => true]
];
}
}

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace yii\gitea\models;
use yii\base\{Model};
/**
* Represents the author or committer of a commit.
*/
class PayloadUser extends Model {
/**
* @var string The mail address.
*/
public $email = '';
/**
* @var string The full name.
*/
public $name = '';
/**
* @var string The name of the Gitea account.
*/
public $username = '';
/**
* 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([
'email' => isset($map->email) && is_string($map->email) ? mb_strtolower($map->email) : '',
'name' => isset($map->name) && is_string($map->name) ? $map->name : '',
'username' => isset($map->username) && is_string($map->username) ? $map->username : ''
]);
}
/**
* Returns the validation rules for attributes.
* @return array[] The validation rules.
*/
function rules(): array {
return [
[['email', 'name', 'username'], 'trim'],
['email', 'filter', 'filter' => 'mb_strtolower'],
['email', 'email']
];
}
}