Message.php 2.3 KB
Newer Older
Juliper committed
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
<?php
/**
 * Nexmo Client Library for PHP
 *
 * @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
 * @license   https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
 */

namespace Nexmo\Message\Response;
use Nexmo\Client\Response\Response;
use Nexmo\Client\Response\ResponseInterface;
use Nexmo\Message\Callback\Receipt;

class Message extends Response implements ResponseInterface
{
    /**
     * @var Receipt
     */
    protected $receipt;

    public function __construct(Array $data, Receipt $receipt = null)
    {
        $this->expected = array(
            'status',
            'message-id',
            'to',
            'message-price',
            'network'
        );

        //default value
        $data = array_merge(array('client-ref' => null, 'remaining-balance' => null), $data);

        $return = parent::__construct($data);

        //validate receipt
        if(!$receipt){
            return $return;
        }

        if($receipt->getId() != $this->getId()){
            throw new \UnexpectedValueException('receipt id must match message id');
        }

        $this->receipt = $receipt;

        return $receipt;
    }

    /**
     * @return int
     */
    public function getStatus()
    {
        return (int) $this->data['status'];
    }

    /**
     * @return string
     */
    public function getId()
    {
        return (string) $this->data['message-id'];
    }

    /**
     * @return string
     */
    public function getTo()
    {
        return (string) $this->data['to'];
    }

    /**
     * @return string
     */
    public function getBalance()
    {
        return (string) $this->data['remaining-balance'];
    }

    /**
     * @return string
     */
    public function getPrice()
    {
        return (string) $this->data['message-price'];
    }

    /**
     * @return string
     */
    public function getNetwork()
    {
        return (string) $this->data['network'];
    }

    /**
     * @return string
     */
    public function getClientRef()
    {
        return (string) $this->data['client-ref'];
    }

    /**
     * @return Receipt|null
     */
    public function getReceipt()
    {
        return $this->receipt;
    }

    /**
     * @return bool
     */
    public function hasReceipt()
    {
        return $this->receipt instanceof Receipt;
    }
}