summaryrefslogtreecommitdiffstats
path: root/src/Recipient/Membership.php
blob: 3f56417a148626f6adad4e484a74adb13ccd1bce (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
<?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\ProcessableComponentInterface;
use IP1\RESTClient\Recipient\ProcessedGroup;
use IP1\RESTClient\Recipient\ProcessedContact;

/**
* Membership is the bridge between ProcessedGroup and ProcessedContact.
*/
class Membership implements ProcessableComponentInterface
{

    /**
    * A Group ID.
    * @var integer $groupID
    */
    protected $groupID;
/**
    * A Contact ID.
    * @var integer $contactID
    */
    protected $contactID;
/**
    * Membership Constructor.
    * @param integer $groupID   A Group ID.
    * @param integer $contactID A Contact ID.
    */
    public function __construct(int $groupID, int $contactID)
    {
        $this->groupID = $groupID;
        $this->contactID = $contactID;
    }
    /**
    * Returns Group ID.
    * @return integereger
    */
    public function getGroupID(): int
    {
        return $this->groupID;
    }
    /**
    * Returns Contact ID.
    * @return integereger
    */
    public function getContactID(): int
    {
        return $this->contactID;
    }
    /**
    * Returns the Group this membership is referenced to.
    * @param Communicator $communicator Used to fetch the Group from the API.
    * @return ProcessedGroup
    */
    public function getGroup(Communicator $communicator): ProcessedGroup
    {
            $groupJSON = $communicator->get('api/groups/'.$this->groupID);
            $group = RecipientFactory::createProcessedGroupFromJSON($groupJSON);
            return $group;
    }
    /**
    * Returns the Contact this membership is referenced to.
    * @param Communicator $communicator Used to fetch the Contact from the API.
    * @return ProcessedContact
    */
    public function getContact(Communicator $communicator): ProcessedContact
    {
            $contactJSON = $communicator->get('api/groups/'.$this->contactID);
            $contact = RecipientFactory::createProcessedGroupFromJSON($contactJSON);
            return $contact;
    }
    /**
     * 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 = [
        'Group' => $this->groupID,
        'Contact' => $this->contactID,
        ];
        return $returnArray;
    }
    /**
    * Returns the object as a JSON string.
    * @return string
    */
    public function __toString(): string
    {
        return json_encode($this->jsonSerialize());
    }
}