From 61625a700e0527295d31d9cfa36f7899634a0519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Fri, 2 Nov 2018 00:04:06 +0100 Subject: [PATCH] Added the `PushEvent` class --- lib/PushEvent.php | 140 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 lib/PushEvent.php diff --git a/lib/PushEvent.php b/lib/PushEvent.php new file mode 100644 index 0000000..5bf98ca --- /dev/null +++ b/lib/PushEvent.php @@ -0,0 +1,140 @@ +commits = new \ArrayObject; + } + + /** + * Creates a new event from the specified JSON map. + * @param object $map A JSON map representing an event. + * @return static The instance corresponding to the specified JSON map. + */ + static function fromJson(object $map): self { + return new static([ + 'after' => isset($map->after) && is_string($map->after) ? $map->after : '', + 'before' => isset($map->before) && is_string($map->before) ? $map->before : '', + 'compareUrl' => isset($map->compare_url) && is_string($map->compare_url) ? new Uri($map->compare_url) : null, + 'commits' => isset($map->commits) && is_array($map->commits) ? array_map([PayloadCommit::class, 'fromJson'], $map->commits) : [], + 'pusher' => isset($map->pusher) && is_object($map->pusher) ? User::fromJson($map->pusher) : null, + 'ref' => isset($map->ref) && is_string($map->ref) ? $map->ref : '', + 'repository' => isset($map->repository) && is_object($map->repository) ? Repository::fromJson($map->repository) : null, + 'secret' => isset($map->secret) && is_string($map->secret) ? $map->secret : '', + 'sender' => isset($map->sender) && is_object($map->sender) ? User::fromJson($map->sender) : 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 [ + 'after', + 'before', + 'compare_url' => function(self $model) { return ($url = $model->getCompareUrl()) ? (string) $url : null; }, + 'commits', + 'pusher', + 'ref', + 'repository', + 'sender' + ]; + } + + /** + * Gets the revision commits. + * @return \ArrayObject The revision commits. + */ + function getCommits(): \ArrayObject { + return $this->commits; + } + + /** + * Gets the URL for comparing the revisions. + * @return UriInterface|null The URL for comparing the revisions. + */ + function getCompareUrl(): ?UriInterface { + return $this->compareUrl; + } + + /** + * Sets the revision commits. + * @param PayloadCommit[] $values The revision commits. + * @return $this This instance. + */ + function setCommits(array $values): self { + $this->getCommits()->exchangeArray($values); + return $this; + } + + /** + * Sets the URL for comparing the revisions. + * @param UriInterface|string|null $value The URL for comparing the revisions. + * @return $this This instance. + */ + function setCompareUrl($value): self { + $this->compareUrl = is_string($value) ? new Uri($value) : $value; + return $this; + } +}