summaryrefslogtreecommitdiffstats
path: root/src/SMS/SMS.php
blob: 4c34819099324de5f9cc0c1e65ed6603c0a9f1d8 (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
<?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;

/**
* Abstract class that all other SMS classes extends.
*/
abstract class SMS implements \JsonSerializable
{
    /**
    * Message priority level, normal (1) or high (2).
    * @var integer $priority
    */
    protected $prio = 1;
    /**
    * Sender's name or phone number.
    * @var string $from
    */
    protected $from;
    /**
    * The message the SMS should contain.
    * @var string $message
    */
    protected $message;



    /**
    * SMS Constructor.
    * @param string $sender  Phone number or name of the sender.
    * @param string $message Message content.
    */
    public function __construct(string $sender, string $message)
    {
        $this->setSender($sender);
        $this->message = $message;
    }
    /**
    * Sets what the content/message of the SMS should be.
    * @param string $message The message the SMS should contain.
    * @return void
    */
    public function setMessage(string $message): void
    {
        $this->message = $message;
    }
    /**
    * Setting priority high (1) will cost 0.10SEK more than setting normal (1) for Swedish customers.
    * For customers outside sweden the priority will be overritten to normal (1) by the API.
    * @param integer $priority Message priority level, normal (1) or high (2).
    * @return void
    */
    public function setPriority(int $priority): void
    {
        $this->prio = $priority;
    }
    /**
    * Sets the sender.
    * @param string $sender Sending name or phone number.
    * @return void
    */
    public function setSender(string  $sender): void
    {

        if (preg_match("/^\+?[0-9]+$/m", $sender) && strlen($sender) > 18) {
            trigger_error(
                "Sender number too long, max length is 18 digits",
                E_USER_WARNING
            );
        } elseif (preg_match("/[a-zA-Z]+/m", $sender) && strlen($sender) > 11) {
            trigger_error(
                "Sender string too long, non numeric senders have max length of 11 characters.",
                E_USER_WARNING
            );
        }
        // We do not truncate the sender as the API will do that and the developer might request the sender string back.
        $this->from = $sender;
    }
    /**
     * 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 = [
        'Prio' => $this->prio,
        'From' => $this->from,
        'Message'=> $this->message
        ];
        return $returnArray;
    }
    /**
    * Returns the object as a JSON string.
    * @return string
    */
    public function __toString(): string
    {
        return json_encode($this->jsonSerialize());
    }
}