web/lib/Zend/Mobile/Push/Message/Apns.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Mobile
       
    17  * @subpackage Zend_Mobile_Push
       
    18  * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id$
       
    21  */
       
    22 
       
    23 /** Zend_Mobile_Push_Message_Abstract **/
       
    24 require_once 'Zend/Mobile/Push/Message/Abstract.php';
       
    25 
       
    26 /**
       
    27  * Apns Message
       
    28  *
       
    29  * @category   Zend
       
    30  * @package    Zend_Mobile
       
    31  * @subpackage Zend_Mobile_Push
       
    32  * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  * @version    $Id$
       
    35  */
       
    36 class Zend_Mobile_Push_Message_Apns extends Zend_Mobile_Push_Message_Abstract
       
    37 {
       
    38     /**
       
    39      * Badge Number
       
    40      *
       
    41      * @var int
       
    42      */
       
    43     protected $_badge;
       
    44 
       
    45     /**
       
    46      * Alert
       
    47      *
       
    48      * @var array
       
    49      */
       
    50     protected $_alert  = array();
       
    51 
       
    52     /**
       
    53      * Expiration
       
    54      *
       
    55      * @var int
       
    56      */
       
    57     protected $_expire;
       
    58 
       
    59     /**
       
    60      * Sound
       
    61      *
       
    62      * @var string
       
    63      */
       
    64     protected $_sound = 'default';
       
    65 
       
    66     /**
       
    67      * Custom Data
       
    68      *
       
    69      * @var array
       
    70      */
       
    71     protected $_custom = array();
       
    72 
       
    73     /**
       
    74      * Get Alert
       
    75      *
       
    76      * @return array
       
    77      */
       
    78     public function getAlert()
       
    79     {
       
    80         return $this->_alert;
       
    81     }
       
    82 
       
    83     /**
       
    84      * Set Alert
       
    85      *
       
    86      * @param string $text
       
    87      * @param string $actionLocKey
       
    88      * @param string $locKey
       
    89      * @param array $locArgs
       
    90      * @param string $launchImage
       
    91      * @return Zend_Mobile_Push_Message_Apns
       
    92      */
       
    93     public function setAlert($text, $actionLocKey=null, $locKey=null, $locArgs=null, $launchImage=null)
       
    94     {
       
    95         if ($text !== null && !is_string($text)) {
       
    96             throw new Zend_Mobile_Push_Message_Exception('$text must be a string');
       
    97         }
       
    98 
       
    99         if ($actionLocKey !== null && !is_string($actionLocKey)) {
       
   100             throw new Zend_Mobile_Push_Message_Exception('$actionLocKey must be a string');
       
   101         }
       
   102 
       
   103         if ($locKey !== null && !is_string($locKey)) {
       
   104             throw new Zend_Mobile_Push_Message_Exception('$locKey must be a string');
       
   105         }
       
   106 
       
   107         if ($locArgs !== null) {
       
   108             if (!is_array($locArgs)) {
       
   109                 throw new Zend_Mobile_Push_Message_Exception('$locArgs must be an array of strings');
       
   110             } else {
       
   111                 foreach ($locArgs as $str) {
       
   112                     if (!is_string($str)) {
       
   113                         throw new Zend_Mobile_Push_Message_Exception('$locArgs contains an item that is not a string');
       
   114                     }
       
   115                 }
       
   116             }
       
   117         }
       
   118 
       
   119         if (null !== $launchImage && !is_string($launchImage)) {
       
   120             throw new Zend_Mobile_Push_Message_Exception('$launchImage must be a string');
       
   121         }
       
   122 
       
   123         $this->_alert = array(
       
   124             'body'           => $text,
       
   125             'action-loc-key' => $actionLocKey,
       
   126             'loc-key'        => $locKey,
       
   127             'loc-args'       => $locArgs,
       
   128             'launch-image'   => $launchImage,
       
   129         );
       
   130         return $this;
       
   131     }
       
   132 
       
   133     /**
       
   134      * Get Badge
       
   135      *
       
   136      * @return int
       
   137      */
       
   138     public function getBadge()
       
   139     {
       
   140         return $this->_badge;
       
   141     }
       
   142 
       
   143     /**
       
   144      * Set Badge
       
   145      *
       
   146      * @param int $badge
       
   147      * @return Zend_Mobile_Push_Message_Apns
       
   148      * @throws Zend_Mobile_Push_Message_Exception
       
   149      */
       
   150     public function setBadge($badge)
       
   151     {
       
   152         if (!is_null($badge) && !is_numeric($badge)) {
       
   153             throw new Zend_Mobile_Push_Message_Exception('$badge must be an integer');
       
   154         }
       
   155         if (!is_null($badge) && $badge < 0) {
       
   156             throw new Zend_Mobile_Push_Message_Exception('$badge must be greater or equal to 0');
       
   157         }
       
   158         $this->_badge = $badge;
       
   159     }
       
   160 
       
   161     /**
       
   162      * Get Expire
       
   163      *
       
   164      * @return int
       
   165      */
       
   166     public function getExpire()
       
   167     {
       
   168         return $this->_expire;
       
   169     }
       
   170 
       
   171     /**
       
   172      * Set Expire
       
   173      *
       
   174      * @param int $expire
       
   175      * @return Zend_Mobile_Push_Message_Apns
       
   176      * @throws Zend_Mobile_Push_Message_Exception
       
   177      */
       
   178     public function setExpire($expire)
       
   179     {
       
   180         if (!is_numeric($expire)) {
       
   181             throw new Zend_Mobile_Push_Message_Exception('$expire must be an integer');
       
   182         }
       
   183         $this->_expire = (int) $expire;
       
   184         return $this;
       
   185     }
       
   186 
       
   187     /**
       
   188      * Get Sound
       
   189      *
       
   190      * @return string
       
   191      */
       
   192     public function getSound()
       
   193     {
       
   194         return $this->_sound;
       
   195     }
       
   196 
       
   197     /**
       
   198      * Set Sound
       
   199      *
       
   200      * @param string $sound
       
   201      * @return Zend_Mobile_Push_Message_Apns
       
   202      * @throws Zend_Mobile_Push_Message_Exception
       
   203      */
       
   204     public function setSound($sound)
       
   205     {
       
   206         if (!is_string($sound)) {
       
   207             throw new Zend_Mobile_Push_Message_Exception('$sound must be a string');
       
   208         }
       
   209         $this->_sound = $sound;
       
   210         return $this;
       
   211     }
       
   212 
       
   213     /**
       
   214      * Add Custom Data
       
   215      *
       
   216      * @param string $key
       
   217      * @param mixed $value
       
   218      * @return Zend_Mobile_Push_Message_Apns
       
   219      * @throws Zend_Mobile_Push_Message_Exception
       
   220      */
       
   221     public function addCustomData($key, $value)
       
   222     {
       
   223         if (!is_string($key)) {
       
   224             throw new Zend_Mobile_Push_Message_Exception('$key is not a string');
       
   225         }
       
   226         if ($key == 'aps') {
       
   227             throw new Zend_Mobile_Push_Message_Exception('$key must not be aps as it is reserved by apple');
       
   228         }
       
   229         $this->_custom[$key] = $value;
       
   230     }
       
   231 
       
   232     /**
       
   233      * Clear Custom Data
       
   234      *
       
   235      * @return throw new Zend_Mobile_Push_Message_Apns
       
   236      */
       
   237     public function clearCustomData()
       
   238     {
       
   239         $this->_custom = array();
       
   240         return $this;
       
   241     }
       
   242 
       
   243     /**
       
   244      * Set Custom Data
       
   245      *
       
   246      * @param array $data
       
   247      * @return Zend_Mobile_Push_Message_Apns
       
   248      * @throws Zend_Mobile_Push_Message_Exception
       
   249      */
       
   250     public function setCustomData($array)
       
   251     {
       
   252         $this->_custom = array();
       
   253         foreach ($array as $k => $v) {
       
   254             $this->addCustomData($k, $v);
       
   255         }
       
   256         return $this;
       
   257     }
       
   258 
       
   259     /**
       
   260      * Get Custom Data
       
   261      *
       
   262      * @return array
       
   263      */
       
   264     public function getCustomData()
       
   265     {
       
   266         return $this->_custom;
       
   267     }
       
   268 
       
   269     /**
       
   270      * Validate this is a proper Apns message
       
   271      *
       
   272      * @return boolean
       
   273      */
       
   274     public function validate()
       
   275     {
       
   276         if (!is_string($this->_token) || strlen($this->_token) === 0) {
       
   277             return false;
       
   278         }
       
   279         if (null != $this->_id && !is_numeric($this->_id)) {
       
   280             return false;
       
   281         }
       
   282         return true;
       
   283     }
       
   284 }