From 55b0fa7951686c027ba33bb6b2115399f9dcedcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Thu, 1 Nov 2018 23:28:31 +0100 Subject: [PATCH] Added the payload models --- lib/models/PayloadCommit.php | 129 +++++++++++++++++++++++ lib/models/PayloadCommitVerification.php | 69 ++++++++++++ lib/models/PayloadUser.php | 51 +++++++++ 3 files changed, 249 insertions(+) create mode 100644 lib/models/PayloadCommit.php create mode 100644 lib/models/PayloadCommitVerification.php create mode 100644 lib/models/PayloadUser.php diff --git a/lib/models/PayloadCommit.php b/lib/models/PayloadCommit.php new file mode 100644 index 0000000..a2bc964 --- /dev/null +++ b/lib/models/PayloadCommit.php @@ -0,0 +1,129 @@ + 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; + } +} diff --git a/lib/models/PayloadCommitVerification.php b/lib/models/PayloadCommitVerification.php new file mode 100644 index 0000000..6cfa581 --- /dev/null +++ b/lib/models/PayloadCommitVerification.php @@ -0,0 +1,69 @@ + 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] + ]; + } +} diff --git a/lib/models/PayloadUser.php b/lib/models/PayloadUser.php new file mode 100644 index 0000000..5b9c26b --- /dev/null +++ b/lib/models/PayloadUser.php @@ -0,0 +1,51 @@ + 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'] + ]; + } +}