summaryrefslogtreecommitdiffstats
path: root/src/SMS/OutGoingSMS.php
blob: 15a628125db38911282fe6b8144eb7ed7ab835a7 (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
<?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.1.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\SMS;

use IP1\RESTClient\Recipient\ProcessedContact;
use IP1\RESTClient\Recipient\Group;
use IP1\RESTClient\Core\ProcessableComponentInterface;

/**
* Class that is used when wanting to send SMSes to the API.
*/
class OutGoingSMS extends SMS implements ProcessableComponentInterface
{
    /**
    * Contains all the phone numbers the SMS should be sent to.
    * @var array $numbers Phone numbers
    */
    protected $numbers = [];
    /**
    * Contains all the ProcessedContact the SMS should be sent to.
    * @var array $contacts
    */
    protected $contacts = [];
    /**
    * Contains all the Group the SMS should be sent to.
    * @var array $groups
    */
    protected $groups = [];
    /**
    * If emails should be sent to the recipients aswell.
    * @var boolean $email
    */
    protected $email = false;

    /**
    * Adds the number to the recipient list.
    * @param string $number A number that should be added to the recipient list.
    * @return void
    */
    public function addNumber(string $number): void
    {
        $this->numbers[] = $number;
    }
    /**
    * Adds the given array of numbers to the recipient list.
    * @param array $numbers An array of numbers(string).
    * @return void
    */
    public function addAllNumbers(array $numbers): void
    {
        $this->numbers = array_merge($this->numbers, $numbers);
    }
    /**
    * Removes the given index from the number recipient list.
    * @param integer $index The index being requested for deletion.
    * @return void
    */
    public function removeNumber(int $index): void
    {
        unset($this->numbers[$index]);
        $this->numbers = array_values($this->numbers);
    }
    /**
    * Returns the number in the given index.
    * @param integer $index The index being requested.
    * @return string    Phone number.
    * @throws \UndefinedOffsetException Thrown when the index requested does not exist.
    */
    public function getNumber(int $index): string
    {
        if ($index >= count($this->numbers)) {
            throw new \UndefinedOffsetException();
        }
        return $this->numbers[$index];
    }
    /**
    * Returns all the number recipient list.
    * @return array
    */
    public function getAllNumbers(): array
    {
        return $this->numbers;
    }
    /**
    * Adds the ProcessedContact to the recipient list.
    * @param ProcessedContact $contact A ProcessedContact that should be added to the recipient list.
    * @return void
    */
    public function addContact(ProcessedContact $contact): void
    {
        $this->contacts[] = $contact;
    }
    /**
    * Removes the Contact in the given index and reindexes the array.
    * @param integer $index The index being requested for deletion.
    * @return void
    */
    public function removeContact(int $index): void
    {
        unset($this->contacts[$index]);
        $this->contacts = array_values($this->contacts);
    }
    /**
    * Adds the given array of contacts to the Contact recipient list.
    * @param array $contacts An array of Contact.
    * @return void
    */
    public function addAllContacts(array $contacts): void
    {
        $this->contacts = array_merge($this->contacts, $contacts);
    }
    /**
    * Adds the given Group to the recipient list.
    * @param Group $group A Group that should be added to the recipient list.
    * @return void
    */
    public function addGroup(Group $group): void
    {
        $this->groups[] = $group;
    }
    /**
    * Adds the given array of Groups to the recipient list.
    * @param array $groups An array of Groups.
    * @return void
    */
    public function addAllGroups(array $groups): void
    {
        $this->groups = array_merge($this->groups, $groups);
    }
    /**
    * Removes the Group in the given index and reindexes the array.
    * @param integer $index The index being requested for deletion.
    * @return void
    */
    public function removeGroup(int $index): void
    {
        unset($this->groups[$index]);
        $this->groups = array_values($this->groups);
    }
    /**
    * Returns the group in the given index.
    * @param integer $index The index being requested.
    * @return Group
    * @throws \UndefinedOffsetException Thrown when the index requested does not exist.
    */
    public function getGroup(int $index): Group
    {
        if ($index >= count($this->groups)) {
            throw new \UndefinedOffsetException();
        }
        return $this->groups[$index];
    }
    /**
    * Returns the Group recipient list.
    * @return array An array of Group.
    */
    public function getAllGroups(): array
    {
        return $this->groups;
    }


    /**
     * 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
    {

        $returnArray = parent::jsonSerialize();
        $returnArray['Email'] = $this->email;
        $returnArray['Numbers'] = $this->numbers;
        if (count($this->contacts) > 0) {
            $returnArray['Contacts'] = [];
            foreach ($this->contacts as $contact) {
                $returnArray['Contacts'][] = $contact->getID();
            }
        }
        if (count($this->groups) > 0) {
            $returnArray['Groups'] = [];
            foreach ($this->groups as $group) {
                $returnArray['Groups'][] = $group->getID();
            }
        }
        return $returnArray;
    }
}