From a9aa362faedac9f9dea772b3a6e9d9a3278a1748 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Thu, 4 Jun 2020 09:21:06 +0100 Subject: [PATCH 1/2] Add new activities filters --- .../Activities/HasActivitiesInterface.php | 14 ++++++++----- src/Model/Activities/HasActivitiesTrait.php | 20 +++++++++---------- src/Model/ApiResourceBase.php | 8 ++------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Model/Activities/HasActivitiesInterface.php b/src/Model/Activities/HasActivitiesInterface.php index ea79375c..154b5604 100644 --- a/src/Model/Activities/HasActivitiesInterface.php +++ b/src/Model/Activities/HasActivitiesInterface.php @@ -24,14 +24,18 @@ public function getActivity($id); /** * Get a list of activities. * - * @param int $limit - * Limit the number of activities to return. - * @param string $type + * @param int $limit + * Limit the number of activities to return. Zero for no limit. + * @param string|null $type * Filter activities by type. - * @param int $startsAt + * @param int|null $startsAt * A UNIX timestamp for the maximum created date of activities to return. + * @param string|null $state + * Filter activities by state ("pending", "in_progress", "complete"). + * @param string $result + * Filter activities by result ("success" or "failure"). * * @return Activity[] */ - public function getActivities($limit = 0, $type = null, $startsAt = null); + public function getActivities($limit = 0, $type = null, $startsAt = null, $state = null, $result = null); } diff --git a/src/Model/Activities/HasActivitiesTrait.php b/src/Model/Activities/HasActivitiesTrait.php index 1b79af6a..0a38a5c3 100644 --- a/src/Model/Activities/HasActivitiesTrait.php +++ b/src/Model/Activities/HasActivitiesTrait.php @@ -22,7 +22,7 @@ public function getActivity($id) /** * {@inheritDoc} */ - public function getActivities($limit = 0, $type = null, $startsAt = null) + public function getActivities($limit = 0, $type = null, $startsAt = null, $state = null, $result = null) { $options = []; if ($type !== null) { @@ -31,16 +31,16 @@ public function getActivities($limit = 0, $type = null, $startsAt = null) if ($startsAt !== null) { $options['query']['starts_at'] = Activity::formatStartsAt($startsAt); } - - $activities = Activity::getCollection($this->getUri() . '/activities', $limit, $options, $this->client); - - // Guarantee the type filter (works around a temporary bug). - if ($type !== null) { - $activities = array_filter($activities, function (Activity $activity) use ($type) { - return $activity->type === $type; - }); + if (!empty($limit)) { + $options['query']['count'] = $limit; + } + if ($state !== null) { + $options['query']['state'] = $state; + } + if ($result !== null) { + $options['query']['result'] = $result; } - return $activities; + return Activity::getCollection($this->getUri() . '/activities', $limit, $options, $this->client); } } diff --git a/src/Model/ApiResourceBase.php b/src/Model/ApiResourceBase.php index 1c4cf5c2..7be60484 100644 --- a/src/Model/ApiResourceBase.php +++ b/src/Model/ApiResourceBase.php @@ -303,7 +303,7 @@ protected static function checkProperty($property, $value) * * @param string $url The collection URL. * @param int $limit A limit on the number of resources to - * return. Use 0 for no limit. + * return. Use 0 for no limit. Deprecated. * @param array $options An array of additional Guzzle request * options. * @param ClientInterface $client A suitably configured Guzzle client. @@ -312,15 +312,11 @@ protected static function checkProperty($property, $value) */ public static function getCollection($url, $limit, array $options, ClientInterface $client) { - // @todo uncomment this when the API implements a 'count' parameter - // if ($limit) { - // $options['query']['count'] = $limit; - // } $request = new Request('get', $url); $data = self::send($request, $client, $options); // @todo remove this when the API implements a 'count' parameter - if ($limit) { + if (!empty($limit) && count($data) > $limit) { $data = array_slice($data, 0, $limit); } From 1948199e4d995f8fb9ec17283baf3450a92c64f7 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Wed, 10 Jun 2020 15:14:26 +0100 Subject: [PATCH 2/2] Support multiple filters --- .../Activities/HasActivitiesInterface.php | 4 ++-- src/Model/Activities/HasActivitiesTrait.php | 23 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Model/Activities/HasActivitiesInterface.php b/src/Model/Activities/HasActivitiesInterface.php index 154b5604..439c00c4 100644 --- a/src/Model/Activities/HasActivitiesInterface.php +++ b/src/Model/Activities/HasActivitiesInterface.php @@ -30,9 +30,9 @@ public function getActivity($id); * Filter activities by type. * @param int|null $startsAt * A UNIX timestamp for the maximum created date of activities to return. - * @param string|null $state + * @param string|string[]|null $state * Filter activities by state ("pending", "in_progress", "complete"). - * @param string $result + * @param string|string[]|null $result * Filter activities by result ("success" or "failure"). * * @return Activity[] diff --git a/src/Model/Activities/HasActivitiesTrait.php b/src/Model/Activities/HasActivitiesTrait.php index 0a38a5c3..d675ed9d 100644 --- a/src/Model/Activities/HasActivitiesTrait.php +++ b/src/Model/Activities/HasActivitiesTrait.php @@ -24,23 +24,30 @@ public function getActivity($id) */ public function getActivities($limit = 0, $type = null, $startsAt = null, $state = null, $result = null) { - $options = []; + $query = ''; if ($type !== null) { - $options['query']['type'] = $type; + $query .= '&type=' . \rawurlencode($type); } if ($startsAt !== null) { - $options['query']['starts_at'] = Activity::formatStartsAt($startsAt); + $query .= '&starts_at=' . Activity::formatStartsAt($startsAt); } if (!empty($limit)) { - $options['query']['count'] = $limit; + $query .= '&count=' . $limit; + } + if ($result !== null) { + foreach ((array) $result as $resultItem) { + $query .= '&result=' . \rawurlencode($resultItem); + } } if ($state !== null) { - $options['query']['state'] = $state; + foreach ((array) $state as $stateItem) { + $query .= '&state=' . \rawurlencode($stateItem); + } } - if ($result !== null) { - $options['query']['result'] = $result; + if ($query !== '') { + $query = '?' . \substr($query, 1); } - return Activity::getCollection($this->getUri() . '/activities', $limit, $options, $this->client); + return Activity::getCollection($this->getUri() . '/activities' . $query, $limit, [], $this->client); } }