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/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/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/GetMembers.php b/src/PubNub/Endpoints/Objects/Member/GetMembers.php index 923ef1f9..9bc498c2 100644 --- a/src/PubNub/Endpoints/Objects/Member/GetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/GetMembers.php @@ -6,34 +6,60 @@ 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; + protected ?string $channel; /** @var array */ - protected $include = []; + protected array $include = []; + + /** @var PNMemberIncludes */ + protected ?PNMemberIncludes $includes; /** - * @param string $ch + * @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 $channel * @return $this */ - public function channel($ch) + public function channel(string $channel): self { - $this->channel = $ch; + $this->channel = $channel; return $this; } + public function include(PNMemberIncludes $includes): self + { + $this->includes = $includes; + return $this; + } + /** * @param array $include * @return $this */ - public function includeFields($include) + public function includeFields(array $include): self { $this->include = $include; @@ -42,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() { @@ -94,8 +121,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'); @@ -109,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); } } @@ -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..cd1db2bb --- /dev/null +++ b/src/PubNub/Endpoints/Objects/Member/ManageMembers.php @@ -0,0 +1,297 @@ +endpointConnectTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); + $this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getConnectTimeout(); + } + + /** + * @param string $ch + * @return $this + */ + public function channel(string $ch): self + { + $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): self + { + $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 + * @return void + */ + protected function validateParams() + { + $this->validateSubscribeKey(); + + if (empty($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 + * @throws \PubNub\Exceptions\PubNubBuildRequestException + */ + protected function buildData() + { + $set = []; + 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); + } + } + $remove = []; + 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 mixed $result Decoded json + * @return PNMembersResult + */ + protected function createResponse($result): PNMembersResult + { + return PNMembersResult::fromPayload($result); + } + + public function sync(): PNMembersResult + { + return parent::sync(); + } + + /** + * @return string[] + */ + 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'); + } + + if (!empty($includes)) { + $params['include'] = implode(",", $includes); + } + } + } + + 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..2e63ee40 100644 --- a/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/RemoveMembers.php @@ -6,27 +6,51 @@ 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; + protected ?string $channel; /** @var array */ - protected $uuids; + protected array $uuids; /** @var array */ - protected $include = []; + protected array $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 */ - public function channel($ch) + public function channel(string $ch): self { $this->channel = $ch; @@ -35,60 +59,97 @@ public function channel($ch) /** * @param array $uuids + * @deprecated Use members() method + * * @return $this */ - public function uuids($uuids) + public function uuids(array $uuids): self { $this->uuids = $uuids; return $this; } + /** + * @param PNChannelMember[] $members + * @return $this + */ + public function members(array $members): self + { + $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) + 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"); } - 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"); } } /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ 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 +188,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)) { @@ -142,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); } } @@ -185,52 +246,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..cbf43bd7 100644 --- a/src/PubNub/Endpoints/Objects/Member/SetMembers.php +++ b/src/PubNub/Endpoints/Objects/Member/SetMembers.php @@ -6,107 +6,168 @@ 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; + 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; + + /** @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 */ - public function channel($ch) + public function channel(string $ch): self { $this->channel = $ch; - return $this; } /** * @param array $uuids + * @deprecated Use members() method + * * @return $this */ - public function uuids($uuids) + public function uuids(array $uuids): self { $this->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) + public function custom(mixed $custom): self { $this->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) + 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"); } - 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"); } } /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ 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 +206,27 @@ protected function customParams() { $params = $this->defaultParams(); - if (count($this->include) > 0) { - $includes = []; - - if (array_key_exists("customFields", $this->include)) { - array_push($includes, 'custom'); - } + if (!empty($this->includes)) { + $params['include'] = (string)$this->includes; + } else { + if (count($this->include) > 0) { + $includes = []; - if (array_key_exists("customUUIDFields", $this->include)) { - array_push($includes, 'uuid.custom'); - } + if (array_key_exists("customFields", $this->include)) { + array_push($includes, 'custom'); + } - if (array_key_exists("UUIDFields", $this->include)) { - array_push($includes, 'uuid'); - } + if (array_key_exists("customUUIDFields", $this->include)) { + array_push($includes, 'uuid.custom'); + } - $includesString = implode(",", $includes); + if (array_key_exists("UUIDFields", $this->include)) { + array_push($includes, 'uuid'); + } - if (strlen($includesString) > 0) { - $params['include'] = $includesString; + if (!empty($includes)) { + $params['include'] = implode(",", $includes); + } } } @@ -202,52 +265,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..5d62f66d 100644 --- a/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/GetMemberships.php @@ -6,26 +6,72 @@ 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 ?string $userId; /** @var array */ - protected $include = []; + protected array $include = []; + + /** @var PNMembershipIncludes */ + 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) + public function uuid(string $uuid): self + { + $this->userId = $uuid; + return $this; + } + + /** + * @param string $userId + * @return $this + */ + public function userId(string $userId): self { - $this->uuid = $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; return $this; } @@ -33,7 +79,7 @@ public function uuid($uuid) * @param array $include * @return $this */ - public function includeFields($include) + public function includeFields(array $include): self { $this->include = $include; @@ -47,14 +93,14 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->uuid)) { + if (empty($this->userId)) { throw new PubNubValidationException("uuid missing"); } } /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ protected function buildData() { @@ -69,7 +115,7 @@ protected function buildPath() return sprintf( static::PATH, $this->pubnub->getConfiguration()->getSubscribeKey(), - $this->uuid + $this->userId ); } @@ -94,7 +140,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)) { @@ -109,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); } } @@ -152,52 +198,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..a0fb093a --- /dev/null +++ b/src/PubNub/Endpoints/Objects/Membership/ManageMemberships.php @@ -0,0 +1,312 @@ +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 $userId + * @return $this + */ + public function userId(string $userId): self + { + $this->userId = $userId; + return $this; + } + + /** + * @param mixed $channels + * @deprecated Use memberships() method + * + * @return $this + */ + public function setChannels(mixed $channels): self + { + $this->setChannels = $channels; + return $this; + } + + /** + * @param mixed $channels + * @deprecated Use memberships() method + * + * @return $this + */ + public function removeChannels(mixed $channels): self + { + $this->removeChannels = $channels; + return $this; + } + + /** + * @param PNChannelMembership[] $memberships + * @return $this + */ + public function setMemberships(array $memberships): self + { + $this->setMemberships = $memberships; + return $this; + } + + /** + * @param PNChannelMembership[] $memberships + * @return $this + */ + public function removeMemberships(array $memberships): self + { + $this->removeMemberships = $memberships; + return $this; + } + + /** + * @param mixed $custom + * @deprecated Use members() method + * + * @return $this + */ + public function custom(mixed $custom): self + { + $this->custom = $custom; + return $this; + } + + /** + * @param string[] $include + * @deprecated Use includes() method + * + * @return $this + */ + public function includeFields(array $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 + * @return void + */ + protected function validateParams() + { + $this->validateSubscribeKey(); + + if (empty($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 \PubNub\Exceptions\PubNubBuildRequestException + */ + protected function buildData() + { + $set = []; + 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 mixed $result Decoded json + * @return PNMembershipsResult + */ + protected function createResponse($result): PNMembershipsResult + { + return PNMembershipsResult::fromPayload($result); + } + + /** + * @return string[] + */ + 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'); + } + + if (!empty($includes)) { + $params['include'] = implode(",", $includes); + } + } + + 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..785ae748 100644 --- a/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/RemoveMemberships.php @@ -6,55 +6,119 @@ 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 ?string $userId; /** @var array */ - protected $channels; + protected array $channels; /** @var array */ - protected $include = []; + protected array $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(string $uuid): self { - $this->uuid = $uuid; + $this->userId = $uuid; + + return $this; + } + + /** + * @param string $userId + * @return $this + */ + public function userId(string $userId): self + { + $this->userId = $userId; return $this; } /** * @param array $channels + * @deprecated Use memberships() method * @return $this */ - public function channels($channels) + public function channels(array $channels): self { $this->channels = $channels; return $this; } + /** + * @param PNChannelMembership[] $memberships + * @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(array $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,31 +126,40 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->uuid)) { + if (empty($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"); } } /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ 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)) { @@ -142,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); } } @@ -185,52 +258,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..e358632b 100644 --- a/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php +++ b/src/PubNub/Endpoints/Objects/Membership/SetMemberships.php @@ -6,66 +6,129 @@ 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 ?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; + + /** @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(string $uuid): self { - $this->uuid = $uuid; + $this->userId = $uuid; + return $this; + } + /** + * @param string $userId + * @return $this + */ + public function userId(string $userId): self + { + $this->userId = $userId; return $this; } /** * @param array $channels + * @deprecated Use memberships() method + * * @return $this */ - public function channels($channels) + public function channels(array $channels): self { $this->channels = $channels; + return $this; + } + /** + * @param PNChannelMembership[] $memberships + * @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(array $custom): self { $this->custom = $custom; - return $this; } /** * @param array $include + * @deprecated Use includes() method + * * @return $this */ - public function includeFields($include) + public function includeFields(array $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,35 +139,43 @@ protected function validateParams() { $this->validateSubscribeKey(); - if (!is_string($this->uuid)) { + if (empty($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"); } } /** * @return string - * @throws PubNubBuildRequestException + * @throws \PubNub\Exceptions\PubNubBuildRequestException */ 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)) { @@ -160,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); } } @@ -203,52 +274,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..f37556b1 --- /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..fc238253 --- /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(): array + { + $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..17a6b384 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; @@ -56,7 +56,7 @@ public function getNext() } /** - * @return array + * @return array | \StdClass */ public function getData() { @@ -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..5b69f424 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; } /** @@ -40,13 +51,29 @@ public function getChannel() } /** - * @return array + * @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 +92,19 @@ public function getETag() public function __toString() { - if (!empty($data)) - { - $data_string = json_encode($data); + $data_string = ''; + 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 +117,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..93ce6e07 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; @@ -60,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; @@ -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..e042f5bd 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() @@ -20,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 new file mode 100644 index 00000000..1faf3e5b --- /dev/null +++ b/tests/integrational/objects/member/MembersHappyPathTest.php @@ -0,0 +1,143 @@ +pubnub->getMembers()->channel($this->channel)->sync(); + foreach ($getStaleMembers->getData() as $member) { + array_push($staleMembers, new PNChannelMember($member->getUser()->getId())); + } + + if (!empty($staleMembers)) { + $cleanup = $this->pubnub->removeMembers()->channel($this->channel)->members($staleMembers)->sync(); + $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([ + '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()); + $this->cleanup(); + sleep(1); + } + + 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..fac2c8d6 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() @@ -16,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(); @@ -24,21 +28,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..c07a7660 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() @@ -20,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 new file mode 100644 index 00000000..65fdff3a --- /dev/null +++ b/tests/integrational/objects/membership/MembershipsHappyPathTest.php @@ -0,0 +1,105 @@ +pubnub->getMemberships()->userId($this->user)->sync(); + foreach ($getStaleMemberships->getData() as $membership) { + array_push($staleMemberships, new PNChannelMembership($membership->getChannel()->getId())); + } + if (!empty($staleMemberships)) { + $cleanup = $this->pubnub->removeMemberships()->userId($this->user)->memberships($staleMemberships)->sync(); + $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([ + new PNChannelMembership($this->channel1, ['BestDish' => 'Pizza'], 'Admin', 'Active'), + new PNChannelMembership($this->channel2, ['BestDish' => 'Lasagna'], 'Guest', 'Away'), + ]) + ->include($includes) + ->sync(); + + $this->checkResponse($addMembership); + sleep(1); + $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..8d416838 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() @@ -16,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(); @@ -24,21 +28,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());