summaryrefslogtreecommitdiffstats
path: root/src/Recipient/RecipientFactory.php
blob: 6e9b5aaab252618a34cf5e03ad1098ef684be4a9 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?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.1-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\ClassValidationArray;
use IP1\RESTClient\SMS\ProcessedOutGoingSMS;

/**
* Handles construction of Recipients.
*/
class RecipientFactory
{
    /**
    * Creates a Contact using the stdClass given.
    * @param string $jsonContact A JSON string matching the format of the IP1 SMS API.
    * @return Contact
    */
    public static function createContactFromJSON(string $jsonContact): Contact
    {
          return self::createContactFromStdClass(json_decode($jsonContact));
    }
    /**
    * Creates a Contact using the stdClass given.
    * @param \stdClass $stdContact An stdClass object matching the format of the IP1 SMS API.
    * @return Contact
    * @throws \InvalidArgumentException Thrown when required parameters in the argument is missing.
    */
    public static function createContactFromStdClass(\stdClass $stdContact): Contact
    {
        if (empty($stdContact->FirstName)) {
            throw new \InvalidArgumentException("stdClass argument must contain FirstName attribute");
        }
        if (empty($stdContact->Phone)) {
            throw new \InvalidArgumentException("stdClass argument must contain Phone attribute");
        }

        $contact = new Contact(
            $stdContact->FirstName,
            $stdContact->Phone,
            $stdContact->LastName ?? null,
            $stdContact->Title ?? null,
            $stdContact->Organization ?? null,
            $stdContact->Email ?? null,
            $stdContact->Notes ?? null
        );
        return $contact;
    }
    /**
    * Creates a Contact using the parameters given.
    * @param string      $firstName    The first name of the contact in question.
    * @param string      $phoneNumber  Contact phone number: with country code and without spaces and dashes.
    * @param string|null $lastName     Contact last name.
    * @param string|null $title        Contact title.
    * @param string|null $organization Contact company or other organization.
    * @param string|null $email        Contact email address.
    * @param string|null $notes        Contact notes.
    * @return Contact
    */
    public static function createContactFromAttributes(
        string $firstName,
        string $phoneNumber,
        ?string $lastName = null,
        ?string $title = null,
        ?string $organization = null,
        ?string $email = null,
        ?string $notes = null
    ) : Contact {
        return new Contact(
            $firstName,
            $phoneNumber,
            $lastName,
            $title,
            $organization,
            $email,
            $notes
        );
    }

