summaryrefslogtreecommitdiffstats
path: root/src/Recipient/ProcessedGroup.php
blob: 9efced75258d45b84353ce904316562ae371b9d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* PHP version 7.1.1
* @author Hannes Kindströmmer <hannes@kindstrommer.se>
* @copyright 2017 iP.1 Networks AB
* @license https://www.gnu.org/licenses/lgpl-3.0.txt LGPL-3.0
* @version 0.3.0-beta
* @since File available since Release 0.1.0-beta
* @link http://api.ip1sms.com/Help
* @link https://github.com/iP1SMS/ip1-php-sdk
*/
namespace IP1\RESTClient\Recipient;

use IP1\RESTClient\Core\Communicator;
use IP1\RESTClient\Core\UpdatableComponentInterface;
use IP1\RESTClient\Core\ClassValidationArray;
use IP1\RESTClient\Recipient\ProcessedMembership;
use IP1\RESTClient\Core\OwnableInterface;

/**
*
*/
class ProcessedGroup extends Group implements UpdatableComponentInterface, MembershipRelationInterface, OwnableInterface
{
    /**
    * The ID of the Group given by the API.
    * @var integer $groupID
    */
    private $groupID;
    /**
    * When the Group was created.
    * @var \DateTime $created
    */
    private $created;
    /**
    * When the Group was last updated.
    * @var \DateTime $updated
    */
    private $updated;
    /**
    * ID of account owning the Group
    * @var string $ownerID
    */
    private $ownerID;
    /**
    * An array of memberships that the Group has.
    *
    * It is empty by default but is filled when the function getMemberships() is called if a Communicator is given
    *   as an argument.
    *
    * @var ClassValidationArray $membership
    */
    protected $memberships = [];
    /**
    * Tells whether memberships has been fetched from the API.
    * @var boolean $fetchedMemberships Defaults to false.
    */
    protected $fetchedMemberships = false;
    /**
    * An array of Contact that the Group has.
    *
    * It is empty by default but is filled when the function getContacts() is called if a Communicator is given
    *   as an argument.
    *
    * @var ClassValidationArray $contacts Default empty array.
    */
    protected $contacts = [];
    /**
    * Tells wheter contacts has been fetched from the API.
    * @var boolean $contactsFetched Defaults to false.
    */
    protected $contactsFetched = false;
    const IS_READ_ONLY = false;
    /**
    * ProcessedGroup Contstructor.
    * @param string    $name    Name of the Group.
    * @param string    $color   A hexadecimal color.
    * @param integer   $groupID An ID generated by the API.
    * @param string    $ownerID ID of account owning the Group.
    * @param \DateTime $created When the Group was initially created.
    * @param \DateTime $updated When the Group was last updated.
    */
    public function __construct(
        string $name,
        string $color,
        int $groupID,
        string $ownerID,
        \DateTime $created,
        \DateTime $updated
    ) {

        parent::__construct($name, $color);
        $this->groupID = $groupID;
        $this->ownerID = $ownerID;
        $this->created = $created;
        $this->updated = $updated;
        $this->memberships = new ClassValidationArray();
        $this->contacts = new ClassValidationArray();
    }
    /**
    * @return integer Group ID.
    */
    public function getID(): int
    {
        return $this->groupID;
    }
    /**
    * Returns ID of account owning the Group.
    * @return string
    */
    public function getOwnerID(): string
    {
        return $this->ownerID;
    }
    /**
    * Returns whether the object is read only or not.
    * @return boolean Whether the object is read only or not.
    */
    public function isReadOnly(): bool
    {
        return self::IS_READ_ONLY;
    }
    /**
    * Returns an array of all the memberships the group is referenced in.
    * If a communicator is not provided it will not fetch memberships from the API
    *               but return those that has been fetched, if any.
    * @param Communicator $communicator Used to fetch memberships from the API.
    * @return array An array of Membership objects
    */
    public function getMemberships(Communicator $communicator = null): ClassValidationArray
    {
        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;
        }
        return $this->memberships;
    }
    /**
    * Tells whether memberships has been fetched from the API or not.
    * @return boolean Whether the memberships has been fetched from the API or not.
    */
    public function memberShipsFetched(): bool
    {
        return $this->fetchedMemberships;
    }
    /**
    * Returns an array of all the Contacts that belong to the Group.
    * If a communicator is not provided it will not fetch Contacts from the API
    *               but return those that has been fetched, if any.
    * @param Communicator $communicator Used to fetch Contacts from the API.
    * @return ClassValidationArray An array of ProcessedContact objects.
    */
    public function getContacts(Communicator $communicator = null): ClassValidationArray
    {
        if ($communicator !== null) {
            $contactStd = $communicator->get('api/groups/'.$this->groupID. '/contacts');
            $contactStd = json_decode($contactStd);
            $contacts = RecipientFactory::createProcessedGroupsFromStdClassArray($contactStd);
            $this->contacts = $contacts;
            $this->contactsFetched = true;
        }
        return $this->contacts;
    }
    /**
    * Tells whether contacts has been fetched from the API or not.
    * @return boolean Whether the contacts has been fetched from the API or not.
    */
    public function contactsFetched(): bool
    {
        return $this->contactsFetched;
    }
    /**
    * @param Communicator     $communicator Used to add the membership to the API.
    * @param ProcessedContact $contact      The contact that is to be added to the Group.
    * @return ProcessedMembership
    */
    public function addMember(Communicator $communicator, ProcessedContact $contact): ProcessedMembership
    {
        $membership = new Membership($this->groupID, $contact->getID());
        $response = $communicator->post('api/memberships', $membership);
        $returnValue = ProcessedMembership(json_decode($response));
        if ($this->memberShipsFetched()) {
            $this->memberships[] = $returnValue;
        }
        return $returnValue;
    }

    /**
    * @param  \DateTimeZone $timezone The timezone that the user wants to get the DateTime in. Default is UTC.
    * @return \DateTime When the Component was added.
    */
    public function getCreated(\DateTimeZone $timezone = null): ?\DateTime
    {
        if (!is_null($timezone)) {
            $returnDate = clone $this->created;
            $returnDate->setTimeZone($timezone);
            return $returnDate;
        }
        return $this->created ?? null;
    }
    /**
    * Returns when the component was updated last.
    * @param  \DateTimeZone $timezone The timezone that the user wants to get the DateTime in. Default is UTC.
    * @return \DateTime When the contact was updated/modified last.
    */
    public function getUpdated(\DateTimeZone $timezone = null) : ?\DateTime
    {
        if (!is_null($timezone)) {
            $returnDate = clone $this->updated;
            $returnDate->setTimeZone($timezone);
            return $returnDate;
        }
        return $this->updated ?? null;
    }
    /**
   * Serializes the object to a value that can be serialized natively by json_encode().
   * @return array Associative.
   * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
   */
    public function jsonSerialize(): array
    {
        $parentArray = parent::jsonSerialize();
        $parentArray['Created'] = isset($this->created) ? $this->updated->format("Y-m-d\TH:i:s.").
            substr($this->updated->format('u'), 0, 3) : null;
        $parentArray['Updated'] = isset($this->created) ? $this->updated->format("Y-m-d\TH:i:s.").
            substr($this->updated->format('u'), 0, 3) : null;
        $parentArray['ID'] = $this->groupID;
        return $parentArray;
    }
    /**
    * Takes the given argument and replaces strings such as {id} to an actual value.
    * @param string $endPoint The endpoint to be corrected.
    * @return void
    */
    public function fillEndPoint(string &$endPoint): void
    {
        $endPoint = str_replace("{group}", $this->groupID, $endPoint);
    }
}