summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHannes Kindströmmer <hannes@kindstrommer.se>2017-03-07 15:20:54 +0100
committerHannes Kindströmmer <hannes@kindstrommer.se>2017-03-07 15:20:54 +0100
commit33a5b9bbdfb92a6ecb0f6fbdef8bfbba6c87fb4f (patch)
tree27937515998818570dc06384c0427525b6c9a970 /src
parentd303ce63873c82dc960b91460b87e7875f46ab2c (diff)
downloadip1-php-sdk-33a5b9bbdfb92a6ecb0f6fbdef8bfbba6c87fb4f.zip
ip1-php-sdk-33a5b9bbdfb92a6ecb0f6fbdef8bfbba6c87fb4f.tar.gz
ip1-php-sdk-33a5b9bbdfb92a6ecb0f6fbdef8bfbba6c87fb4f.tar.bz2
Add Group and Membership
Diffstat (limited to 'src')
-rw-r--r--src/Recipient/Group.php89
-rw-r--r--src/Recipient/Membership.php60
-rw-r--r--src/Recipient/MembershipRelation.php27
-rw-r--r--src/Recipient/ProcessedGroup.php82
-rw-r--r--src/Recipient/RecipientFactory.php15
5 files changed, 273 insertions, 0 deletions
diff --git a/src/Recipient/Group.php b/src/Recipient/Group.php
new file mode 100644
index 0000000..447aa57
--- /dev/null
+++ b/src/Recipient/Group.php
@@ -0,0 +1,89 @@
+<?php
+/**
+* Contains the Group class
+* PHP version 7.1.1
+* @author Hannes Kindströmmer <hannes@kindstrommer.se>
+* @copyright 2017 IP1 SMS
+*/
+use IP1\RESTClient\Core\UpdatableComponent;
+
+namespace IP1\RESTClient\Recipient;
+
+class Group implements \JsonSerializable
+{
+ /**
+ * Name of Group
+ * @var string $name
+ */
+ protected $name;
+ /**
+ * Contact group color, hexadecimal
+ * @example #5E5E5E
+ * @var string $color
+ */
+ protected $color;
+ /**
+ * An array of Membership
+ * @var array Membership
+ */
+ protected $memberships = [];
+
+ /**
+ * Group constructor
+ * @param string $name Name of Group.
+ * @param string $color Hexadecimal color code.
+ */
+ public function __construct(string $name, string $color)
+ {
+ $this->setName($name);
+ $this->setColor($color);
+ }
+ /**
+ * Returns the name of the group.
+ * @return string Name of Group.
+ */
+ public function getName(): string
+ {
+ return $this->name;
+ }
+ /**
+ * Returns the groups color in hexadecimal
+ * @return string hexadecimal color
+ */
+ public function getColor(): string
+ {
+ return $this->color;
+ }
+ /**
+ * Sets the Group's name to the given string
+ * @param string $name
+ */
+ public function setName(string $name):void
+ {
+ if (empty($name)) {
+ //TODO: Throw EmptyStringException
+ }
+ $this->name = $name;
+ }
+ /**
+ * Sets the groups color.
+ * @param $color A hexadecimal color code.
+ */
+ public function setColor(string $color): void
+ {
+ if (!preg_match("/^#([A-Fa-f0-9]{6})$/", $color)) {
+ //TODO Throw non hexadecimal color exception
+ throw new Exception($color. " is not a valid hexadecimal color");
+ }
+ $this->color = $color;
+ }
+ /** {@inheritDoc} */
+ public function jsonSerialize()
+ {
+ $returnArray = [
+ 'Name' => $this->name,
+ 'Color' => $this->Color,
+ ];
+ return $returnArray;
+ }
+}
diff --git a/src/Recipient/Membership.php b/src/Recipient/Membership.php
new file mode 100644
index 0000000..7eb9d6f
--- /dev/null
+++ b/src/Recipient/Membership.php
@@ -0,0 +1,60 @@
+<?php
+/**
+* Contains the Membership class
+* PHP version 7.1.1
+* @author Hannes Kindströmmer <hannes@kindstrommer.se>
+* @copyright 2017 IP1 SMS
+*/
+namespace IP1\RESTClient\Recipient;
+
+/**
+*
+*/
+class Membership implements \JsonSerializable
+{
+
+ /**
+ * A Group ID
+ * @var int $groupID
+ */
+ protected $groupID;
+/**
+ * A Contact ID
+ * @var int $contactID
+ */
+ protected $contactID;
+/**
+ * Membership Constructor
+ * @param int $groupID A Group ID
+ * @param int $contactID A Contact ID
+ */
+ public function __construct(int $groupID, int $contactID)
+ {
+ $this->groupID = $groupID;
+ $this->contactID = $contactID;
+ }
+ /**
+ * Returns Group ID
+ * @return int
+ */
+ public function getGroupID()
+ {
+ return $this->groupID;
+ }
+ /**
+ * Returns Contact ID
+ * @return int
+ */
+ public function getContactID()
+ {
+ return $this->contactID;
+ }
+ public function jsonSerialize()
+ {
+ $returnArray = [
+ 'Group' => $this->groupID,
+ 'Contact' => $this->ContactID,
+ ];
+ return $returnArray;
+ }
+}
diff --git a/src/Recipient/MembershipRelation.php b/src/Recipient/MembershipRelation.php
new file mode 100644
index 0000000..02d0f35
--- /dev/null
+++ b/src/Recipient/MembershipRelation.php
@@ -0,0 +1,27 @@
+<?php
+/**
+* Contains the MembershipRelation interface
+* PHP version 7.1.1
+* @author Hannes Kindströmmer <hannes@kindstrommer.se>
+* @copyright 2017 IP1 SMS
+*/
+namespace IP1\RESTClient\Recipient;
+
+/**
+* Classes that has deals with membership should implement this interface
+* @link http://api.ip1sms.com/Help/Api/GET-api-memberships
+* @package \IP1\RESTClient\Recipient
+*/
+interface MembershipRelation
+{
+ /**
+ * Returns an array of all the memberships
+ * @return array An array of Group objects
+ */
+ public function getMemberShips(): array;
+ /**
+ * Tells whether memberships has been fetched from the API or not.
+ * @return bool Whether the memberships has been fetched from the API or not
+ */
+ public function memberShipsFetched(): bool;
+}
diff --git a/src/Recipient/ProcessedGroup.php b/src/Recipient/ProcessedGroup.php
new file mode 100644
index 0000000..993600a
--- /dev/null
+++ b/src/Recipient/ProcessedGroup.php
@@ -0,0 +1,82 @@
+<?php
+/**
+* Contains the ProcessedGroup class
+* PHP version 7.1.1
+* @author Hannes Kindströmmer <hannes@kindstrommer.se>
+* @copyright 2017 IP1 SMS
+*/
+namespace IP1\RESTClient\Recipient;
+
+use IP1\RESTClient\Core\Communicator;
+use IP1\RESTClient\Core\UpdatableComponent;
+use IP1\RESTClient\Recipient\RecipientFactory;
+
+class ProcessedGroup extends Group implements UpdatableComponent, MembershipRelation
+{
+
+ private $groupID;
+ private $created;
+ private $updated;
+ protected $memberships = [];
+ protected $fechedMemberships = false;
+ const IS_READ_ONLY = false;
+
+ public function __construct(string $name, string $color, int $groupID, \DateTime $created, \DateTime $updated)
+ {
+ parent::__construct($name, $color);
+ $this->groupID = $groupID;
+ $this->created = $created;
+ $this->updated = $updated;
+ }
+ /** {@inheritDoc} */
+ public function getID(): int
+ {
+ return $this->groupID;
+ }
+ /** {@inheritDoc} */
+ public function isReadOnly(): bool
+ {
+ return self::IS_READ_ONLY;
+ }
+ public function getMemberships(Communicator $communicator = null): array
+ {
+ if ($communicator != null) {
+ $membershipJSON = $communicator->get("api/groups/".$this->groupID."/memberships");
+ $membershipStd = json_decode($membershipJSON);
+ $memberships = [];
+ foreach ($membershipStd as $value) {
+ $memberships[] = RecipientFactory::createProcessedMembershipFromStdClass($value);
+ }
+ $this->memberships = $memberships;
+ $this->fetchedMemberships = true;
+ }
+ //TODO: Add functionality for fetching from the API
+ return $this->memberships;
+ }
+ public function memberShipsFetched(): bool
+ {
+ return $this->fetchedMemberships;
+ }
+
+
+ /** {@inheritDoc} */
+ public function getCreated(\DateTimeZone $timezone = null): ?\DateTime
+ {
+ if (!is_null($timezone)) {
+ $returnDate = clone $this->created;
+ $returnDate->setTimeZone($timezone);
+ return $returnDate;
+ }
+ return $this->created ?? null;
+ }
+ /** {@inheritDoc} */
+ public function getUpdated(\DateTimeZone $timezone = null) : ?\DateTime
+ {
+ if (!is_null($timezone)) {
+ $returnDate = clone $this->updated;
+ $returnDate->setTimeZone($timezone);
+ return $returnDate;
+ }
+ return $this->updated ?? null;
+ }
+}
diff --git a/src/Recipient/RecipientFactory.php b/src/Recipient/RecipientFactory.php
index ce987d6..61e145b 100644
--- a/src/Recipient/RecipientFactory.php
+++ b/src/Recipient/RecipientFactory.php
@@ -7,6 +7,7 @@
*/
namespace IP1\RESTClient\Recipient;
+use IP1\RESTClient\Recipient\ProcessedGroup;
use IP1\RESTClient\Recipient\ProcessedContact;
/**
@@ -124,4 +125,18 @@ class RecipientFactory
);
return $contact;
}
+ public static function createProcessedGroupFromJSON(string $jsonContact): ProcessedGroup
+ {
+ return self::createProcessedGroupFromStdClass(json_decode($jsonGroup));
+ }
+ public static function createProcessedGroupFromStdClass(\stdClass $stdContact): ProcessedGroup
+ {
+ return new ProcessedGroup(
+ $stdContact->Name,
+ $stdContact->Color,
+ $stdContact->ID,
+ new \DateTime($stdContact->Created),
+ new \DateTime($stdContact->Modified)
+ );
+ }
}