    /**
    * Creates a ProcessedContact using the JSON given.
    * @param string $jsonContact A JSON string matching the format of the IP1 SMS API.
    * @return ProcessedContact
    */
    public static function createProcessedContactFromJSON(string $jsonContact): ProcessedContact
    {
        return self::createProcessedContactFromStdClass(json_decode($jsonContact));
    }
    /**
    * Take an array filled with contact stdClasses and returns a ClassValidationArray filled with ProcessedContact.
    * @param array $contactArray An array filled with stdClass contacts.
    * @return ClassValidationArray Filled with ProcessedContact.
    */
    public static function createProcessedContactFromStdClassArray(array $contactArray): ClassValidationArray
    {
        $contacts = new ClassValidationArray();
        foreach ($contactArray as $c) {
            $contacts[] = self::createProcessedContactFromStdClass($c);
        }
        return $contacts;
    }
    /**
    * Creates a ProcessedContact using the stdClass given.
    * @param \stdClass $stdContact An stdClass object matching the format of the IP1 SMS API.
    * @return ProcessedContact
    * @throws \InvalidArgumentException Thrown when required parameters in the argument is missing.
    */
    public static function createProcessedContactFromStdClass(\stdClass $stdContact): ProcessedContact
    {
        if (empty($stdContact->FirstName)) {
            throw new \InvalidArgumentException("stdClass argument must contain FirstName attribute");
        }
        if (empty($stdContact->Phone)) {
            throw new \InvalidArgumentException("stdClass argument must contain Phone attribute");
        }
        $contact = new ProcessedContact(
            $stdContact->FirstName,
            $stdContact->Phone,
            $stdContact->ID,
            $stdContact->OwnerID,
            $stdContact->LastName ?? null,
            $stdContact->Title ?? null,
            $stdContact->Organization ?? null,
            $stdContact->Email ?? null,
            $stdContact->Notes ?? null,
            isset($stdContact->Created) ? new \DateTime($stdContact->Created, new \DateTimeZone("UTC")) : null,
            isset($stdContact->Modified) ? new \DateTime($stdContact->Modified, new \DateTimeZone("UTC")) : null
        );
        return $contact;
    }
    /**
    * Takes a JSON string group and returns a ProcessedGroup.
    * @param string $jsonGroup A single group JSON string.
    * @return ProcessedGroup
    */
    public static function createProcessedGroupFromJSON(string $jsonGroup): ProcessedGroup
    {
        return self::createProcessedGroupFromStdClass(json_decode($jsonGroup));
    }
    /**
    * Takes a stdClass group and returns a ProcessedGroup.
    * @param \stdClass $stdGroup A single stdClass group.
    * @return ProcessedGroup
    */
    public static function createProcessedGroupFromStdClass(\stdClass $stdGroup): ProcessedGroup
    {
        return new ProcessedGroup(
            $stdGroup->Name,
            $stdGroup->Color,
            $stdGroup->ID,
            $stdGroup->OwnerID,
            new \DateTime($stdGroup->Created),
            new \DateTime($stdGroup->Modified)
        );
    }
    /**
    * @param string $jsonMembership A membership JSON string.
    * @return ProcessedMembership
    */
    public static function createProcessedMembershipFromJSON(string $jsonMembership): ProcessedMembership
    {
        return self::createProcessedMembershipFromStdClass(json_decode($jsonMembership));
    }
    /**
    * @param array $stdGroups An array filled with stdclass Group.
    * @return ClassValidationArray Filled with ProcessedGroup.
    */
    public static function createProcessedGroupsFromStdClassArray(array $stdGroups): ClassValidationArray
    {
        $groups = new ClassValidationArray();
        foreach ($stdGroups as $value) {
            $groups[] = self::createProcessedGroupFromStdClass($value);
        }
        return $groups;
    }
    /**
    * @param \stdClass $stdMembership An stdClass membership.
    * @return ProcessedMembership
    */
    public static function createProcessedMembershipFromStdClass(\stdClass $stdMembership): ProcessedMembership
    {
        return new ProcessedMembership(
            $stdMembership->Group,
            $stdMembership->Contact,
            $stdMembership->ID,
            $stdMembership->OwnerID,
            new \DateTime($stdMembership->Created)
        );
    }
    /**
    * @param array $stdMemberships An stdClass Membership.
    * @return ClassValidationArray Filled with ProcessedMembership.
    */
    public static function createProcessedMembershipsFromStdClassArray(array $stdMemberships): ClassValidationArray
    {
        $memberships = new ClassValidationArray();
        foreach ($stdMemberships as $m) {
            $memberships[] = self::createProcessedMembershipFromStdClass($m);
        }
        return $memberships;
    }
    /**
    * @param string $membershipJSONArray An stdClass Group array encoded as a JSON string.
    * @return ClassValidationArray Filled with ProcessedMembership
    */
    public static function createProcessedMembershipsFromStringArray(string $membershipJSONArray): ClassValidationArray
    {
        return self::createProcessedMembershipsFromStdClassArray(json_decode($membershipJSONArray));
    }
    /**
    * Creates a ProcessedOutGoingSMS from an stdClass object.
    * @param \stdClass $stdClassSMS An stdClass.
    * @return ProcessedOutGoingSMS
    */
    public static function createProcessedOutGoingSMSFromStdClass(\stdClass  $stdClassSMS): ProcessedOutGoingSMS
    {
        return new ProcessedOutGoingSMS(
            $stdClassSMS->From,
            $stdClassSMS->Message,
            $stdClassSMS->To,
            $stdClassSMS->ID,
            new \DateTime($stdClassSMS->Created ?? null),
            new \DateTime($stdClassSMS->Updated ?? null),
            $stdClassSMS->Status,
            $stdClassSMS->StatusDescription,
            $stdClassSMS->BundleID
        );
    }
    /**
    * Creates a ClassValidationArray filled with ProcessedOutGoingSMS given an array of stdClass.
    * @param array $stdClassArray An array of stdClass OutGoingSMS.
    * @return ClassValidationArray Filled with ProcessedOutGoingSMS.
    */
    public static function createProcessedOutGoingSMSFromStdClassArray(array $stdClassArray): ClassValidationArray
    {
        $array = new ClassValidationArray(ProcessedOutGoingSMS::class);
        foreach ($stdClassArray as $stdClass) {
            $array[]= self::createProcessedOutGoingSMSFromStdClass($stdClass);
        }
        return $array;
    }
    /**
    * Creates a ClassValidationArray given a JSON string.
    * @param string $jsonArray A JSON string that when decoded becomes an array of stdClass ProcessedOutGoingSMS.
    * @return ClassValidationArray Filled with ProcessedOutGoingSMS.
    */
    public static function createProcessedOutGoingSMSFromJSONArray(string $jsonArray): ClassValidationArray
    {
        return self::createProcessedOutGoingSMSFromStdClassArray(json_decode($jsonArray));
    }
}