From f542b788fb975c35fef3767f43fd128b32bc46aa Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Mon, 3 Feb 2025 13:57:45 +0100 Subject: [PATCH 1/9] Support for Members and Membership type, status and include objects --- .../Endpoints/Objects/Member/GetMembers.php | 79 ++--- .../Objects/Member/ManageMembers.php | 299 +++++++++++++++++ .../Objects/Member/RemoveMembers.php | 138 ++++---- .../Endpoints/Objects/Member/SetMembers.php | 172 +++++----- .../Objects/Membership/GetMemberships.php | 94 +++--- .../Objects/Membership/ManageMemberships.php | 314 ++++++++++++++++++ .../Objects/Membership/RemoveMemberships.php | 161 +++++---- .../Objects/Membership/SetMemberships.php | 167 ++++++---- src/PubNub/Enums/PNOperationType.php | 3 + .../Objects/Member/PNChannelMember.php | 99 ++++++ .../Consumer/Objects/Member/PNMember.php | 108 ++++-- .../Objects/Member/PNMemberIncludes.php | 55 +++ .../Objects/Member/PNMembersResult.php | 33 +- .../Objects/Member/PNMembersResultItem.php | 77 ++++- .../Membership/PNChannelMembership.php | 100 ++++++ .../Objects/Membership/PNMembership.php | 96 ++++-- .../Membership/PNMembershipIncludes.php | 55 +++ .../Membership/PNMembershipsResult.php | 33 +- .../Membership/PNMembershipsResultItem.php | 69 +++- .../Models/Consumer/Objects/PNIncludes.php | 54 +++ .../Models/Consumer/Objects/PNMemberData.php | 26 -- .../Consumer/Objects/PNMembershipData.php | 26 -- src/PubNub/PubNub.php | 18 + .../objects/member/GetMembersEndpointTest.php | 1 - .../objects/member/MembersHappyPathTest.php | 133 ++++++++ .../member/RemoveMembersEndpointTest.php | 1 - .../objects/member/SetMembersEndpointTest.php | 7 +- .../membership/GetMembershipsEndpointTest.php | 1 - .../membership/MembershipsHappyPathTest.php | 106 ++++++ .../RemoveMembershipsEndpointTest.php | 1 - .../membership/SetMembershipsEndpointTest.php | 7 +- 31 files changed, 1977 insertions(+), 556 deletions(-) create mode 100644 src/PubNub/Endpoints/Objects/Member/ManageMembers.php create mode 100644 src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php create mode 100644 src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php create mode 100644 src/PubNub/Models/Consumer/Objects/Member/PNMemberIncludes.php create mode 100644 src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php create mode 100644 src/PubNub/Models/Consumer/Objects/Membership/PNMembershipIncludes.php create mode 100644 src/PubNub/Models/Consumer/Objects/PNIncludes.php delete mode 100644 src/PubNub/Models/Consumer/Objects/PNMemberData.php delete mode 100644 src/PubNub/Models/Consumer/Objects/PNMembershipData.php create mode 100644 tests/integrational/objects/member/MembersHappyPathTest.php create mode 100644 tests/integrational/objects/membership/MembershipsHappyPathTest.php diff --git a/src/PubNub/Endpoints/Objects/Member/GetMembers.php b/src/PubNub/Endpoints/Objects/Member/GetMembers.php index 923ef1f9..57f37d59 100644 --- a/src/PubNub/Endpoints/Objects/Member/GetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/GetMembers.php @@ -6,18 +6,37 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Models\Consumer\Objects\Member\PNMemberIncludes; use PubNub\Models\Consumer\Objects\Member\PNMembersResult; +use PubNub\PubNub; class GetMembers extends ObjectsCollectionEndpoint { protected const PATH = "/v2/objects/%s/channels/%s/uuids"; + protected bool $endpointAuthRequired = true; + protected string $endpointHttpMethod = PNHttpMethod::GET; + protected int $endpointOperationType = PNOperationType::PNGetMembersOperation; + protected string $endpointName = "GetMembers"; + /** @var string */ protected $channel; /** @var array */ protected $include = []; + protected ?PNMemberIncludes $includes; + + /** + * @param PubNub $pubnubInstance + */ + public function __construct(PubNub $pubnubInstance) + { + parent::__construct($pubnubInstance); + $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + /** * @param string $ch * @return $this @@ -29,6 +48,12 @@ public function channel($ch) return $this; } + public function include(PNMemberIncludes $includes): self + { + $this->includes = $includes; + return $this; + } + /** * @param array $include * @return $this @@ -94,8 +119,10 @@ protected function customParams() { $params = $this->defaultParams(); - if (count($this->include) > 0) { - $includes = []; + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } elseif (count($this->include) > 0) { + $includes = []; if (array_key_exists("customFields", $this->include)) { array_push($includes, 'custom'); @@ -152,52 +179,4 @@ protected function customParams() return $params; } - - /** - * @return bool - */ - protected function isAuthRequired() - { - return true; - } - - /** - * @return int - */ - protected function getRequestTimeout() - { - return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); - } - - /** - * @return int - */ - protected function getConnectTimeout() - { - return $this->pubnub->getConfiguration()->getConnectTimeout(); - } - - /** - * @return string PNHttpMethod - */ - protected function httpMethod() - { - return PNHttpMethod::GET; - } - - /** - * @return int - */ - protected function getOperationType() - { - return PNOperationType::PNGetMembersOperation; - } - - /** - * @return string - */ - protected function getName() - { - return "GetMembers"; - } } diff --git a/src/PubNub/Endpoints/Objects/Member/ManageMembers.php b/src/PubNub/Endpoints/Objects/Member/ManageMembers.php new file mode 100644 index 00000000..15f9f704 --- /dev/null +++ b/src/PubNub/Endpoints/Objects/Member/ManageMembers.php @@ -0,0 +1,299 @@ +endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + /** + * @param string $ch + * @return $this + */ + public function channel($ch) + { + $this->channel = $ch; + + return $this; + } + + /** + * @param string[] $uuids + * @deprecated Use members() method + * + * @return $this + */ + public function setUuids($uuids): self + { + $this->setUuids = $uuids; + + return $this; + } + + /** + * @param string[] $uuids + * @deprecated Use members() method + * + * @return $this + */ + public function removeUuids($uuids): self + { + $this->removeUuids = $uuids; + + return $this; + } + + /** + * @param PNChannelMember[] $setMembers + * @return $this + */ + public function setMembers(array $setMembers) + { + $this->setMembers = $setMembers; + return $this; + } + + /** + * @param PNChannelMember[] $removeMembers + * @return $this + */ + public function removeMembers(array $removeMembers) + { + $this->removeMembers = $removeMembers; + return $this; + } + + /** + * Defines a list of fields to be included in response. It takes an instance of PNMemberIncludes. + * + * Example: + * + * $includes = (new PNMemberIncludes())->custom()->status()->totalCount()->type()-user(); + * $pnGetMembers->include($includes); + * + * @param PNMemberIncludes $includes + * @return $this + */ + public function include(PNMemberIncludes $includes): self + { + $this->includes = $includes; + return $this; + } + + /** + * @param string[] $include + * @deprecated Use include() method + * @return $this + */ + public function includeFields($include) + { + $this->include = $include; + return $this; + } + + /** + * @throws PubNubValidationException + */ + protected function validateParams() + { + $this->validateSubscribeKey(); + + if (!is_string($this->channel)) { + throw new PubNubValidationException("channel missing"); + } + $members = !empty($this->setMembers) or !empty($this->removeMembers); + $uuids = !empty($this->setUuids) or !empty($this->removeUuids); + + if ($members and $uuids) { + throw new PubNubValidationException("Either members or uuids should be provided"); + } + + if (!$members and !$uuids) { + throw new PubNubValidationException("Members or a list of uuids missing"); + } + } + + /** + * @return string + */ + protected function buildData() + { + $set = []; + $remove = []; + if (!empty($this->setMembers)) { + foreach ($this->setMembers as $member) { + array_push($set, $member->toArray()); + } + } else { + foreach ($this->setUuids as $value) { + $entry = [ + "uuid" => [ + "id" => $value, + ] + ]; + array_push($set, $entry); + } + } + + if (!empty($this->removeMembers)) { + foreach ($this->removeMembers as $member) { + array_push($remove, $member->toArray()); + } + } else { + foreach ($this->removeUuids as $value) { + $entry = [ + "uuid" => [ + "id" => $value, + ] + ]; + array_push($remove, $entry); + } + } + return PubNubUtil::writeValueAsString([ + "set" => $set, + "delete" => $remove + ]); + } + + /** + * @return string + */ + protected function buildPath() + { + return sprintf( + static::PATH, + $this->pubnub->getConfiguration()->getSubscribeKey(), + $this->channel + ); + } + + /** + * @param array $result Decoded json + * @return PNMembersResult + */ + protected function createResponse($result): PNMembersResult + { + return PNMembersResult::fromPayload($result); + } + + public function sync(): PNMembersResult + { + return parent::sync(); + } + + /** + * @return array + */ + protected function customParams() + { + $params = $this->defaultParams(); + + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } else { + if (count($this->include) > 0) { + $includes = []; + + if (array_key_exists("customFields", $this->include)) { + array_push($includes, 'custom'); + } + + if (array_key_exists("customUUIDFields", $this->include)) { + array_push($includes, 'uuid.custom'); + } + + if (array_key_exists("UUIDFields", $this->include)) { + array_push($includes, 'uuid'); + } + + $includesString = implode(",", $includes); + + if (strlen($includesString) > 0) { + $params['include'] = $includesString; + } + } + } + + if (array_key_exists("totalCount", $this->include)) { + $params['count'] = "true"; + } + + if (array_key_exists("next", $this->page)) { + $params['start'] = $this->page["next"]; + } + + if (array_key_exists("prev", $this->page)) { + $params['end'] = $this->page["prev"]; + } + + if (!empty($this->filter)) { + $params['filter'] = $this->filter; + } + + if (!empty($this->limit)) { + $params['limit'] = $this->limit; + } + + if (!empty($this->sort)) { + $sortEntries = []; + + foreach ($this->sort as $key => $value) { + if ($value === 'asc' || $value === 'desc') { + array_push($sortEntries, "$key:$value"); + } else { + array_push($sortEntries, $key); + } + } + + $params['sort'] = $sortEntries; + } + + return $params; + } +} diff --git a/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php b/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php index 8fd83cfc..0b880bf4 100644 --- a/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php @@ -6,13 +6,21 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Models\Consumer\Objects\Member\PNMemberIncludes; +use PubNub\Models\Consumer\Objects\Member\PNChannelMember; use PubNub\Models\Consumer\Objects\Member\PNMembersResult; use PubNub\PubNubUtil; +use PubNub\PubNub; class RemoveMembers extends ObjectsCollectionEndpoint { protected const PATH = "/v2/objects/%s/channels/%s/uuids"; + protected bool $endpointAuthRequired = true; + protected string $endpointHttpMethod = PNHttpMethod::PATCH; + protected int $endpointOperationType = PNOperationType::PNRemoveMembersOperation; + protected string $endpointName = "ManageMembers"; + /** @var string */ protected $channel; @@ -22,6 +30,22 @@ class RemoveMembers extends ObjectsCollectionEndpoint /** @var array */ protected $include = []; + /** @var PNMemberIncludes */ + protected PNMemberIncludes $includes; + + /** @var PNChannelMember[] */ + protected array $members; + + /** + * @param PubNub $pubnubInstance + */ + public function __construct(PubNub $pubnubInstance) + { + parent::__construct($pubnubInstance); + $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + /** * @param string $ch * @return $this @@ -35,6 +59,8 @@ public function channel($ch) /** * @param array $uuids + * @deprecated Use members() method + * * @return $this */ public function uuids($uuids) @@ -44,8 +70,37 @@ public function uuids($uuids) return $this; } + /** + * @param PNChannelMember[] $members + * @return $this + */ + public function members(array $members) + { + $this->members = $members; + + return $this; + } + + /** + * Defines a list of fields to be included in response. It takes an instance of PNMemberIncludes. + * + * Example: + * + * $includes = (new PNMemberIncludes())->custom()->status()->totalCount()->type()-user(); + * $pnGetMembers->include($includes); + * + * @param PNMemberIncludes $includes + * @return $this + */ + public function include(PNMemberIncludes $includes): self + { + $this->includes = $includes; + return $this; + } + /** * @param array $include + * @deprecated Use include() method * @return $this */ public function includeFields($include) @@ -66,8 +121,12 @@ protected function validateParams() throw new PubNubValidationException("channel missing"); } - if (empty($this->uuids)) { - throw new PubNubValidationException("uuids missing"); + if (!empty($this->members) and !empty($this->uuids)) { + throw new PubNubValidationException("Either members or uuids should be provided"); + } + + if (empty($this->uuids) and empty($this->members)) { + throw new PubNubValidationException("Members or a list of uuids missing"); } } @@ -78,17 +137,20 @@ protected function validateParams() protected function buildData() { $entries = []; - - foreach ($this->uuids as $value) { - $entry = [ - "uuid" => [ - "id" => $value, - ] - ]; - - array_push($entries, $entry); + if (!empty($this->members)) { + foreach ($this->members as $member) { + array_push($entries, $member->toArray()); + } + } else { + foreach ($this->uuids as $value) { + $entry = [ + "uuid" => [ + "id" => $value, + ] + ]; + array_push($entries, $entry); + } } - return PubNubUtil::writeValueAsString([ "delete" => $entries ]); @@ -127,7 +189,9 @@ protected function customParams() { $params = $this->defaultParams(); - if (count($this->include) > 0) { + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } elseif (count($this->include) > 0) { $includes = []; if (array_key_exists("customFields", $this->include)) { @@ -185,52 +249,4 @@ protected function customParams() return $params; } - - /** - * @return bool - */ - protected function isAuthRequired() - { - return true; - } - - /** - * @return int - */ - protected function getRequestTimeout() - { - return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); - } - - /** - * @return int - */ - protected function getConnectTimeout() - { - return $this->pubnub->getConfiguration()->getConnectTimeout(); - } - - /** - * @return string PNHttpMethod - */ - protected function httpMethod() - { - return PNHttpMethod::PATCH; - } - - /** - * @return int - */ - protected function getOperationType() - { - return PNOperationType::PNRemoveMembersOperation; - } - - /** - * @return string - */ - protected function getName() - { - return "RemoveMembers"; - } } diff --git a/src/PubNub/Endpoints/Objects/Member/SetMembers.php b/src/PubNub/Endpoints/Objects/Member/SetMembers.php index 26253e2a..9dbcf8e7 100644 --- a/src/PubNub/Endpoints/Objects/Member/SetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/SetMembers.php @@ -6,13 +6,21 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Models\Consumer\Objects\Member\PNMemberIncludes; +use PubNub\Models\Consumer\Objects\Member\PNChannelMember; use PubNub\Models\Consumer\Objects\Member\PNMembersResult; use PubNub\PubNubUtil; +use PubNub\PubNub; class SetMembers extends ObjectsCollectionEndpoint { protected const PATH = "/v2/objects/%s/channels/%s/uuids"; + protected bool $endpointAuthRequired = true; + protected string $endpointHttpMethod = PNHttpMethod::PATCH; + protected int $endpointOperationType = PNOperationType::PNSetMembersOperation; + protected string $endpointName = "SetMembers"; + /** @var string */ protected $channel; @@ -25,6 +33,22 @@ class SetMembers extends ObjectsCollectionEndpoint /** @var array */ protected $include = []; + /** @var PNMemberIncludes */ + protected PNMemberIncludes $includes; + + /** @var PNChannelMember[] */ + protected array $members; + + /** + * @param PubNub $pubnubInstance + */ + public function __construct(PubNub $pubnubInstance) + { + parent::__construct($pubnubInstance); + $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + /** * @param string $ch * @return $this @@ -38,6 +62,8 @@ public function channel($ch) /** * @param array $uuids + * @deprecated Use members() method + * * @return $this */ public function uuids($uuids) @@ -47,8 +73,20 @@ public function uuids($uuids) return $this; } + /** + * @param PNChannelMember[] $members + * @return $this + */ + public function members(array $members) + { + $this->members = $members; + + return $this; + } + /** * @param array $custom + * @deprecated Use members() method * @return $this */ public function custom($custom) @@ -58,8 +96,26 @@ public function custom($custom) return $this; } + /** + * Defines a list of fields to be included in response. It takes an instance of PNMemberIncludes. + * + * Example: + * + * $includes = (new PNMemberIncludes())->custom()->status()->totalCount()->type()-user(); + * $pnGetMembers->include($includes); + * + * @param PNMemberIncludes $includes + * @return $this + */ + public function include(PNMemberIncludes $includes): self + { + $this->includes = $includes; + return $this; + } + /** * @param array $include + * @deprecated Use include() method * @return $this */ public function includeFields($include) @@ -80,8 +136,12 @@ protected function validateParams() throw new PubNubValidationException("channel missing"); } - if (empty($this->uuids)) { - throw new PubNubValidationException("uuids missing"); + if (!empty($this->members) and !empty($this->uuids)) { + throw new PubNubValidationException("Either members or uuids should be provided"); + } + + if (empty($this->uuids) and empty($this->members)) { + throw new PubNubValidationException("Members or a list of uuids missing"); } } @@ -92,21 +152,25 @@ protected function validateParams() protected function buildData() { $entries = []; - - foreach ($this->uuids as $value) { - $entry = [ - "uuid" => [ - "id" => $value, - ] - ]; - - if (!empty($this->custom)) { - $entry["custom"] = $this->custom; + if (!empty($this->members)) { + foreach ($this->members as $member) { + array_push($entries, $member->toArray()); } + } else { + foreach ($this->uuids as $value) { + $entry = [ + "uuid" => [ + "id" => $value, + ] + ]; + + if (!empty($this->custom)) { + $entry["custom"] = $this->custom; + } - array_push($entries, $entry); + array_push($entries, $entry); + } } - return PubNubUtil::writeValueAsString([ "set" => $entries ]); @@ -145,25 +209,29 @@ protected function customParams() { $params = $this->defaultParams(); - if (count($this->include) > 0) { - $includes = []; + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } else { + if (count($this->include) > 0) { + $includes = []; - if (array_key_exists("customFields", $this->include)) { - array_push($includes, 'custom'); - } + if (array_key_exists("customFields", $this->include)) { + array_push($includes, 'custom'); + } - if (array_key_exists("customUUIDFields", $this->include)) { - array_push($includes, 'uuid.custom'); - } + if (array_key_exists("customUUIDFields", $this->include)) { + array_push($includes, 'uuid.custom'); + } - if (array_key_exists("UUIDFields", $this->include)) { - array_push($includes, 'uuid'); - } + if (array_key_exists("UUIDFields", $this->include)) { + array_push($includes, 'uuid'); + } - $includesString = implode(",", $includes); + $includesString = implode(",", $includes); - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (strlen($includesString) > 0) { + $params['include'] = $includesString; + } } } @@ -202,52 +270,4 @@ protected function customParams() return $params; } - - /** - * @return bool - */ - protected function isAuthRequired() - { - return true; - } - - /** - * @return int - */ - protected function getRequestTimeout() - { - return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); - } - - /** - * @return int - */ - protected function getConnectTimeout() - { - return $this->pubnub->getConfiguration()->getConnectTimeout(); - } - - /** - * @return string PNHttpMethod - */ - protected function httpMethod() - { - return PNHttpMethod::PATCH; - } - - /** - * @return int - */ - protected function getOperationType() - { - return PNOperationType::PNSetMembersOperation; - } - - /** - * @return string - */ - protected function getName() - { - return "SetMembers"; - } } diff --git a/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php b/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php index 9aad0809..f7effd30 100644 --- a/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php @@ -6,26 +6,60 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Models\Consumer\Objects\Membership\PNMembershipIncludes; use PubNub\Models\Consumer\Objects\Membership\PNMembershipsResult; +use PubNub\PubNub; class GetMemberships extends ObjectsCollectionEndpoint { protected const PATH = "/v2/objects/%s/uuids/%s/channels"; + protected bool $endpointAuthRequired = true; + protected string $endpointHttpMethod = PNHttpMethod::GET; + protected int $endpointOperationType = PNOperationType::PNGetMembershipsOperation; + protected string $endpointName = "GetMemberships"; + /** @var string */ - protected $uuid; + protected $userId; /** @var array */ protected $include = []; + protected ?PNMembershipIncludes $includes; + + /** + * @param PubNub $pubnubInstance + */ + public function __construct(PubNub $pubnubInstance) + { + parent::__construct($pubnubInstance); + $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + /** * @param string $uuid * @return $this */ public function uuid($uuid) { - $this->uuid = $uuid; + $this->userId = $uuid; + return $this; + } + /** + * @param string $uuid + * @return $this + */ + public function userId($uuid) + { + $this->userId = $uuid; + return $this; + } + + public function include(PNMembershipIncludes $includes): self + { + $this->includes = $includes; return $this; } @@ -47,7 +81,7 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->uuid)) { + if (!is_string($this->userId)) { throw new PubNubValidationException("uuid missing"); } } @@ -69,7 +103,7 @@ protected function buildPath() return sprintf( static::PATH, $this->pubnub->getConfiguration()->getSubscribeKey(), - $this->uuid + $this->userId ); } @@ -94,7 +128,9 @@ protected function customParams() { $params = $this->defaultParams(); - if (count($this->include) > 0) { + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } elseif (count($this->include) > 0) { $includes = []; if (array_key_exists("customFields", $this->include)) { @@ -152,52 +188,4 @@ protected function customParams() return $params; } - - /** - * @return bool - */ - protected function isAuthRequired() - { - return true; - } - - /** - * @return int - */ - protected function getRequestTimeout() - { - return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); - } - - /** - * @return int - */ - protected function getConnectTimeout() - { - return $this->pubnub->getConfiguration()->getConnectTimeout(); - } - - /** - * @return string PNHttpMethod - */ - protected function httpMethod() - { - return PNHttpMethod::GET; - } - - /** - * @return int - */ - protected function getOperationType() - { - return PNOperationType::PNGetMembershipsOperation; - } - - /** - * @return string - */ - protected function getName() - { - return "GetMemberships"; - } } diff --git a/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php b/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php new file mode 100644 index 00000000..3dbe4fc1 --- /dev/null +++ b/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php @@ -0,0 +1,314 @@ +endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + /** + * @param string $uuid + * @return $this + */ + public function uuid($uuid): self + { + $this->userId = $uuid; + return $this; + } + + /** + * @param string $userId + * @return $this + */ + public function userId($userId): self + { + $this->userId = $userId; + return $this; + } + + /** + * @param array $channels + * @deprecated Use memberships() method + * + * @return $this + */ + public function setChannels($channels): self + { + $this->setChannels = $channels; + return $this; + } + + /** + * @param array $channels + * @deprecated Use memberships() method + * + * @return $this + */ + public function removeChannels($channels): self + { + $this->removeChannels = $channels; + return $this; + } + + /** + * @param PNChannelMemberhips[] $members + * @return $this + */ + public function setMemberships(array $memberships): self + { + $this->setMemberships = $memberships; + return $this; + } + + /** + * @param PNChannelMemberhips[] $members + * @return $this + */ + public function removeMemberships(array $memberships): self + { + $this->removeMemberships = $memberships; + return $this; + } + + /** + * @param array $custom + * @deprecated Use members() method + * + * @return $this + */ + public function custom($custom): self + { + $this->custom = $custom; + return $this; + } + + /** + * @param array $include + * @deprecated Use includes() method + * + * @return $this + */ + public function includeFields($include): self + { + $this->include = $include; + return $this; + } + + /** + * Defines a list of fields to be included in response. It takes an instance of PNMemberIncludes. + * + * Example: + * + * $includes = (new PNMembershipIncludes())->custom()->status()->totalCount()->type()-user(); + * $pnGetMembers->include($includes); + * + * @param PNMembershipIncludes $includes + * @return $this + */ + public function include(PNMembershipIncludes $includes): self + { + $this->includes = $includes; + return $this; + } + + /** + * @throws PubNubValidationException + */ + protected function validateParams() + { + $this->validateSubscribeKey(); + + if (!is_string($this->userId)) { + throw new PubNubValidationException("uuid missing"); + } + + $memberships = !empty($this->setMemberships) or !empty($this->removeMemberships); + $channels = !empty($this->setChannels) or !empty($this->removeChannels); + + if ($memberships and $channels) { + throw new PubNubValidationException("Either memberships or channels should be provided"); + } + + if (!$memberships and !$channels) { + throw new PubNubValidationException("Memberships or a list of channels missing"); + } + } + + /** + * @return string + * @throws PubNubBuildRequestException + */ + protected function buildData() + { + $set = []; + $remove = []; + if (!empty($this->setMemberships)) { + foreach ($this->setMemberships as $memberhip) { + array_push($set, $memberhip->toArray()); + } + } else { + foreach ($this->setChannels as $value) { + array_push($set, ["channel" => ["id" => $value], "custom" => $this->custom]); + } + } + + $remove = []; + if (!empty($this->removeMemberships)) { + foreach ($this->removeMemberships as $memberhip) { + array_push($remove, $memberhip->toArray()); + } + } else { + foreach ($this->removeChannels as $value) { + array_push($remove, ["channel" => ["id" => $value]]); + } + } + + return PubNubUtil::writeValueAsString([ + "set" => $set, + "delete" => $remove + ]); + } + + /** + * @return string + */ + protected function buildPath() + { + return sprintf( + static::PATH, + $this->pubnub->getConfiguration()->getSubscribeKey(), + $this->userId + ); + } + + public function sync(): PNMembershipsResult + { + return parent::sync(); + } + + /** + * @param array $result Decoded json + * @return PNMembershipsResult + */ + protected function createResponse($result): PNMembershipsResult + { + return PNMembershipsResult::fromPayload($result); + } + + /** + * @return array + */ + protected function customParams() + { + $params = $this->defaultParams(); + + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } elseif (count($this->include) > 0) { + $includes = []; + + if (array_key_exists("customFields", $this->include)) { + array_push($includes, 'custom'); + } + + if (array_key_exists("customChannelFields", $this->include)) { + array_push($includes, 'channel.custom'); + } + + if (array_key_exists("channelFields", $this->include)) { + array_push($includes, 'channel'); + } + + $includesString = implode(",", $includes); + + if (strlen($includesString) > 0) { + $params['include'] = $includesString; + } + } + + if (array_key_exists("totalCount", $this->include)) { + $params['count'] = "true"; + } + + if (array_key_exists("next", $this->page)) { + $params['start'] = $this->page["next"]; + } + + if (array_key_exists("prev", $this->page)) { + $params['end'] = $this->page["prev"]; + } + + if (!empty($this->filter)) { + $params['filter'] = $this->filter; + } + + if (!empty($this->limit)) { + $params['limit'] = $this->limit; + } + + if (!empty($this->sort)) { + $sortEntries = []; + + foreach ($this->sort as $key => $value) { + if ($value === 'asc' || $value === 'desc') { + array_push($sortEntries, "$key:$value"); + } else { + array_push($sortEntries, $key); + } + } + + $params['sort'] = $sortEntries; + } + + return $params; + } +} diff --git a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php index 254727c5..64a51242 100644 --- a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php @@ -6,15 +6,23 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Models\Consumer\Objects\Membership\PNChannelMembership; +use PubNub\Models\Consumer\Objects\Membership\PNMembershipIncludes; use PubNub\Models\Consumer\Objects\Membership\PNMembershipsResult; use PubNub\PubNubUtil; +use PubNub\PubNub; class RemoveMemberships extends ObjectsCollectionEndpoint { protected const PATH = "/v2/objects/%s/uuids/%s/channels"; + protected bool $endpointAuthRequired = true; + protected string $endpointHttpMethod = PNHttpMethod::PATCH; + protected int $endpointOperationType = PNOperationType::PNRemoveMembershipsOperation; + protected string $endpointName = "RemoveMemberships"; + /** @var string */ - protected $uuid; + protected $userId; /** @var array */ protected $channels; @@ -22,39 +30,95 @@ class RemoveMemberships extends ObjectsCollectionEndpoint /** @var array */ protected $include = []; + /** @var PNMembershipIncludes */ + protected ?PNMembershipIncludes $includes; + + /** @var ?PNChannelMembership[] */ + protected array $memberships; + + /** + * @param PubNub $pubnubInstance + */ + public function __construct(PubNub $pubnubInstance) + { + parent::__construct($pubnubInstance); + $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + /** + * @param string $uuid + * @return $this + */ + public function uuid(string $uuid): self + { + $this->userId = $uuid; + + return $this; + } + /** * @param string $uuid * @return $this */ - public function uuid($uuid) + public function userId(string $userId): self { - $this->uuid = $uuid; + $this->userId = $userId; return $this; } /** * @param array $channels + * @deprecated Use memberships() method * @return $this */ - public function channels($channels) + public function channels($channels): self { $this->channels = $channels; return $this; } + /** + * @param PNChannelMemberhips[] $members + * @return $this + */ + public function memberships(array $memberships): self + { + $this->memberships = $memberships; + return $this; + } + /** * @param array $include + * @deprecated Use includes() method * @return $this */ - public function includeFields($include) + public function includeFields($include): self { $this->include = $include; return $this; } + /** + * Defines a list of fields to be included in response. It takes an instance of PNMemberIncludes. + * + * Example: + * + * $includes = (new PNMembershipIncludes())->custom()->status()->totalCount()->type()-user(); + * $pnGetMembers->include($includes); + * + * @param PNMembershipIncludes $includes + * @return $this + */ + public function include(PNMembershipIncludes $includes): self + { + $this->includes = $includes; + return $this; + } + /** * @throws PubNubValidationException */ @@ -62,12 +126,16 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->uuid)) { + if (!is_string($this->userId)) { throw new PubNubValidationException("uuid missing"); } - if (empty($this->channels)) { - throw new PubNubValidationException("channels missing"); + if (!empty($this->channels) and !empty($this->memberships)) { + throw new PubNubValidationException("Either memberships or channels should be provided"); + } + + if (empty($this->channels) and empty($this->memberships)) { + throw new PubNubValidationException("Memberships or a list of channels missing"); } } @@ -78,15 +146,20 @@ protected function validateParams() protected function buildData() { $entries = []; - - foreach ($this->channels as $value) { - $entry = [ - "channel" => [ - "id" => $value, - ] - ]; - - array_push($entries, $entry); + if (!empty($this->memberships)) { + foreach ($this->memberships as $memberhip) { + array_push($entries, $memberhip->toArray()); + } + } elseif (!empty($this->channels)) { + foreach ($this->channels as $value) { + $entry = [ + "channel" => [ + "id" => $value, + ] + ]; + + array_push($entries, $entry); + } } return PubNubUtil::writeValueAsString([ @@ -102,7 +175,7 @@ protected function buildPath() return sprintf( static::PATH, $this->pubnub->getConfiguration()->getSubscribeKey(), - $this->uuid + $this->userId ); } @@ -127,7 +200,9 @@ protected function customParams() { $params = $this->defaultParams(); - if (count($this->include) > 0) { + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } elseif (count($this->include) > 0) { $includes = []; if (array_key_exists("customFields", $this->include)) { @@ -185,52 +260,4 @@ protected function customParams() return $params; } - - /** - * @return bool - */ - protected function isAuthRequired() - { - return true; - } - - /** - * @return int - */ - protected function getRequestTimeout() - { - return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); - } - - /** - * @return int - */ - protected function getConnectTimeout() - { - return $this->pubnub->getConfiguration()->getConnectTimeout(); - } - - /** - * @return string PNHttpMethod - */ - protected function httpMethod() - { - return PNHttpMethod::PATCH; - } - - /** - * @return int - */ - protected function getOperationType() - { - return PNOperationType::PNRemoveMembershipsOperation; - } - - /** - * @return string - */ - protected function getName() - { - return "RemoveMemberships"; - } } diff --git a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php index 99eb2215..e6d58f1c 100644 --- a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php @@ -6,15 +6,23 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Models\Consumer\Objects\Membership\PNMembershipIncludes; +use PubNub\Models\Consumer\Objects\Membership\PNChannelMembership; use PubNub\Models\Consumer\Objects\Membership\PNMembershipsResult; use PubNub\PubNubUtil; +use PubNub\PubNub; class SetMemberships extends ObjectsCollectionEndpoint { protected const PATH = "/v2/objects/%s/uuids/%s/channels"; + protected bool $endpointAuthRequired = true; + protected string $endpointHttpMethod = PNHttpMethod::PATCH; + protected int $endpointOperationType = PNOperationType::PNSetMembershipsOperation; + protected string $endpointName = "SetMemberships"; + /** @var string */ - protected $uuid; + protected $userId; /** @var array */ protected $channels; @@ -25,47 +33,102 @@ class SetMemberships extends ObjectsCollectionEndpoint /** @var array */ protected $include = []; + /** @var PNMembershipIncludes */ + protected ?PNMembershipIncludes $includes; + + /** @var ?PNChannelMembership[] */ + protected array $memberships; + + /** + * @param PubNub $pubnubInstance + */ + public function __construct(PubNub $pubnubInstance) + { + parent::__construct($pubnubInstance); + $this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + /** * @param string $uuid * @return $this */ - public function uuid($uuid) + public function uuid($uuid): self { - $this->uuid = $uuid; + $this->userId = $uuid; + return $this; + } + /** + * @param string $userId + * @return $this + */ + public function userId($userId): self + { + $this->userId = $userId; return $this; } /** * @param array $channels + * @deprecated Use memberships() method + * * @return $this */ - public function channels($channels) + public function channels($channels): self { $this->channels = $channels; + return $this; + } + /** + * @param PNChannelMemberhips[] $members + * @return $this + */ + public function memberships(array $memberships): self + { + $this->memberships = $memberships; return $this; } /** * @param array $custom + * @deprecated Use members() method + * * @return $this */ - public function custom($custom) + public function custom($custom): self { $this->custom = $custom; - return $this; } /** * @param array $include + * @deprecated Use includes() method + * * @return $this */ - public function includeFields($include) + public function includeFields($include): self { $this->include = $include; + return $this; + } + /** + * Defines a list of fields to be included in response. It takes an instance of PNMemberIncludes. + * + * Example: + * + * $includes = (new PNMembershipIncludes())->custom()->status()->totalCount()->type()-user(); + * $pnGetMembers->include($includes); + * + * @param PNMembershipIncludes $includes + * @return $this + */ + public function include(PNMembershipIncludes $includes): self + { + $this->includes = $includes; return $this; } @@ -76,12 +139,16 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->uuid)) { + if (!is_string($this->userId)) { throw new PubNubValidationException("uuid missing"); } - if (empty($this->channels)) { - throw new PubNubValidationException("channels missing"); + if (!empty($this->channels) and !empty($this->memberships)) { + throw new PubNubValidationException("Either memberships or channels should be provided"); + } + + if (empty($this->channels) and empty($this->memberships)) { + throw new PubNubValidationException("Memberships or a list of channels missing"); } } @@ -92,19 +159,23 @@ protected function validateParams() protected function buildData() { $entries = []; - - foreach ($this->channels as $value) { - $entry = [ - "channel" => [ - "id" => $value, - ] - ]; - - if (!empty($this->custom)) { - $entry["custom"] = $this->custom; + if (!empty($this->memberships)) { + foreach ($this->memberships as $memberhip) { + array_push($entries, $memberhip->toArray()); } + } elseif (!empty($this->channels)) { + foreach ($this->channels as $value) { + $entry = [ + "channel" => [ + "id" => $value, + ] + ]; + if (!empty($this->custom)) { + $entry["custom"] = $this->custom; + } - array_push($entries, $entry); + array_push($entries, $entry); + } } return PubNubUtil::writeValueAsString([ @@ -120,7 +191,7 @@ protected function buildPath() return sprintf( static::PATH, $this->pubnub->getConfiguration()->getSubscribeKey(), - $this->uuid + $this->userId ); } @@ -145,7 +216,9 @@ protected function customParams() { $params = $this->defaultParams(); - if (count($this->include) > 0) { + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } elseif (count($this->include) > 0) { $includes = []; if (array_key_exists("customFields", $this->include)) { @@ -203,52 +276,4 @@ protected function customParams() return $params; } - - /** - * @return bool - */ - protected function isAuthRequired() - { - return true; - } - - /** - * @return int - */ - protected function getRequestTimeout() - { - return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); - } - - /** - * @return int - */ - protected function getConnectTimeout() - { - return $this->pubnub->getConfiguration()->getConnectTimeout(); - } - - /** - * @return string PNHttpMethod - */ - protected function httpMethod() - { - return PNHttpMethod::PATCH; - } - - /** - * @return int - */ - protected function getOperationType() - { - return PNOperationType::PNSetMembershipsOperation; - } - - /** - * @return string - */ - protected function getName() - { - return "SetMemberships"; - } } diff --git a/src/PubNub/Enums/PNOperationType.php b/src/PubNub/Enums/PNOperationType.php index d941075e..2d9cd666 100755 --- a/src/PubNub/Enums/PNOperationType.php +++ b/src/PubNub/Enums/PNOperationType.php @@ -74,4 +74,7 @@ class PNOperationType const PNAddMessageActionOperation = 53; const PNGetMessageActionsOperation = 54; const PNRemoveMessageActionOperation = 55; + + const PNManageMembersOperation = 56; + const PNManageMembershipsOperation = 56; } diff --git a/src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php b/src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php new file mode 100644 index 00000000..0e6d2ff5 --- /dev/null +++ b/src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php @@ -0,0 +1,99 @@ +userId = $userId; + $this->custom = $custom; + $this->type = $type; + $this->status = $status; + } + + public function setUserId(string $userId): self + { + $this->userId = $userId; + return $this; + } + + public function setCustom(mixed $custom): self + { + $this->custom = $custom; + return $this; + } + + public function setType(string $type): self + { + $this->type = $type; + return $this; + } + + public function setStatus(string $status): self + { + $this->status = $status; + return $this; + } + + public function getUserId(): string + { + return $this->userId; + } + + /** + * @return string[] | \StdClass + */ + public function getCustom() + { + return $this->custom; + } + + public function getType(): string + { + return $this->type; + } + + public function getStatus(): string + { + return $this->status; + } + + /** + * @return string[] + */ + public function toArray() + { + $result = [ + 'uuid' => [ + 'id' => $this->userId + ] + ]; + + if ($this->custom) { + $result['custom'] = $this->custom; + } + + if ($this->type) { + $result['type'] = $this->type; + } + + if ($this->status) { + $result['status'] = $this->status; + } + + return $result; + } +} diff --git a/src/PubNub/Models/Consumer/Objects/Member/PNMember.php b/src/PubNub/Models/Consumer/Objects/Member/PNMember.php index 42d8dc50..d9168c7b 100755 --- a/src/PubNub/Models/Consumer/Objects/Member/PNMember.php +++ b/src/PubNub/Models/Consumer/Objects/Member/PNMember.php @@ -22,6 +22,12 @@ class PNMember /** @var array */ protected $custom; + /** @var string */ + protected ?string $status; + + /** @var string */ + protected ?string $type; + /** @var string */ protected $updated; @@ -38,9 +44,21 @@ class PNMember * @param array $custom * @param string $updated * @param string $eTag + * @param string $status + * @param string $type */ - function __construct($id, $name, $externalId, $profileUrl, $email, $custom = null, $updated = null, $eTag = null) - { + public function __construct( + $id, + $name, + $externalId, + $profileUrl, + $email, + $custom = null, + $updated = null, + $eTag = null, + $status = null, + $type = null + ) { $this->id = $id; $this->name = $name; $this->externalId = $externalId; @@ -49,6 +67,8 @@ function __construct($id, $name, $externalId, $profileUrl, $email, $custom = nul $this->custom = $custom; $this->updated = $updated; $this->eTag = $eTag; + $this->status = $status; + $this->type = $type; } /** @@ -99,6 +119,22 @@ public function getCustom() return $this->custom; } + /** + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + /** * @return string */ @@ -118,22 +154,29 @@ public function getETag() public function __toString() { $custom_string = ""; - - foreach($this->custom as $key => $value) { + + foreach ($this->custom as $key => $value) { if (strlen($custom_string) > 0) { $custom_string .= ", "; } $custom_string .= "$key: $value"; } - - return sprintf("id: %s, custom: %s, updated: %s, eTag: %s", - $this->id, "[" . $custom_string . "]", $this->updated, $this->eTag); + + return sprintf( + "id: %s, custom: %s, updated: %s, eTag: %s, status: %s, type: %s", + $this->id, + "[" . $custom_string . "]", + $this->updated, + $this->eTag, + $this->status, + $this->type + ); } /** * @param array $payload - * @return PNMember + * @return PNMember */ public static function fromPayload(array $payload) { @@ -145,48 +188,61 @@ public static function fromPayload(array $payload) $email = null; $custom = null; $updated = null; + $status = null; + $type = null; $eTag = null; - if (array_key_exists("id", $data)) - { + if (array_key_exists("id", $data)) { $id = $data["id"]; } - if (array_key_exists("name", $data)) - { + if (array_key_exists("name", $data)) { $name = $data["name"]; } - if (array_key_exists("externalId", $data)) - { + if (array_key_exists("externalId", $data)) { $externalId = $data["externalId"]; } - if (array_key_exists("profileUrl", $data)) - { + if (array_key_exists("profileUrl", $data)) { $profileUrl = $data["profileUrl"]; } - if (array_key_exists("email", $data)) - { + if (array_key_exists("email", $data)) { $email = $data["email"]; } - if (array_key_exists("custom", $data)) - { + if (array_key_exists("custom", $data)) { $custom = (object)$data["custom"]; } - if (array_key_exists("updated", $data)) - { + if (array_key_exists("updated", $data)) { $updated = (object)$data["updated"]; } - if (array_key_exists("eTag", $data)) - { + if (array_key_exists("eTag", $data)) { $eTag = (object)$data["eTag"]; } - return new PNMember($id, $name, $externalId, $profileUrl, $email, (object) $custom, $updated, $eTag); + if (array_key_exists("status", $data)) { + $status = $data["status"]; + } + + if (array_key_exists("type", $data)) { + $type = $data["type"]; + } + + return new PNMember( + $id, + $name, + $externalId, + $profileUrl, + $email, + (object)$custom, + $updated, + $eTag, + $status, + $type + ); } -} \ No newline at end of file +} diff --git a/src/PubNub/Models/Consumer/Objects/Member/PNMemberIncludes.php b/src/PubNub/Models/Consumer/Objects/Member/PNMemberIncludes.php new file mode 100644 index 00000000..62763524 --- /dev/null +++ b/src/PubNub/Models/Consumer/Objects/Member/PNMemberIncludes.php @@ -0,0 +1,55 @@ +mapping = array_merge($this->mapping, [ + 'user' => 'uuid', + 'userId' => 'uuid.id', + 'userCustom' => 'uuid.custom', + 'userType' => 'uuid.type', + 'userStatus' => 'uuid.status', + ]); + } + + public function user(bool $user = true): self + { + $this->user = $user; + return $this; + } + + public function userId(bool $userId = true): self + { + $this->userId = $userId; + return $this; + } + + public function userCustom(bool $userCustom = true): self + { + $this->userCustom = $userCustom; + return $this; + } + + public function userType(bool $userType = true): self + { + $this->userType = $userType; + return $this; + } + + public function userStatus(bool $userStatus = true): self + { + $this->userStatus = $userStatus; + return $this; + } +} diff --git a/src/PubNub/Models/Consumer/Objects/Member/PNMembersResult.php b/src/PubNub/Models/Consumer/Objects/Member/PNMembersResult.php index 7dc9bde1..b3c69dc6 100755 --- a/src/PubNub/Models/Consumer/Objects/Member/PNMembersResult.php +++ b/src/PubNub/Models/Consumer/Objects/Member/PNMembersResult.php @@ -23,7 +23,7 @@ class PNMembersResult * @param string $next * @param array $data */ - function __construct($totalCount, $prev, $next, $data) + public function __construct($totalCount, $prev, $next, $data) { $this->totalCount = $totalCount; $this->prev = $prev; @@ -65,13 +65,17 @@ public function getData() public function __toString() { - if (!empty($data)) - { - $data_string = json_encode($data); + if (!empty($data)) { + $data_string = json_encode($data); } - return sprintf("totalCount: %s, prev: %s, next: %s, data: %s", - $this->totalCount, $this->prev, $this->next, $data_string); + return sprintf( + "totalCount: %s, prev: %s, next: %s, data: %s", + $this->totalCount, + $this->prev, + $this->next, + $data_string + ); } /** @@ -85,31 +89,26 @@ public static function fromPayload(array $payload) $next = null; $data = null; - if (array_key_exists("totalCount", $payload)) - { + if (array_key_exists("totalCount", $payload)) { $totalCount = $payload["totalCount"]; } - if (array_key_exists("prev", $payload)) - { + if (array_key_exists("prev", $payload)) { $prev = $payload["prev"]; } - if (array_key_exists("next", $payload)) - { + if (array_key_exists("next", $payload)) { $next = $payload["next"]; } - if (array_key_exists("data", $payload)) - { + if (array_key_exists("data", $payload)) { $data = []; - foreach($payload["data"] as $value) - { + foreach ($payload["data"] as $value) { array_push($data, PNMembersResultItem::fromPayload($value)); } } return new PNMembersResult($totalCount, $prev, $next, $data); } -} \ No newline at end of file +} diff --git a/src/PubNub/Models/Consumer/Objects/Member/PNMembersResultItem.php b/src/PubNub/Models/Consumer/Objects/Member/PNMembersResultItem.php index dd21ecb6..787a4e43 100755 --- a/src/PubNub/Models/Consumer/Objects/Member/PNMembersResultItem.php +++ b/src/PubNub/Models/Consumer/Objects/Member/PNMembersResultItem.php @@ -10,6 +10,12 @@ class PNMembersResultItem /** @var array */ protected $custom; + /** @var string */ + protected $status; + + /** @var string */ + protected $type; + /** @var string */ protected $updated; @@ -22,13 +28,17 @@ class PNMembersResultItem * @param object $custom * @param string $updated * @param string $eTag + * @param string $status + * @param string $type */ - function __construct($uuid, $custom, $updated, $eTag) + public function __construct($uuid, $custom, $updated, $eTag, $status = null, $type = null) { $this->uuid = $uuid; $this->custom = $custom; $this->updated = $updated; $this->eTag = $eTag; + $this->status = $status; + $this->type = $type; } /** @@ -40,13 +50,37 @@ public function getUUID() } /** - * @return array + * @return PNMember + */ + public function getUser() + { + return $this->uuid; + } + + /** + * @return array | \StdClass */ public function getCustom() { return $this->custom; } + /** + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + /** * @return string */ @@ -65,13 +99,17 @@ public function getETag() public function __toString() { - if (!empty($data)) - { - $data_string = json_encode($data); + if (!empty($data)) { + $data_string = json_encode($data); } - return sprintf("uuid: %s, custom: %s, updated: %s, eTag: %s", - $this->uuid, $this->custom, $this->updated, $this->eTag); + return sprintf( + "uuid: %s, custom: %s, updated: %s, eTag: %s", + $this->uuid, + $this->custom, + $this->updated, + $this->eTag + ); } /** @@ -84,27 +122,32 @@ public static function fromPayload(array $payload) $custom = null; $updated = null; $eTag = null; + $status = null; + $type = null; - if (array_key_exists("uuid", $payload)) - { + if (array_key_exists("uuid", $payload)) { $uuid = PNMember::fromPayload([ "data" => $payload["uuid"] ]); } - if (array_key_exists("custom", $payload)) - { + if (array_key_exists("custom", $payload)) { $custom = $payload["custom"]; } - if (array_key_exists("updated", $payload)) - { + if (array_key_exists("status", $payload)) { + $status = $payload["status"]; + } + if (array_key_exists("type", $payload)) { + $type = $payload["type"]; + } + + if (array_key_exists("updated", $payload)) { $updated = $payload["updated"]; } - if (array_key_exists("eTag", $payload)) - { + if (array_key_exists("eTag", $payload)) { $eTag = $payload["eTag"]; } - return new PNMembersResultItem($uuid, (object) $custom, $updated, $eTag); + return new PNMembersResultItem($uuid, (object) $custom, $updated, $eTag, $status, $type); } -} \ No newline at end of file +} diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php b/src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php new file mode 100644 index 00000000..68221015 --- /dev/null +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php @@ -0,0 +1,100 @@ +channel = $channel; + $this->custom = $custom; + $this->type = $type; + $this->status = $status; + } + + public function setchannel(string $channel): self + { + $this->channel = $channel; + return $this; + } + + public function setCustom(mixed $custom): self + { + $this->custom = $custom; + return $this; + } + + public function setType(string $type): self + { + $this->type = $type; + return $this; + } + + public function setStatus(string $status): self + { + $this->status = $status; + return $this; + } + + public function getchannel(): string + { + return $this->channel; + } + + /** + * @return string[] | \StdClass + */ + public function getCustom(): mixed + { + return $this->custom; + } + + public function getType(): string + { + return $this->type; + } + + public function getStatus(): string + { + return $this->status; + } + + /** + * @return string[] + */ + public function toArray() + { + $result = [ + 'channel' => [ + 'id' => $this->channel + ] + ]; + + if ($this->custom) { + $result['custom'] = $this->custom; + } + + if ($this->type) { + $result['type'] = $this->type; + } + + if ($this->status) { + $result['status'] = $this->status; + } + + return $result; + } +} diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNMembership.php b/src/PubNub/Models/Consumer/Objects/Membership/PNMembership.php index 8bb379c4..54cd901d 100755 --- a/src/PubNub/Models/Consumer/Objects/Membership/PNMembership.php +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNMembership.php @@ -16,6 +16,12 @@ class PNMembership /** @var array */ protected $custom; + /** @var string */ + protected ?string $status; + + /** @var string */ + protected ?string $type; + /** @var string */ protected $updated; @@ -30,15 +36,27 @@ class PNMembership * @param array $custom * @param string $updated * @param string $eTag + * @param string $status + * @param string $type */ - function __construct($id, $name, $description, $custom = null, $updated = null, $eTag = null) - { + public function __construct( + $id, + $name, + $description, + $custom = null, + $updated = null, + $eTag = null, + $status = null, + $type = null + ) { $this->id = $id; $this->name = $name; $this->description = $description; $this->custom = $custom; $this->updated = $updated; $this->eTag = $eTag; + $this->status = $status; + $this->type = $type; } /** @@ -73,6 +91,22 @@ public function getCustom() return $this->custom; } + /** + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + /** * @return string */ @@ -92,17 +126,24 @@ public function getETag() public function __toString() { $custom_string = ""; - - foreach($this->custom as $key => $value) { + + foreach ($this->custom as $key => $value) { if (strlen($custom_string) > 0) { $custom_string .= ", "; } $custom_string .= "$key: $value"; } - - return sprintf("id: %s, custom: %s, updated: %s, eTag: %s", - $this->id, "[" . $custom_string . "]", $this->updated, $this->eTag); + + return sprintf( + "id: %s, custom: %s, updated: %s, eTag: %s, status: %s, type: %s", + $this->id, + "[" . $custom_string . "]", + $this->updated, + $this->eTag, + $this->status, + $this->type + ); } /** @@ -116,39 +157,52 @@ public static function fromPayload(array $payload) $name = null; $description = null; $custom = null; + $status = null; + $type = null; $updated = null; $eTag = null; - if (array_key_exists("id", $data)) - { + if (array_key_exists("id", $data)) { $id = $data["id"]; } - if (array_key_exists("name", $data)) - { + if (array_key_exists("name", $data)) { $name = $data["name"]; } - if (array_key_exists("description", $data)) - { + if (array_key_exists("description", $data)) { $description = $data["description"]; } - if (array_key_exists("custom", $data)) - { + if (array_key_exists("custom", $data)) { $custom = (object)$data["custom"]; } - if (array_key_exists("updated", $data)) - { + if (array_key_exists("updated", $data)) { $updated = (object)$data["updated"]; } - if (array_key_exists("eTag", $data)) - { + if (array_key_exists("eTag", $data)) { $eTag = (object)$data["eTag"]; } - return new PNMembership($id, $name, $description, (object) $custom, $updated, $eTag); + if (array_key_exists("status", $data)) { + $status = $data["status"]; + } + + if (array_key_exists("type", $data)) { + $type = $data["type"]; + } + + return new PNMembership( + $id, + $name, + $description, + (object) $custom, + $updated, + $eTag, + $status, + $type + ); } -} \ No newline at end of file +} diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipIncludes.php b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipIncludes.php new file mode 100644 index 00000000..571bff08 --- /dev/null +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipIncludes.php @@ -0,0 +1,55 @@ +mapping = array_merge($this->mapping, [ + 'channel' => 'channel', + 'channelId' => 'channel.id', + 'channelCustom' => 'channel.custom', + 'channelType' => 'channel.type', + 'channelStatus' => 'channel.status', + ]); + } + + public function channel(bool $channel = true): self + { + $this->channel = $channel; + return $this; + } + + public function channelId(bool $channelId = true): self + { + $this->channelId = $channelId; + return $this; + } + + public function channelCustom(bool $channelCustom = true): self + { + $this->channelCustom = $channelCustom; + return $this; + } + + public function channelType(bool $channelType = true): self + { + $this->channelType = $channelType; + return $this; + } + + public function channelStatus(bool $channelStatus = true): self + { + $this->channelStatus = $channelStatus; + return $this; + } +} diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php index 4b0325bf..f126f619 100755 --- a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php @@ -23,7 +23,7 @@ class PNMembershipsResult * @param string $next * @param array $data */ - function __construct($totalCount, $prev, $next, $data) + public function __construct($totalCount, $prev, $next, $data) { $this->totalCount = $totalCount; $this->prev = $prev; @@ -65,13 +65,17 @@ public function getData() public function __toString() { - if (!empty($data)) - { - $data_string = json_encode($data); + if (!empty($data)) { + $data_string = json_encode($data); } - return sprintf("totalCount: %s, prev: %s, next: %s, data: %s", - $this->totalCount, $this->prev, $this->next, $data_string); + return sprintf( + "totalCount: %s, prev: %s, next: %s, data: %s", + $this->totalCount, + $this->prev, + $this->next, + $data_string + ); } /** @@ -85,31 +89,26 @@ public static function fromPayload(array $payload) $next = null; $data = null; - if (array_key_exists("totalCount", $payload)) - { + if (array_key_exists("totalCount", $payload)) { $totalCount = $payload["totalCount"]; } - if (array_key_exists("prev", $payload)) - { + if (array_key_exists("prev", $payload)) { $prev = $payload["prev"]; } - if (array_key_exists("next", $payload)) - { + if (array_key_exists("next", $payload)) { $next = $payload["next"]; } - if (array_key_exists("data", $payload)) - { + if (array_key_exists("data", $payload)) { $data = []; - foreach($payload["data"] as $value) - { + foreach ($payload["data"] as $value) { array_push($data, PNMembershipsResultItem::fromPayload($value)); } } return new PNMembershipsResult($totalCount, $prev, $next, $data); } -} \ No newline at end of file +} diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php index 7340699e..51df2f3f 100755 --- a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php @@ -10,6 +10,13 @@ class PNMembershipsResultItem /** @var array */ protected $custom; + /** @var string */ + protected $status; + + /** @var string */ + protected $type; + + /** @var string */ protected $updated; @@ -22,13 +29,17 @@ class PNMembershipsResultItem * @param object $custom * @param string $updated * @param string $eTag + * @param string $status + * @param string $type */ - function __construct($channel, $custom, $updated, $eTag) + public function __construct($channel, $custom, $updated, $eTag, $status = null, $type = null) { $this->channel = $channel; $this->custom = $custom; $this->updated = $updated; $this->eTag = $eTag; + $this->status = $status; + $this->type = $type; } /** @@ -47,6 +58,22 @@ public function getCustom() return $this->custom; } + /** + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + /** * @return string */ @@ -65,13 +92,18 @@ public function getETag() public function __toString() { - if (!empty($data)) - { - $data_string = json_encode($data); + if (!empty($data)) { + $data_string = json_encode($data); } - return sprintf("channel: %s, custom: %s, updated: %s, eTag: %s", - $this->channel, $this->custom, $this->updated, $this->eTag); + return sprintf( + "channel: %s, custom: %s, updated: %s, eTag: %s, Data: %s", + $this->channel, + $this->custom, + $this->updated, + $this->eTag, + $data_string + ); } /** @@ -84,27 +116,32 @@ public static function fromPayload(array $payload) $custom = null; $updated = null; $eTag = null; + $status = null; + $type = null; - if (array_key_exists("channel", $payload)) - { + if (array_key_exists("channel", $payload)) { $channel = PNMembership::fromPayload([ "data" => $payload["channel"] ]); } - if (array_key_exists("custom", $payload)) - { + if (array_key_exists("custom", $payload)) { $custom = $payload["custom"]; } - if (array_key_exists("updated", $payload)) - { + if (array_key_exists("status", $payload)) { + $status = $payload["status"]; + } + if (array_key_exists("type", $payload)) { + $type = $payload["type"]; + } + + if (array_key_exists("updated", $payload)) { $updated = $payload["updated"]; } - if (array_key_exists("eTag", $payload)) - { + if (array_key_exists("eTag", $payload)) { $eTag = $payload["eTag"]; } - return new PNMembershipsResultItem($channel, (object) $custom, $updated, $eTag); + return new PNMembershipsResultItem($channel, (object) $custom, $updated, $eTag, $status, $type); } -} \ No newline at end of file +} diff --git a/src/PubNub/Models/Consumer/Objects/PNIncludes.php b/src/PubNub/Models/Consumer/Objects/PNIncludes.php new file mode 100644 index 00000000..6d6d4a9a --- /dev/null +++ b/src/PubNub/Models/Consumer/Objects/PNIncludes.php @@ -0,0 +1,54 @@ + 'custom', + 'status' => 'status', + 'totalCount' => 'totalCount', + 'type' => 'type', + ]; + + public function __toString(): string + { + $result = []; + foreach ($this->mapping as $key => $value) { + if (isset($this->$key)) { + array_push($result, $value); + } + } + return implode(',', $result); + } + + public function custom(bool $custom = true): self + { + $this->custom = $custom; + return $this; + } + + public function status(bool $status = true): self + { + $this->status = $status; + return $this; + } + + public function totalCount(bool $totalCount = true): self + { + $this->totalCount = $totalCount; + return $this; + } + + public function type(bool $type = true): self + { + $this->type = $type; + return $this; + } +} diff --git a/src/PubNub/Models/Consumer/Objects/PNMemberData.php b/src/PubNub/Models/Consumer/Objects/PNMemberData.php deleted file mode 100644 index e7663654..00000000 --- a/src/PubNub/Models/Consumer/Objects/PNMemberData.php +++ /dev/null @@ -1,26 +0,0 @@ -/** @flow */ - -import type { UUIDMetadata } from '../uuid/uuid'; - -export type Member = {| - uuid: UUIDMetadata, - custom: ?any, - updated: string, - eTag: string, -|}; - -export type PaginatedResultParams = { - filter?: string, - sort?: { [key: string]: 'asc' | 'desc' | null }, - limit?: number, - page?: {| - next?: string, - prev?: string, - |}, - include?: {| - totalCount?: boolean, - customFields?: boolean, // custom - UUIDFields?: boolean, // uuid - customUUIDFields?: boolean, // uuid.custom - |}, -}; diff --git a/src/PubNub/Models/Consumer/Objects/PNMembershipData.php b/src/PubNub/Models/Consumer/Objects/PNMembershipData.php deleted file mode 100644 index ffd30f61..00000000 --- a/src/PubNub/Models/Consumer/Objects/PNMembershipData.php +++ /dev/null @@ -1,26 +0,0 @@ -/** @flow */ - -import type { ChannelMetadata } from '../channel/channel'; - -export type Membership = {| - channel: ChannelMetadata, - custom: ?any, - updated: string, - eTag: string, -|}; - -export type PaginatedResultParams = { - filter?: string, - sort?: { [key: string]: 'asc' | 'desc' | null }, - limit?: number, - page?: {| - next?: string, - prev?: string, - |}, - include?: {| - totalCount?: boolean, - customFields?: boolean, // custom - channelFields?: boolean, // channel - customChannelFields?: boolean, // channel.custom - |}, -}; diff --git a/src/PubNub/PubNub.php b/src/PubNub/PubNub.php index b646038a..4783c934 100644 --- a/src/PubNub/PubNub.php +++ b/src/PubNub/PubNub.php @@ -32,9 +32,11 @@ use PubNub\Endpoints\Objects\Member\SetMembers; use PubNub\Endpoints\Objects\Member\GetMembers; use PubNub\Endpoints\Objects\Member\RemoveMembers; +use PubNub\Endpoints\Objects\Member\ManageMembers; use PubNub\Endpoints\Objects\Membership\SetMemberships; use PubNub\Endpoints\Objects\Membership\GetMemberships; use PubNub\Endpoints\Objects\Membership\RemoveMemberships; +use PubNub\Endpoints\Objects\Membership\ManageMemberships; use PubNub\Endpoints\Presence\GetState; use PubNub\Endpoints\Presence\HereNow; use PubNub\Endpoints\Presence\SetState; @@ -400,6 +402,14 @@ public function removeMembers(): RemoveMembers return new RemoveMembers($this); } + /** + * @return ManageMembers + */ + public function manageMembers(): ManageMembers + { + return new ManageMembers($this); + } + /** * @return GetMemberships */ @@ -424,6 +434,14 @@ public function removeMemberships(): RemoveMemberships return new RemoveMemberships($this); } + /** + * @return ManageMemberships + */ + public function manageMemberships(): ManageMemberships + { + return new ManageMemberships($this); + } + /** * @return int */ diff --git a/tests/integrational/objects/member/GetMembersEndpointTest.php b/tests/integrational/objects/member/GetMembersEndpointTest.php index 26bd2d75..980462c8 100644 --- a/tests/integrational/objects/member/GetMembersEndpointTest.php +++ b/tests/integrational/objects/member/GetMembersEndpointTest.php @@ -4,7 +4,6 @@ use PubNubTestCase; - class GetMembersEndpointTest extends PubNubTestCase { public function testGetMembersForChannel() diff --git a/tests/integrational/objects/member/MembersHappyPathTest.php b/tests/integrational/objects/member/MembersHappyPathTest.php new file mode 100644 index 00000000..854e1865 --- /dev/null +++ b/tests/integrational/objects/member/MembersHappyPathTest.php @@ -0,0 +1,133 @@ +pubnub->removeMembers() + ->channel($this->channel) + ->members([ + new PNChannelMember($this->userName1), + new PNChannelMember($this->userName2), + new PNChannelMember($this->userName3), + ]) + ->sync(); + + $channelSetup = $this->pubnub->setChannelMetadata() + ->channel($this->channel) + ->meta([ + 'name' => 'Foodies', + 'description' => 'Best ThouTuba Creators on the planet', + ]) + ->sync(); + + $this->assertInstanceOf(PNSetChannelMetadataResult::class, $channelSetup); + + $userSetup1 = $this->pubnub->setUUIDMetadata() + ->uuid($this->userName1) + ->meta([ + 'name' => 'FoodMonarch', + 'description' => 'The Emperor of Foodies', + ]) + ->sync(); + + $this->assertInstanceOf(PNSetUUIDMetadataResult::class, $userSetup1); + + $userSetup2 = $this->pubnub->setUUIDMetadata() + ->uuid($this->userName2) + ->meta([ + 'name' => 'EpicFeastTime', + 'description' => 'It is Time for an Epic Feast', + ]) + ->sync(); + + $this->assertInstanceOf(PNSetUUIDMetadataResult::class, $userSetup2); + + $includes = new PNMemberIncludes(); + $includes->user()->userId()->userCustom()->userType()->userStatus()->custom()->status()->type(); + + $addMembers = $this->pubnub->setMembers() + ->channel($this->channel) + ->members([ + new PNChannelMember($this->userName1, ['BestDish' => 'Pizza'], 'Svensson', 'Active'), + new PNChannelMember($this->userName2, ['BestDish' => 'Lasagna'], 'Baconstrips', 'Retired'), + ]) + ->include($includes) + ->sync(); + $this->checkResponse($addMembers); + + $getMembers = $this->pubnub->getMembers() + ->channel($this->channel) + ->include($includes) + ->sync(); + $this->checkResponse($getMembers); + + $manageMembers = $this->pubnub->manageMembers() + ->channel($this->channel) + ->removeMembers([ + new PNChannelMember($this->userName1), + new PNChannelMember($this->userName2), + ]) + ->setMembers([ + new PNChannelMember($this->userName3, ['BestDish' => 'Everything'], 'Siemanko', 'StillKicking'), + ]) + ->include($includes) + ->sync(); + + $this->assertInstanceOf(PNMembersResult::class, $manageMembers); + $members = $manageMembers->getData(); + $this->assertCount(1, $members); + $piotrek = $members[0]; + $this->assertEquals($this->userName3, $piotrek->getUser()->getId()); + $this->assertEquals('Everything', $piotrek->getCustom()->BestDish); + $this->assertEquals('StillKicking', $piotrek->getStatus()); + $this->assertEquals('Siemanko', $piotrek->getType()); + + $removeMembers = $this->pubnub->removeMembers() + ->channel($this->channel) + ->members([ + new PNChannelMember($this->userName3), + ]) + ->include($includes) + ->sync(); + + $this->assertInstanceOf(PNMembersResult::class, $removeMembers); + $this->assertCount(0, $removeMembers->getData()); + } + + private function checkResponse(PNMembersResult $response): void + { + $this->assertInstanceOf(PNMembersResult::class, $response); + $members = $response->getData(); + $this->assertCount(2, $members); + $epic = $members[0]; + $monarch = $members[1]; + + $this->assertInstanceOf(PNMembersResultItem::class, $epic); + $this->assertInstanceOf(PNMembersResultItem::class, $monarch); + $this->assertEquals('FoodMonarch', $monarch->getUser()->getId()); + $this->assertEquals('EpicFeastTime', $epic->getUser()->getId()); + $this->assertEquals('Pizza', $monarch->getCustom()->BestDish); + $this->assertEquals('Lasagna', $epic->getCustom()->BestDish); + $this->assertEquals('Active', $monarch->getStatus()); + $this->assertEquals('Retired', $epic->getStatus()); + $this->assertEquals('Svensson', $monarch->getType()); + $this->assertEquals('Baconstrips', $epic->getType()); + } +} diff --git a/tests/integrational/objects/member/RemoveMembersEndpointTest.php b/tests/integrational/objects/member/RemoveMembersEndpointTest.php index bf771c03..87ef6473 100644 --- a/tests/integrational/objects/member/RemoveMembersEndpointTest.php +++ b/tests/integrational/objects/member/RemoveMembersEndpointTest.php @@ -4,7 +4,6 @@ use PubNubTestCase; - class RemoveMembersEndpointTest extends PubNubTestCase { public function testRemoveMembersForChannel() diff --git a/tests/integrational/objects/member/SetMembersEndpointTest.php b/tests/integrational/objects/member/SetMembersEndpointTest.php index d7d85c7b..8a3ca7b7 100644 --- a/tests/integrational/objects/member/SetMembersEndpointTest.php +++ b/tests/integrational/objects/member/SetMembersEndpointTest.php @@ -4,7 +4,6 @@ use PubNubTestCase; - class SetMembersEndpointTest extends PubNubTestCase { public function testSetMembersForChannel() @@ -24,21 +23,21 @@ public function testSetMembersForChannel() $data = $response->getData(); $this->assertNotEmpty($data); $this->assertEquals(3, count($data)); - + $this->assertNotEmpty($data[0]->getUUID()); $this->assertEquals("uuid", $data[0]->getUUID()->getId()); $this->assertNotEmpty($data[0]->getCustom()); $custom_data = $data[0]->getCustom(); $this->assertEquals("aa", $custom_data->a); $this->assertEquals("bb", $custom_data->b); - + $this->assertNotEmpty($data[1]->getUUID()); $this->assertEquals("uuid1", $data[1]->getUUID()->getId()); $this->assertNotEmpty($data[1]->getCustom()); $custom_data = $data[1]->getCustom(); $this->assertEquals("aa", $custom_data->a); $this->assertEquals("bb", $custom_data->b); - + $this->assertNotEmpty($data[2]->getUUID()); $this->assertEquals("uuid2", $data[2]->getUUID()->getId()); $this->assertNotEmpty($data[2]->getCustom()); diff --git a/tests/integrational/objects/membership/GetMembershipsEndpointTest.php b/tests/integrational/objects/membership/GetMembershipsEndpointTest.php index 91845913..312e84e6 100644 --- a/tests/integrational/objects/membership/GetMembershipsEndpointTest.php +++ b/tests/integrational/objects/membership/GetMembershipsEndpointTest.php @@ -4,7 +4,6 @@ use PubNubTestCase; - class GetMembershipsEndpointTest extends PubNubTestCase { public function testGetMemberships() diff --git a/tests/integrational/objects/membership/MembershipsHappyPathTest.php b/tests/integrational/objects/membership/MembershipsHappyPathTest.php new file mode 100644 index 00000000..949d35b7 --- /dev/null +++ b/tests/integrational/objects/membership/MembershipsHappyPathTest.php @@ -0,0 +1,106 @@ +pubnub->removeMemberships() + ->userId($this->user) + ->memberships([ + new PNChannelMembership($this->channel1), + new PNChannelMembership($this->channel2), + new PNChannelMembership($this->channel3), + ]) + ->sync(); + $this->assertInstanceOf(PNMembershipsResult::class, $cleanup); + $this->assertCount(0, $cleanup->getData()); + + $includes = new PNMembershipIncludes(); + $includes->channel()->channelId()->channelCustom()->channelType()->channelStatus()->custom()->status()->type(); + + $addMembership = $this->pubnub->setMemberships() + ->userId($this->user) + ->memberships([ + new PNChannelMembership($this->channel1, ['BestDish' => 'Pizza'], 'Admin', 'Active'), + new PNChannelMembership($this->channel2, ['BestDish' => 'Lasagna'], 'Guest', 'Away'), + ]) + ->include($includes) + ->sync(); + + $this->checkResponse($addMembership); + + $getMembership = $this->pubnub->getMemberships() + ->userId($this->user) + ->include($includes) + ->sync(); + $this->checkResponse($getMembership); + + $manageMembership = $this->pubnub->manageMemberships() + ->userId($this->user) + ->removeMemberships([ + new PNChannelMembership($this->channel1), + new PNChannelMembership($this->channel2), + ]) + ->setMemberships([ + new PNChannelMembership($this->channel3, ['BestDish' => 'Everything'], 'Moderator', 'Omnomnomnom'), + ]) + ->include($includes) + ->sync(); + + $this->assertInstanceOf(PNMembershipsResult::class, $manageMembership); + $memberships = $manageMembership->getData(); + $this->assertCount(1, $memberships); + $pierogies = $memberships[0]; + $this->assertEquals($this->channel3, $pierogies->getChannel()->getId()); + $this->assertEquals('Everything', $pierogies->getCustom()->BestDish); + $this->assertEquals('Omnomnomnom', $pierogies->getStatus()); + $this->assertEquals('Moderator', $pierogies->getType()); + + $removeMembers = $this->pubnub->removeMemberships() + ->userId($this->user) + ->memberships([ + new PNChannelMembership($this->channel1), + new PNChannelMembership($this->channel2), + new PNChannelMembership($this->channel3), + ]) + ->sync(); + + $this->assertInstanceOf(PNMembershipsResult::class, $removeMembers); + $this->assertCount(0, $removeMembers->getData()); + } + + private function checkResponse(PNMembershipsResult $response): void + { + $this->assertInstanceOf(PNMembershipsResult::class, $response); + $memberships = $response->getData(); + $this->assertCount(2, $memberships); + $ch2 = $memberships[0]; + $ch1 = $memberships[1]; + + $this->assertInstanceOf(PNMembershipsResultItem::class, $ch1); + $this->assertInstanceOf(PNMembershipsResultItem::class, $ch2); + $this->assertEquals($this->channel1, $ch1->getChannel()->getId()); + $this->assertEquals($this->channel2, $ch2->getChannel()->getId()); + $this->assertEquals('Pizza', $ch1->getCustom()->BestDish); + $this->assertEquals('Lasagna', $ch2->getCustom()->BestDish); + $this->assertEquals('Active', $ch1->getStatus()); + $this->assertEquals('Away', $ch2->getStatus()); + $this->assertEquals('Admin', $ch1->getType()); + $this->assertEquals('Guest', $ch2->getType()); + } +} diff --git a/tests/integrational/objects/membership/RemoveMembershipsEndpointTest.php b/tests/integrational/objects/membership/RemoveMembershipsEndpointTest.php index e3b4bd72..619f5d9f 100644 --- a/tests/integrational/objects/membership/RemoveMembershipsEndpointTest.php +++ b/tests/integrational/objects/membership/RemoveMembershipsEndpointTest.php @@ -4,7 +4,6 @@ use PubNubTestCase; - class RemoveMembershipsEndpointTest extends PubNubTestCase { public function testGetMemberships() diff --git a/tests/integrational/objects/membership/SetMembershipsEndpointTest.php b/tests/integrational/objects/membership/SetMembershipsEndpointTest.php index b5150508..4890c127 100644 --- a/tests/integrational/objects/membership/SetMembershipsEndpointTest.php +++ b/tests/integrational/objects/membership/SetMembershipsEndpointTest.php @@ -4,7 +4,6 @@ use PubNubTestCase; - class SetMembershipsEndpointTest extends PubNubTestCase { public function testSetMembershipsForChannel() @@ -24,21 +23,21 @@ public function testSetMembershipsForChannel() $data = $response->getData(); $this->assertNotEmpty($data); $this->assertEquals(3, count($data)); - + $this->assertNotEmpty($data[0]->getChannel()); $this->assertEquals("ch", $data[0]->getChannel()->getId()); $this->assertNotEmpty($data[0]->getCustom()); $custom_data = $data[0]->getCustom(); $this->assertEquals("aa", $custom_data->a); $this->assertEquals("bb", $custom_data->b); - + $this->assertNotEmpty($data[1]->getChannel()); $this->assertEquals("ch1", $data[1]->getChannel()->getId()); $this->assertNotEmpty($data[1]->getCustom()); $custom_data = $data[1]->getCustom(); $this->assertEquals("aa", $custom_data->a); $this->assertEquals("bb", $custom_data->b); - + $this->assertNotEmpty($data[2]->getChannel()); $this->assertEquals("ch2", $data[2]->getChannel()->getId()); $this->assertNotEmpty($data[2]->getCustom()); From f587c3cb19e6f2dc42dd1fc09d540511b2177607 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Mon, 3 Feb 2025 15:13:24 +0100 Subject: [PATCH 2/9] debug --- tests/integrational/objects/member/MembersHappyPathTest.php | 2 +- .../objects/membership/MembershipsHappyPathTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integrational/objects/member/MembersHappyPathTest.php b/tests/integrational/objects/member/MembersHappyPathTest.php index 854e1865..5599cc70 100644 --- a/tests/integrational/objects/member/MembersHappyPathTest.php +++ b/tests/integrational/objects/member/MembersHappyPathTest.php @@ -115,7 +115,7 @@ private function checkResponse(PNMembersResult $response): void { $this->assertInstanceOf(PNMembersResult::class, $response); $members = $response->getData(); - $this->assertCount(2, $members); + $this->assertCount(2, $members, print_r($members, true)); $epic = $members[0]; $monarch = $members[1]; diff --git a/tests/integrational/objects/membership/MembershipsHappyPathTest.php b/tests/integrational/objects/membership/MembershipsHappyPathTest.php index 949d35b7..39b55219 100644 --- a/tests/integrational/objects/membership/MembershipsHappyPathTest.php +++ b/tests/integrational/objects/membership/MembershipsHappyPathTest.php @@ -28,7 +28,7 @@ public function testHappyPath(): void ]) ->sync(); $this->assertInstanceOf(PNMembershipsResult::class, $cleanup); - $this->assertCount(0, $cleanup->getData()); + $this->assertCount(0, $cleanup->getData(), print_r($cleanup->getData(), true)); $includes = new PNMembershipIncludes(); $includes->channel()->channelId()->channelCustom()->channelType()->channelStatus()->custom()->status()->type(); From bdaaa9f8a30370846af98a33881d5924a87bdd47 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Mon, 3 Feb 2025 15:31:21 +0100 Subject: [PATCH 3/9] Fix test with stale membership cleanup --- .../objects/member/MembersHappyPathTest.php | 18 ++++++++++-------- .../membership/MembershipsHappyPathTest.php | 17 +++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/tests/integrational/objects/member/MembersHappyPathTest.php b/tests/integrational/objects/member/MembersHappyPathTest.php index 5599cc70..afd24e77 100644 --- a/tests/integrational/objects/member/MembersHappyPathTest.php +++ b/tests/integrational/objects/member/MembersHappyPathTest.php @@ -20,14 +20,16 @@ class MembersHappyPathTest extends PubNubTestCase public function testHappyPath(): void { // Cleanup + $staleMembers = []; + $getStaleMembers = $this->pubnub->getMembers()->channel($this->channel)->sync(); + foreach ($getStaleMembers->getData() as $member) { + array_push($staleMembers, new PNChannelMember($member->getUser()->getId())); + } + $this->pubnub->removeMembers() - ->channel($this->channel) - ->members([ - new PNChannelMember($this->userName1), - new PNChannelMember($this->userName2), - new PNChannelMember($this->userName3), - ]) - ->sync(); + ->channel($this->channel) + ->members($staleMembers) + ->sync(); $channelSetup = $this->pubnub->setChannelMetadata() ->channel($this->channel) @@ -115,7 +117,7 @@ private function checkResponse(PNMembersResult $response): void { $this->assertInstanceOf(PNMembersResult::class, $response); $members = $response->getData(); - $this->assertCount(2, $members, print_r($members, true)); + $this->assertCount(2, $members); $epic = $members[0]; $monarch = $members[1]; diff --git a/tests/integrational/objects/membership/MembershipsHappyPathTest.php b/tests/integrational/objects/membership/MembershipsHappyPathTest.php index 39b55219..28a63024 100644 --- a/tests/integrational/objects/membership/MembershipsHappyPathTest.php +++ b/tests/integrational/objects/membership/MembershipsHappyPathTest.php @@ -2,7 +2,6 @@ namespace PubNubTests\integrational\objects\member; -use PubNub\Models\Consumer\Objects\Channel\PNSetChannelMetadataResult; use PubNubTestCase; use PubNub\Models\Consumer\Objects\Membership\PNChannelMembership; use PubNub\Models\Consumer\Objects\Membership\PNMembershipIncludes; @@ -19,16 +18,14 @@ class MembershipsHappyPathTest extends PubNubTestCase public function testHappyPath(): void { // Cleanup - $cleanup = $this->pubnub->removeMemberships() - ->userId($this->user) - ->memberships([ - new PNChannelMembership($this->channel1), - new PNChannelMembership($this->channel2), - new PNChannelMembership($this->channel3), - ]) - ->sync(); + $staleMemberships = []; + $getStaleMemberships = $this->pubnub->getMemberships()->userId($this->user)->sync(); + foreach ($getStaleMemberships->getData() as $membership) { + array_push($staleMemberships, new PNChannelMembership($membership->getChannel()->getId())); + } + $cleanup = $this->pubnub->removeMemberships()->userId($this->user)->memberships($staleMemberships)->sync(); $this->assertInstanceOf(PNMembershipsResult::class, $cleanup); - $this->assertCount(0, $cleanup->getData(), print_r($cleanup->getData(), true)); + $this->assertCount(0, $cleanup->getData()); $includes = new PNMembershipIncludes(); $includes->channel()->channelId()->channelCustom()->channelType()->channelStatus()->custom()->status()->type(); From 5b631f85d124c5761f39899c58e39623d2d07e91 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Mon, 3 Feb 2025 16:00:20 +0100 Subject: [PATCH 4/9] fix test and linter --- src/PubNub/Endpoints/Endpoint.php | 4 ++-- .../Objects/Member/ManageMembers.php | 5 +++-- .../Objects/Membership/ManageMemberships.php | 22 ++++++++++--------- .../Objects/Membership/RemoveMemberships.php | 6 ++--- .../Objects/Membership/SetMemberships.php | 4 ++-- .../Membership/PNMembershipsResult.php | 2 +- .../Membership/PNMembershipsResultItem.php | 3 ++- .../objects/member/GetMembersEndpointTest.php | 7 +++++- .../objects/member/MembersHappyPathTest.php | 9 ++++---- .../objects/member/SetMembersEndpointTest.php | 7 +++++- .../membership/GetMembershipsEndpointTest.php | 7 +++++- .../membership/MembershipsHappyPathTest.php | 8 ++++--- .../membership/SetMembershipsEndpointTest.php | 7 +++++- 13 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/PubNub/Endpoints/Endpoint.php b/src/PubNub/Endpoints/Endpoint.php index 474991d6..a4b79ace 100755 --- a/src/PubNub/Endpoints/Endpoint.php +++ b/src/PubNub/Endpoints/Endpoint.php @@ -130,7 +130,7 @@ protected function validateSubscribeKey() { $subscribeKey = $this->pubnub->getConfiguration()->getSubscribeKey(); - if ($subscribeKey == null || empty($subscribeKey)) { + if (strlen($subscribeKey) === 0) { throw new PubNubValidationException("Subscribe Key not configured"); } } @@ -142,7 +142,7 @@ protected function validatePublishKey() { $publishKey = $this->pubnub->getConfiguration()->getPublishKey(); - if ($publishKey == null || empty($publishKey)) { + if (strlen($publishKey) === 0) { throw new PubNubValidationException("Publish Key not configured"); } } diff --git a/src/PubNub/Endpoints/Objects/Member/ManageMembers.php b/src/PubNub/Endpoints/Objects/Member/ManageMembers.php index 15f9f704..b789bbe3 100644 --- a/src/PubNub/Endpoints/Objects/Member/ManageMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/ManageMembers.php @@ -139,6 +139,7 @@ public function includeFields($include) /** * @throws PubNubValidationException + * @return void */ protected function validateParams() { @@ -214,7 +215,7 @@ protected function buildPath() } /** - * @param array $result Decoded json + * @param mixed $result Decoded json * @return PNMembersResult */ protected function createResponse($result): PNMembersResult @@ -228,7 +229,7 @@ public function sync(): PNMembersResult } /** - * @return array + * @return string[] */ protected function customParams() { diff --git a/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php b/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php index 3dbe4fc1..74651121 100644 --- a/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php @@ -6,6 +6,7 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; +use PubNub\Exceptions\PubNubBuildRequestException; use PubNub\Models\Consumer\Objects\Membership\PNMembershipIncludes; use PubNub\Models\Consumer\Objects\Membership\PNChannelMembership; use PubNub\Models\Consumer\Objects\Membership\PNMembershipsResult; @@ -40,10 +41,10 @@ class ManageMemberships extends ObjectsCollectionEndpoint protected ?PNMembershipIncludes $includes; /** @var ?PNChannelMembership[] */ - protected array $setMemberships; + protected ?array $setMemberships; /** @var ?PNChannelMembership[] */ - protected array $removeMemberships; + protected ?array $removeMemberships; /** * @param PubNub $pubnubInstance @@ -76,7 +77,7 @@ public function userId($userId): self } /** - * @param array $channels + * @param mixed $channels * @deprecated Use memberships() method * * @return $this @@ -88,7 +89,7 @@ public function setChannels($channels): self } /** - * @param array $channels + * @param mixed $channels * @deprecated Use memberships() method * * @return $this @@ -100,7 +101,7 @@ public function removeChannels($channels): self } /** - * @param PNChannelMemberhips[] $members + * @param PNChannelMembership[] $memberships * @return $this */ public function setMemberships(array $memberships): self @@ -110,7 +111,7 @@ public function setMemberships(array $memberships): self } /** - * @param PNChannelMemberhips[] $members + * @param PNChannelMembership[] $memberships * @return $this */ public function removeMemberships(array $memberships): self @@ -120,7 +121,7 @@ public function removeMemberships(array $memberships): self } /** - * @param array $custom + * @param mixed $custom * @deprecated Use members() method * * @return $this @@ -132,7 +133,7 @@ public function custom($custom): self } /** - * @param array $include + * @param string[] $include * @deprecated Use includes() method * * @return $this @@ -162,6 +163,7 @@ public function include(PNMembershipIncludes $includes): self /** * @throws PubNubValidationException + * @return void */ protected function validateParams() { @@ -236,7 +238,7 @@ public function sync(): PNMembershipsResult } /** - * @param array $result Decoded json + * @param mixed $result Decoded json * @return PNMembershipsResult */ protected function createResponse($result): PNMembershipsResult @@ -245,7 +247,7 @@ protected function createResponse($result): PNMembershipsResult } /** - * @return array + * @return string[] */ protected function customParams() { diff --git a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php index 64a51242..bcd19d45 100644 --- a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php @@ -33,7 +33,7 @@ class RemoveMemberships extends ObjectsCollectionEndpoint /** @var PNMembershipIncludes */ protected ?PNMembershipIncludes $includes; - /** @var ?PNChannelMembership[] */ + /** @var PNChannelMembership[] */ protected array $memberships; /** @@ -58,7 +58,7 @@ public function uuid(string $uuid): self } /** - * @param string $uuid + * @param string $userId * @return $this */ public function userId(string $userId): self @@ -81,7 +81,7 @@ public function channels($channels): self } /** - * @param PNChannelMemberhips[] $members + * @param PNChannelMembership[] $memberships * @return $this */ public function memberships(array $memberships): self diff --git a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php index e6d58f1c..08c7f1a2 100644 --- a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php @@ -36,7 +36,7 @@ class SetMemberships extends ObjectsCollectionEndpoint /** @var PNMembershipIncludes */ protected ?PNMembershipIncludes $includes; - /** @var ?PNChannelMembership[] */ + /** @var PNChannelMembership[] */ protected array $memberships; /** @@ -82,7 +82,7 @@ public function channels($channels): self } /** - * @param PNChannelMemberhips[] $members + * @param PNChannelMembership[] $memberships * @return $this */ public function memberships(array $memberships): self diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php index f126f619..17a6b384 100755 --- a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResult.php @@ -56,7 +56,7 @@ public function getNext() } /** - * @return array + * @return array | \StdClass */ public function getData() { diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php index 51df2f3f..5b69f424 100755 --- a/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNMembershipsResultItem.php @@ -51,7 +51,7 @@ public function getChannel() } /** - * @return array + * @return array | \StdClass */ public function getCustom() { @@ -92,6 +92,7 @@ public function getETag() public function __toString() { + $data_string = ''; if (!empty($data)) { $data_string = json_encode($data); } diff --git a/tests/integrational/objects/member/GetMembersEndpointTest.php b/tests/integrational/objects/member/GetMembersEndpointTest.php index 980462c8..e042f5bd 100644 --- a/tests/integrational/objects/member/GetMembersEndpointTest.php +++ b/tests/integrational/objects/member/GetMembersEndpointTest.php @@ -19,7 +19,12 @@ public function testGetMembersForChannel() $response = $this->pubnub_pam->getMembers() ->channel("ch") - ->includeFields([ "totalCount" => true, "customFields" => true, "customChannelFields" => true, "channelFields" => true ]) + ->includeFields([ + "totalCount" => true, + "customFields" => true, + "customChannelFields" => true, + "channelFields" => true, + ]) ->sync(); $this->assertNotEmpty($response); diff --git a/tests/integrational/objects/member/MembersHappyPathTest.php b/tests/integrational/objects/member/MembersHappyPathTest.php index afd24e77..308fcb25 100644 --- a/tests/integrational/objects/member/MembersHappyPathTest.php +++ b/tests/integrational/objects/member/MembersHappyPathTest.php @@ -26,10 +26,11 @@ public function testHappyPath(): void array_push($staleMembers, new PNChannelMember($member->getUser()->getId())); } - $this->pubnub->removeMembers() - ->channel($this->channel) - ->members($staleMembers) - ->sync(); + if (!empty($staleMembers)) { + $cleanup = $this->pubnub->removeMembers()->channel($this->channel)->members($staleMembers)->sync(); + $this->assertInstanceOf(PNSetChannelMetadataResult::class, $cleanup); + $this->assertCount(0, $cleanup->getData()); + } $channelSetup = $this->pubnub->setChannelMetadata() ->channel($this->channel) diff --git a/tests/integrational/objects/member/SetMembersEndpointTest.php b/tests/integrational/objects/member/SetMembersEndpointTest.php index 8a3ca7b7..fac2c8d6 100644 --- a/tests/integrational/objects/member/SetMembersEndpointTest.php +++ b/tests/integrational/objects/member/SetMembersEndpointTest.php @@ -15,7 +15,12 @@ public function testSetMembersForChannel() "a" => "aa", "b" => "bb", ]) - ->includeFields([ "totalCount" => true, "customFields" => true, "customChannelFields" => true, "channelFields" => true ]) + ->includeFields([ + "totalCount" => true, + "customFields" => true, + "customChannelFields" => true, + "channelFields" => true, + ]) ->sync(); diff --git a/tests/integrational/objects/membership/GetMembershipsEndpointTest.php b/tests/integrational/objects/membership/GetMembershipsEndpointTest.php index 312e84e6..c07a7660 100644 --- a/tests/integrational/objects/membership/GetMembershipsEndpointTest.php +++ b/tests/integrational/objects/membership/GetMembershipsEndpointTest.php @@ -19,7 +19,12 @@ public function testGetMemberships() $response = $this->pubnub_pam->getMemberships() ->uuid("uuid") - ->includeFields([ "totalCount" => true, "customFields" => true, "customUUIDFields" => true, "UUIDFields" => true ]) + ->includeFields([ + "totalCount" => true, + "customFields" => true, + "customUUIDFields" => true, + "UUIDFields" => true, + ]) ->sync(); $this->assertNotEmpty($response); diff --git a/tests/integrational/objects/membership/MembershipsHappyPathTest.php b/tests/integrational/objects/membership/MembershipsHappyPathTest.php index 28a63024..861fc46f 100644 --- a/tests/integrational/objects/membership/MembershipsHappyPathTest.php +++ b/tests/integrational/objects/membership/MembershipsHappyPathTest.php @@ -23,9 +23,11 @@ public function testHappyPath(): void foreach ($getStaleMemberships->getData() as $membership) { array_push($staleMemberships, new PNChannelMembership($membership->getChannel()->getId())); } - $cleanup = $this->pubnub->removeMemberships()->userId($this->user)->memberships($staleMemberships)->sync(); - $this->assertInstanceOf(PNMembershipsResult::class, $cleanup); - $this->assertCount(0, $cleanup->getData()); + if (!empty($staleMemberships)) { + $cleanup = $this->pubnub->removeMemberships()->userId($this->user)->memberships($staleMemberships)->sync(); + $this->assertInstanceOf(PNMembershipsResult::class, $cleanup); + $this->assertCount(0, $cleanup->getData()); + } $includes = new PNMembershipIncludes(); $includes->channel()->channelId()->channelCustom()->channelType()->channelStatus()->custom()->status()->type(); diff --git a/tests/integrational/objects/membership/SetMembershipsEndpointTest.php b/tests/integrational/objects/membership/SetMembershipsEndpointTest.php index 4890c127..8d416838 100644 --- a/tests/integrational/objects/membership/SetMembershipsEndpointTest.php +++ b/tests/integrational/objects/membership/SetMembershipsEndpointTest.php @@ -15,7 +15,12 @@ public function testSetMembershipsForChannel() "a" => "aa", "b" => "bb", ]) - ->includeFields([ "totalCount" => true, "customFields" => true, "customChannelFields" => true, "channelFields" => true ]) + ->includeFields([ + "totalCount" => true, + "customFields" => true, + "customChannelFields" => true, + "channelFields" => true + ]) ->sync(); From 67432f21b5c2570a7febc9bcd33af743ba172f73 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Mon, 3 Feb 2025 16:45:20 +0100 Subject: [PATCH 5/9] type typo --- tests/integrational/objects/member/MembersHappyPathTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrational/objects/member/MembersHappyPathTest.php b/tests/integrational/objects/member/MembersHappyPathTest.php index 308fcb25..78591f48 100644 --- a/tests/integrational/objects/member/MembersHappyPathTest.php +++ b/tests/integrational/objects/member/MembersHappyPathTest.php @@ -28,7 +28,7 @@ public function testHappyPath(): void if (!empty($staleMembers)) { $cleanup = $this->pubnub->removeMembers()->channel($this->channel)->members($staleMembers)->sync(); - $this->assertInstanceOf(PNSetChannelMetadataResult::class, $cleanup); + $this->assertInstanceOf(PNMembersResult::class, $cleanup); $this->assertCount(0, $cleanup->getData()); } From e33dfc540898c61a174c6d14cf322ed23ee9618c Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Mon, 3 Feb 2025 20:18:25 +0100 Subject: [PATCH 6/9] test --- .../objects/member/MembersHappyPathTest.php | 9 ++++++++- .../objects/membership/MembershipsHappyPathTest.php | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/integrational/objects/member/MembersHappyPathTest.php b/tests/integrational/objects/member/MembersHappyPathTest.php index 78591f48..1faf3e5b 100644 --- a/tests/integrational/objects/member/MembersHappyPathTest.php +++ b/tests/integrational/objects/member/MembersHappyPathTest.php @@ -17,7 +17,7 @@ class MembersHappyPathTest extends PubNubTestCase protected string $userName2 = "EpicFeastTime"; protected string $userName3 = "PiotrekOgi"; - public function testHappyPath(): void + protected function cleanup(): void { // Cleanup $staleMembers = []; @@ -31,7 +31,12 @@ public function testHappyPath(): void $this->assertInstanceOf(PNMembersResult::class, $cleanup); $this->assertCount(0, $cleanup->getData()); } + } + public function testHappyPath(): void + { + $this->cleanup(); + sleep(1); $channelSetup = $this->pubnub->setChannelMetadata() ->channel($this->channel) ->meta([ @@ -112,6 +117,8 @@ public function testHappyPath(): void $this->assertInstanceOf(PNMembersResult::class, $removeMembers); $this->assertCount(0, $removeMembers->getData()); + $this->cleanup(); + sleep(1); } private function checkResponse(PNMembersResult $response): void diff --git a/tests/integrational/objects/membership/MembershipsHappyPathTest.php b/tests/integrational/objects/membership/MembershipsHappyPathTest.php index 861fc46f..1e2d5321 100644 --- a/tests/integrational/objects/membership/MembershipsHappyPathTest.php +++ b/tests/integrational/objects/membership/MembershipsHappyPathTest.php @@ -28,10 +28,10 @@ public function testHappyPath(): void $this->assertInstanceOf(PNMembershipsResult::class, $cleanup); $this->assertCount(0, $cleanup->getData()); } + sleep(1); $includes = new PNMembershipIncludes(); $includes->channel()->channelId()->channelCustom()->channelType()->channelStatus()->custom()->status()->type(); - $addMembership = $this->pubnub->setMemberships() ->userId($this->user) ->memberships([ From fc9d965da5eb8b62a6de0f17828dfe1ba1b30946 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Mon, 3 Feb 2025 20:24:39 +0100 Subject: [PATCH 7/9] one more sleep --- .../objects/membership/MembershipsHappyPathTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrational/objects/membership/MembershipsHappyPathTest.php b/tests/integrational/objects/membership/MembershipsHappyPathTest.php index 1e2d5321..65fdff3a 100644 --- a/tests/integrational/objects/membership/MembershipsHappyPathTest.php +++ b/tests/integrational/objects/membership/MembershipsHappyPathTest.php @@ -42,7 +42,7 @@ public function testHappyPath(): void ->sync(); $this->checkResponse($addMembership); - + sleep(1); $getMembership = $this->pubnub->getMemberships() ->userId($this->user) ->include($includes) From d4fc4a150609c39b90ea38b3037ef37c23f48f0e Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Tue, 4 Feb 2025 14:13:45 +0100 Subject: [PATCH 8/9] review fixes --- phpstan-baseline.neon | 45 ------------------- .../Endpoints/Objects/Member/GetMembers.php | 24 +++++----- .../Objects/Member/ManageMembers.php | 25 +++++------ .../Objects/Member/RemoveMembers.php | 27 +++++------ .../Endpoints/Objects/Member/SetMembers.php | 31 ++++++------- .../Objects/Membership/GetMemberships.php | 36 +++++++++------ .../Objects/Membership/ManageMemberships.php | 34 +++++++------- .../Objects/Membership/RemoveMemberships.php | 20 ++++----- .../Objects/Membership/SetMemberships.php | 28 ++++++------ .../Objects/Member/PNChannelMember.php | 2 +- .../Membership/PNChannelMembership.php | 10 ++--- 11 files changed, 114 insertions(+), 168 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index bc22c40b..5baf1f0b 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1371,16 +1371,6 @@ parameters: count: 1 path: src/PubNub/Endpoints/Objects/Member/GetMembers.php - - - message: "#^Method PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\GetMembers\\:\\:validateParams\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Member/GetMembers.php - - - - message: "#^PHPDoc tag @throws with type PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\PubNubBuildRequestException is not subtype of Throwable$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Member/GetMembers.php - - message: "#^Property PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\GetMembers\\:\\:\\$include type has no value type specified in iterable type array\\.$#" count: 1 @@ -1406,16 +1396,6 @@ parameters: count: 1 path: src/PubNub/Endpoints/Objects/Member/RemoveMembers.php - - - message: "#^Method PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\RemoveMembers\\:\\:validateParams\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Member/RemoveMembers.php - - - - message: "#^PHPDoc tag @throws with type PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\PubNubBuildRequestException is not subtype of Throwable$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Member/RemoveMembers.php - - message: "#^Property PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\RemoveMembers\\:\\:\\$include type has no value type specified in iterable type array\\.$#" count: 1 @@ -1451,16 +1431,6 @@ parameters: count: 1 path: src/PubNub/Endpoints/Objects/Member/SetMembers.php - - - message: "#^Method PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\SetMembers\\:\\:validateParams\\(\\) has no return type specified\\.$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Member/SetMembers.php - - - - message: "#^PHPDoc tag @throws with type PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\PubNubBuildRequestException is not subtype of Throwable$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Member/SetMembers.php - - message: "#^Property PubNub\\\\Endpoints\\\\Objects\\\\Member\\\\SetMembers\\:\\:\\$custom type has no value type specified in iterable type array\\.$#" count: 1 @@ -1501,11 +1471,6 @@ parameters: count: 1 path: src/PubNub/Endpoints/Objects/Membership/GetMemberships.php - - - message: "#^PHPDoc tag @throws with type PubNub\\\\Endpoints\\\\Objects\\\\Membership\\\\PubNubBuildRequestException is not subtype of Throwable$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Membership/GetMemberships.php - - message: "#^Property PubNub\\\\Endpoints\\\\Objects\\\\Membership\\\\GetMemberships\\:\\:\\$include type has no value type specified in iterable type array\\.$#" count: 1 @@ -1536,11 +1501,6 @@ parameters: count: 1 path: src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php - - - message: "#^PHPDoc tag @throws with type PubNub\\\\Endpoints\\\\Objects\\\\Membership\\\\PubNubBuildRequestException is not subtype of Throwable$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php - - message: "#^Property PubNub\\\\Endpoints\\\\Objects\\\\Membership\\\\RemoveMemberships\\:\\:\\$channels type has no value type specified in iterable type array\\.$#" count: 1 @@ -1581,11 +1541,6 @@ parameters: count: 1 path: src/PubNub/Endpoints/Objects/Membership/SetMemberships.php - - - message: "#^PHPDoc tag @throws with type PubNub\\\\Endpoints\\\\Objects\\\\Membership\\\\PubNubBuildRequestException is not subtype of Throwable$#" - count: 1 - path: src/PubNub/Endpoints/Objects/Membership/SetMemberships.php - - message: "#^Property PubNub\\\\Endpoints\\\\Objects\\\\Membership\\\\SetMemberships\\:\\:\\$channels type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/src/PubNub/Endpoints/Objects/Member/GetMembers.php b/src/PubNub/Endpoints/Objects/Member/GetMembers.php index 57f37d59..9bc498c2 100644 --- a/src/PubNub/Endpoints/Objects/Member/GetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/GetMembers.php @@ -20,11 +20,12 @@ class GetMembers extends ObjectsCollectionEndpoint protected string $endpointName = "GetMembers"; /** @var string */ - protected $channel; + protected ?string $channel; /** @var array */ - protected $include = []; + protected array $include = []; + /** @var PNMemberIncludes */ protected ?PNMemberIncludes $includes; /** @@ -38,12 +39,12 @@ public function __construct(PubNub $pubnubInstance) } /** - * @param string $ch + * @param string $channel * @return $this */ - public function channel($ch) + public function channel(string $channel): self { - $this->channel = $ch; + $this->channel = $channel; return $this; } @@ -58,7 +59,7 @@ public function include(PNMemberIncludes $includes): self * @param array $include * @return $this */ - public function includeFields($include) + public function includeFields(array $include): self { $this->include = $include; @@ -67,19 +68,20 @@ public function includeFields($include) /** * @throws PubNubValidationException + * @return void */ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->channel)) { + if (empty($this->channel)) { throw new PubNubValidationException("channel missing"); } } /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { @@ -136,10 +138,8 @@ protected function customParams() array_push($includes, 'uuid'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } diff --git a/src/PubNub/Endpoints/Objects/Member/ManageMembers.php b/src/PubNub/Endpoints/Objects/Member/ManageMembers.php index b789bbe3..cd1db2bb 100644 --- a/src/PubNub/Endpoints/Objects/Member/ManageMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/ManageMembers.php @@ -22,16 +22,16 @@ class ManageMembers extends ObjectsCollectionEndpoint protected string $endpointName = "ManageMembers"; /** @var string */ - protected $channel; + protected ?string $channel; /** @var string[] */ - protected $setUuids; + protected array $setUuids; /** @var string[] */ - protected $removeUuids; + protected array $removeUuids; /** @var string[] */ - protected $include = []; + protected array $include = []; /** @var PNMemberIncludes */ protected PNMemberIncludes $includes; @@ -56,10 +56,9 @@ public function __construct(PubNub $pubnubInstance) * @param string $ch * @return $this */ - public function channel($ch) + public function channel(string $ch): self { $this->channel = $ch; - return $this; } @@ -93,7 +92,7 @@ public function removeUuids($uuids): self * @param PNChannelMember[] $setMembers * @return $this */ - public function setMembers(array $setMembers) + public function setMembers(array $setMembers): self { $this->setMembers = $setMembers; return $this; @@ -145,7 +144,7 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->channel)) { + if (empty($this->channel)) { throw new PubNubValidationException("channel missing"); } $members = !empty($this->setMembers) or !empty($this->removeMembers); @@ -162,11 +161,11 @@ protected function validateParams() /** * @return string + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { $set = []; - $remove = []; if (!empty($this->setMembers)) { foreach ($this->setMembers as $member) { array_push($set, $member->toArray()); @@ -181,7 +180,7 @@ protected function buildData() array_push($set, $entry); } } - + $remove = []; if (!empty($this->removeMembers)) { foreach ($this->removeMembers as $member) { array_push($remove, $member->toArray()); @@ -253,10 +252,8 @@ protected function customParams() array_push($includes, 'uuid'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } } diff --git a/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php b/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php index 0b880bf4..2e63ee40 100644 --- a/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php @@ -22,13 +22,13 @@ class RemoveMembers extends ObjectsCollectionEndpoint protected string $endpointName = "ManageMembers"; /** @var string */ - protected $channel; + protected ?string $channel; /** @var array */ - protected $uuids; + protected array $uuids; /** @var array */ - protected $include = []; + protected array $include = []; /** @var PNMemberIncludes */ protected PNMemberIncludes $includes; @@ -50,7 +50,7 @@ public function __construct(PubNub $pubnubInstance) * @param string $ch * @return $this */ - public function channel($ch) + public function channel(string $ch): self { $this->channel = $ch; @@ -63,7 +63,7 @@ public function channel($ch) * * @return $this */ - public function uuids($uuids) + public function uuids(array $uuids): self { $this->uuids = $uuids; @@ -74,10 +74,9 @@ public function uuids($uuids) * @param PNChannelMember[] $members * @return $this */ - public function members(array $members) + public function members(array $members): self { $this->members = $members; - return $this; } @@ -103,21 +102,21 @@ public function include(PNMemberIncludes $includes): self * @deprecated Use include() method * @return $this */ - public function includeFields($include) + public function includeFields(array $include): self { $this->include = $include; - return $this; } /** * @throws PubNubValidationException + * @return void */ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->channel)) { + if (empty($this->channel)) { throw new PubNubValidationException("channel missing"); } @@ -132,7 +131,7 @@ protected function validateParams() /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { @@ -206,10 +205,8 @@ protected function customParams() array_push($includes, 'uuid'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } diff --git a/src/PubNub/Endpoints/Objects/Member/SetMembers.php b/src/PubNub/Endpoints/Objects/Member/SetMembers.php index 9dbcf8e7..cbf43bd7 100644 --- a/src/PubNub/Endpoints/Objects/Member/SetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/SetMembers.php @@ -22,16 +22,16 @@ class SetMembers extends ObjectsCollectionEndpoint protected string $endpointName = "SetMembers"; /** @var string */ - protected $channel; + protected ?string $channel; /** @var array */ - protected $uuids; + protected array $uuids; /** @var array */ - protected $custom; + protected array $custom; /** @var array */ - protected $include = []; + protected array $include = []; /** @var PNMemberIncludes */ protected PNMemberIncludes $includes; @@ -53,10 +53,9 @@ public function __construct(PubNub $pubnubInstance) * @param string $ch * @return $this */ - public function channel($ch) + public function channel(string $ch): self { $this->channel = $ch; - return $this; } @@ -66,10 +65,9 @@ public function channel($ch) * * @return $this */ - public function uuids($uuids) + public function uuids(array $uuids): self { $this->uuids = $uuids; - return $this; } @@ -89,10 +87,9 @@ public function members(array $members) * @deprecated Use members() method * @return $this */ - public function custom($custom) + public function custom(mixed $custom): self { $this->custom = $custom; - return $this; } @@ -118,21 +115,21 @@ public function include(PNMemberIncludes $includes): self * @deprecated Use include() method * @return $this */ - public function includeFields($include) + public function includeFields(array $include): self { $this->include = $include; - return $this; } /** * @throws PubNubValidationException + * @return void */ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->channel)) { + if (empty($this->channel)) { throw new PubNubValidationException("channel missing"); } @@ -147,7 +144,7 @@ protected function validateParams() /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { @@ -227,10 +224,8 @@ protected function customParams() array_push($includes, 'uuid'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } } diff --git a/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php b/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php index f7effd30..5d62f66d 100644 --- a/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php @@ -20,11 +20,12 @@ class GetMemberships extends ObjectsCollectionEndpoint protected string $endpointName = "GetMemberships"; /** @var string */ - protected $userId; + protected ?string $userId; /** @var array */ - protected $include = []; + protected array $include = []; + /** @var PNMembershipIncludes */ protected ?PNMembershipIncludes $includes; /** @@ -41,22 +42,33 @@ public function __construct(PubNub $pubnubInstance) * @param string $uuid * @return $this */ - public function uuid($uuid) + public function uuid(string $uuid): self { $this->userId = $uuid; return $this; } /** - * @param string $uuid + * @param string $userId * @return $this */ - public function userId($uuid) + public function userId(string $userId): self { - $this->userId = $uuid; + $this->userId = $userId; return $this; } + /** + * Defines a list of fields to be included in response. It takes an instance of PNMemberIncludes. + * + * Example: + * + * $includes = (new PNMembershipIncludes())->custom()->status()->totalCount()->type()-user(); + * $pnGetMembers->include($includes); + * + * @param PNMembershipIncludes $includes + * @return $this + */ public function include(PNMembershipIncludes $includes): self { $this->includes = $includes; @@ -67,7 +79,7 @@ public function include(PNMembershipIncludes $includes): self * @param array $include * @return $this */ - public function includeFields($include) + public function includeFields(array $include): self { $this->include = $include; @@ -81,14 +93,14 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->userId)) { + if (empty($this->userId)) { throw new PubNubValidationException("uuid missing"); } } /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { @@ -145,10 +157,8 @@ protected function customParams() array_push($includes, 'channel'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } diff --git a/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php b/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php index 74651121..a0fb093a 100644 --- a/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php @@ -6,7 +6,6 @@ use PubNub\Enums\PNHttpMethod; use PubNub\Enums\PNOperationType; use PubNub\Exceptions\PubNubValidationException; -use PubNub\Exceptions\PubNubBuildRequestException; use PubNub\Models\Consumer\Objects\Membership\PNMembershipIncludes; use PubNub\Models\Consumer\Objects\Membership\PNChannelMembership; use PubNub\Models\Consumer\Objects\Membership\PNMembershipsResult; @@ -23,19 +22,19 @@ class ManageMemberships extends ObjectsCollectionEndpoint protected string $endpointName = "ManageMemberships"; /** @var string */ - protected $userId; + protected ?string $userId; /** @var string[] */ - protected $setChannels; + protected array $setChannels; /** @var string[] */ - protected $removeChannels; + protected array $removeChannels; /** @var string[] */ - protected $custom; + protected array $custom; /** @var string[] */ - protected $include = []; + protected array $include = []; /** @var PNMembershipIncludes */ protected ?PNMembershipIncludes $includes; @@ -60,7 +59,7 @@ public function __construct(PubNub $pubnubInstance) * @param string $uuid * @return $this */ - public function uuid($uuid): self + public function uuid(string $uuid): self { $this->userId = $uuid; return $this; @@ -70,7 +69,7 @@ public function uuid($uuid): self * @param string $userId * @return $this */ - public function userId($userId): self + public function userId(string $userId): self { $this->userId = $userId; return $this; @@ -82,7 +81,7 @@ public function userId($userId): self * * @return $this */ - public function setChannels($channels): self + public function setChannels(mixed $channels): self { $this->setChannels = $channels; return $this; @@ -94,7 +93,7 @@ public function setChannels($channels): self * * @return $this */ - public function removeChannels($channels): self + public function removeChannels(mixed $channels): self { $this->removeChannels = $channels; return $this; @@ -126,7 +125,7 @@ public function removeMemberships(array $memberships): self * * @return $this */ - public function custom($custom): self + public function custom(mixed $custom): self { $this->custom = $custom; return $this; @@ -138,7 +137,7 @@ public function custom($custom): self * * @return $this */ - public function includeFields($include): self + public function includeFields(array $include): self { $this->include = $include; return $this; @@ -169,7 +168,7 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->userId)) { + if (empty($this->userId)) { throw new PubNubValidationException("uuid missing"); } @@ -187,12 +186,11 @@ protected function validateParams() /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { $set = []; - $remove = []; if (!empty($this->setMemberships)) { foreach ($this->setMemberships as $memberhip) { array_push($set, $memberhip->toArray()); @@ -270,10 +268,8 @@ protected function customParams() array_push($includes, 'channel'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } diff --git a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php index bcd19d45..785ae748 100644 --- a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php @@ -22,13 +22,13 @@ class RemoveMemberships extends ObjectsCollectionEndpoint protected string $endpointName = "RemoveMemberships"; /** @var string */ - protected $userId; + protected ?string $userId; /** @var array */ - protected $channels; + protected array $channels; /** @var array */ - protected $include = []; + protected array $include = []; /** @var PNMembershipIncludes */ protected ?PNMembershipIncludes $includes; @@ -73,7 +73,7 @@ public function userId(string $userId): self * @deprecated Use memberships() method * @return $this */ - public function channels($channels): self + public function channels(array $channels): self { $this->channels = $channels; @@ -95,7 +95,7 @@ public function memberships(array $memberships): self * @deprecated Use includes() method * @return $this */ - public function includeFields($include): self + public function includeFields(array $include): self { $this->include = $include; @@ -126,7 +126,7 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->userId)) { + if (empty($this->userId)) { throw new PubNubValidationException("uuid missing"); } @@ -141,7 +141,7 @@ protected function validateParams() /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { @@ -217,10 +217,8 @@ protected function customParams() array_push($includes, 'channel'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } diff --git a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php index 08c7f1a2..e358632b 100644 --- a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php @@ -22,16 +22,16 @@ class SetMemberships extends ObjectsCollectionEndpoint protected string $endpointName = "SetMemberships"; /** @var string */ - protected $userId; + protected ?string $userId; /** @var array */ - protected $channels; + protected array $channels; /** @var array */ - protected $custom; + protected array $custom; /** @var array */ - protected $include = []; + protected array $include = []; /** @var PNMembershipIncludes */ protected ?PNMembershipIncludes $includes; @@ -53,7 +53,7 @@ public function __construct(PubNub $pubnubInstance) * @param string $uuid * @return $this */ - public function uuid($uuid): self + public function uuid(string $uuid): self { $this->userId = $uuid; return $this; @@ -63,7 +63,7 @@ public function uuid($uuid): self * @param string $userId * @return $this */ - public function userId($userId): self + public function userId(string $userId): self { $this->userId = $userId; return $this; @@ -75,7 +75,7 @@ public function userId($userId): self * * @return $this */ - public function channels($channels): self + public function channels(array $channels): self { $this->channels = $channels; return $this; @@ -97,7 +97,7 @@ public function memberships(array $memberships): self * * @return $this */ - public function custom($custom): self + public function custom(array $custom): self { $this->custom = $custom; return $this; @@ -109,7 +109,7 @@ public function custom($custom): self * * @return $this */ - public function includeFields($include): self + public function includeFields(array $include): self { $this->include = $include; return $this; @@ -139,7 +139,7 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->userId)) { + if (empty($this->userId)) { throw new PubNubValidationException("uuid missing"); } @@ -154,7 +154,7 @@ protected function validateParams() /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { @@ -233,10 +233,8 @@ protected function customParams() array_push($includes, 'channel'); } - $includesString = implode(",", $includes); - - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); } } diff --git a/src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php b/src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php index 0e6d2ff5..f37556b1 100644 --- a/src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php +++ b/src/PubNub/Models/Consumer/Objects/Member/PNChannelMember.php @@ -16,7 +16,7 @@ class PNChannelMember * @param ?string $status * @return void */ - public function __construct($userId, $custom = null, $type = null, $status = null) + public function __construct(string $userId, mixed $custom = null, ?string $type = null, ?string $status = null) { $this->userId = $userId; $this->custom = $custom; diff --git a/src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php b/src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php index 68221015..fc238253 100644 --- a/src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php +++ b/src/PubNub/Models/Consumer/Objects/Membership/PNChannelMembership.php @@ -17,7 +17,7 @@ class PNChannelMembership * @param ?string $status * @return void */ - public function __construct($channel, $custom = null, $type = null, $status = null) + public function __construct(string $channel, mixed $custom = null, ?string $type = null, ?string $status = null) { $this->channel = $channel; $this->custom = $custom; @@ -25,7 +25,7 @@ public function __construct($channel, $custom = null, $type = null, $status = nu $this->status = $status; } - public function setchannel(string $channel): self + public function setChannel(string $channel): self { $this->channel = $channel; return $this; @@ -62,12 +62,12 @@ public function getCustom(): mixed return $this->custom; } - public function getType(): string + public function getType(): ?string { return $this->type; } - public function getStatus(): string + public function getStatus(): ?string { return $this->status; } @@ -75,7 +75,7 @@ public function getStatus(): string /** * @return string[] */ - public function toArray() + public function toArray(): array { $result = [ 'channel' => [ From 20ec4e17e67edcfd89fea45782b4b763a63e952b Mon Sep 17 00:00:00 2001 From: PubNub Release Bot <120067856+pubnub-release-bot@users.noreply.github.com> Date: Wed, 5 Feb 2025 12:02:20 +0000 Subject: [PATCH 9/9] PubNub SDK 7.3.0 release. --- .pubnub.yml | 11 ++++++++--- CHANGELOG.md | 6 ++++++ README.md | 2 +- composer.json | 2 +- src/PubNub/PubNub.php | 2 +- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.pubnub.yml b/.pubnub.yml index 9b907d17..ad00128d 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,8 +1,13 @@ name: php -version: 7.2.1 +version: 7.3.0 schema: 1 scm: github.com/pubnub/php changelog: + - date: 2025-02-05 + version: 7.3.0 + changes: + - type: feature + text: "Extended functionality of Channel Members and User Membership. Now it's possible to use fine-grade includes and set member/membership status and type." - date: 2025-02-03 version: 7.2.1 changes: @@ -442,8 +447,8 @@ sdks: - x86-64 - distribution-type: library distribution-repository: GitHub release - package-name: php-7.2.1.zip - location: https://github.com/pubnub/php/releases/tag/7.2.1 + package-name: php-7.3.0.zip + location: https://github.com/pubnub/php/releases/tag/7.3.0 requires: - name: rmccue/requests min-version: 1.0.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index f58f9351..469cad3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 7.3.0 +February 05 2025 + +#### Added +- Extended functionality of Channel Members and User Membership. Now it's possible to use fine-grade includes and set member/membership status and type. + ## 7.2.1 February 03 2025 diff --git a/README.md b/README.md index ab451b66..284a471c 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your { "require": { - "pubnub/pubnub": "7.2.1" + "pubnub/pubnub": "7.3.0" } } ``` diff --git a/composer.json b/composer.json index cc23f7a2..d7903910 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "keywords": ["api", "real-time", "realtime", "real time", "ajax", "push"], "homepage": "http://www.pubnub.com/", "license": "proprietary", - "version": "7.2.1", + "version": "7.3.0", "authors": [ { "name": "PubNub", diff --git a/src/PubNub/PubNub.php b/src/PubNub/PubNub.php index 4783c934..93ce6e07 100644 --- a/src/PubNub/PubNub.php +++ b/src/PubNub/PubNub.php @@ -62,7 +62,7 @@ class PubNub implements LoggerAwareInterface { - protected const SDK_VERSION = "7.2.1"; + protected const SDK_VERSION = "7.3.0"; protected const SDK_NAME = "PubNub-PHP"; public static $MAX_SEQUENCE = 65535;