File indexing completed on 2025-03-02 05:29:35

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Mobile
0017  * @subpackage Zend_Mobile_Push
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Mobile_Push_Message_Abstract **/
0024 // require_once 'Zend/Mobile/Push/Message/Abstract.php';
0025 
0026 /**
0027  * Apns Message
0028  *
0029  * @category   Zend
0030  * @package    Zend_Mobile
0031  * @subpackage Zend_Mobile_Push
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  * @version    $Id$
0035  */
0036 class Zend_Mobile_Push_Message_Apns extends Zend_Mobile_Push_Message_Abstract
0037 {
0038     /**
0039      * Badge Number
0040      *
0041      * @var int
0042      */
0043     protected $_badge;
0044 
0045     /**
0046      * Alert
0047      *
0048      * @var array
0049      */
0050     protected $_alert  = array();
0051 
0052     /**
0053      * Expiration
0054      *
0055      * @var int
0056      */
0057     protected $_expire;
0058 
0059     /**
0060      * Sound
0061      *
0062      * @var string
0063      */
0064     protected $_sound = 'default';
0065 
0066     /**
0067      * Custom Data
0068      *
0069      * @var array
0070      */
0071     protected $_custom = array();
0072 
0073     /**
0074      * Get Alert
0075      *
0076      * @return array
0077      */
0078     public function getAlert()
0079     {
0080         return $this->_alert;
0081     }
0082 
0083     /**
0084      * Set Alert
0085      *
0086      * @param  string      $text
0087      * @param  string|null $actionLocKey
0088      * @param  string|null $locKey
0089      * @param  array|null  $locArgs
0090      * @param  string|null $launchImage
0091      * @throws Zend_Mobile_Push_Message_Exception
0092      * @return Zend_Mobile_Push_Message_Apns
0093      */
0094     public function setAlert($text, $actionLocKey=null, $locKey=null, $locArgs=null, $launchImage=null)
0095     {
0096         if ($text !== null && !is_string($text)) {
0097             throw new Zend_Mobile_Push_Message_Exception('$text must be a string');
0098         }
0099 
0100         if ($actionLocKey !== null && !is_string($actionLocKey)) {
0101             throw new Zend_Mobile_Push_Message_Exception('$actionLocKey must be a string');
0102         }
0103 
0104         if ($locKey !== null && !is_string($locKey)) {
0105             throw new Zend_Mobile_Push_Message_Exception('$locKey must be a string');
0106         }
0107 
0108         if ($locArgs !== null) {
0109             if (!is_array($locArgs)) {
0110                 throw new Zend_Mobile_Push_Message_Exception('$locArgs must be an array of strings');
0111             } else {
0112                 foreach ($locArgs as $str) {
0113                     if (!is_string($str)) {
0114                         throw new Zend_Mobile_Push_Message_Exception('$locArgs contains an item that is not a string');
0115                     }
0116                 }
0117             }
0118         }
0119 
0120         if (null !== $launchImage && !is_string($launchImage)) {
0121             throw new Zend_Mobile_Push_Message_Exception('$launchImage must be a string');
0122         }
0123 
0124         $this->_alert = array(
0125             'body'           => $text,
0126             'action-loc-key' => $actionLocKey,
0127             'loc-key'        => $locKey,
0128             'loc-args'       => $locArgs,
0129             'launch-image'   => $launchImage,
0130         );
0131         return $this;
0132     }
0133 
0134     /**
0135      * Get Badge
0136      *
0137      * @return int
0138      */
0139     public function getBadge()
0140     {
0141         return $this->_badge;
0142     }
0143 
0144     /**
0145      * Set Badge
0146      *
0147      * @param int $badge
0148      * @return Zend_Mobile_Push_Message_Apns
0149      * @throws Zend_Mobile_Push_Message_Exception
0150      */
0151     public function setBadge($badge)
0152     {
0153         if (!is_null($badge) && !is_numeric($badge)) {
0154             throw new Zend_Mobile_Push_Message_Exception('$badge must be an integer');
0155         }
0156         if (!is_null($badge) && $badge < 0) {
0157             throw new Zend_Mobile_Push_Message_Exception('$badge must be greater or equal to 0');
0158         }
0159         $this->_badge = $badge;
0160     }
0161 
0162     /**
0163      * Get Expire
0164      *
0165      * @return int
0166      */
0167     public function getExpire()
0168     {
0169         return $this->_expire;
0170     }
0171 
0172     /**
0173      * Set Expire
0174      *
0175      * @param int $expire
0176      * @return Zend_Mobile_Push_Message_Apns
0177      * @throws Zend_Mobile_Push_Message_Exception
0178      */
0179     public function setExpire($expire)
0180     {
0181         if (!is_numeric($expire)) {
0182             throw new Zend_Mobile_Push_Message_Exception('$expire must be an integer');
0183         }
0184         $this->_expire = (int) $expire;
0185         return $this;
0186     }
0187 
0188     /**
0189      * Get Sound
0190      *
0191      * @return string
0192      */
0193     public function getSound()
0194     {
0195         return $this->_sound;
0196     }
0197 
0198     /**
0199      * Set Sound
0200      *
0201      * @param string $sound
0202      * @return Zend_Mobile_Push_Message_Apns
0203      * @throws Zend_Mobile_Push_Message_Exception
0204      */
0205     public function setSound($sound)
0206     {
0207         if (!is_string($sound)) {
0208             throw new Zend_Mobile_Push_Message_Exception('$sound must be a string');
0209         }
0210         $this->_sound = $sound;
0211         return $this;
0212     }
0213 
0214     /**
0215      * Add Custom Data
0216      *
0217      * @param string $key
0218      * @param mixed $value
0219      * @return Zend_Mobile_Push_Message_Apns
0220      * @throws Zend_Mobile_Push_Message_Exception
0221      */
0222     public function addCustomData($key, $value)
0223     {
0224         if (!is_string($key)) {
0225             throw new Zend_Mobile_Push_Message_Exception('$key is not a string');
0226         }
0227         if ($key == 'aps') {
0228             throw new Zend_Mobile_Push_Message_Exception('$key must not be aps as it is reserved by apple');
0229         }
0230         $this->_custom[$key] = $value;
0231     }
0232 
0233     /**
0234      * Clear Custom Data
0235      *
0236      * @return throw new Zend_Mobile_Push_Message_Apns
0237      */
0238     public function clearCustomData()
0239     {
0240         $this->_custom = array();
0241         return $this;
0242     }
0243 
0244     /**
0245      * Set Custom Data
0246      *
0247      * @param  array $array
0248      * @throws Zend_Mobile_Push_Message_Exception
0249      * @return Zend_Mobile_Push_Message_Apns
0250      */
0251     public function setCustomData($array)
0252     {
0253         $this->_custom = array();
0254         foreach ($array as $k => $v) {
0255             $this->addCustomData($k, $v);
0256         }
0257         return $this;
0258     }
0259 
0260     /**
0261      * Get Custom Data
0262      *
0263      * @return array
0264      */
0265     public function getCustomData()
0266     {
0267         return $this->_custom;
0268     }
0269 
0270     /**
0271      * Validate this is a proper Apns message
0272      *
0273      * @return boolean
0274      */
0275     public function validate()
0276     {
0277         if (!is_string($this->_token) || strlen($this->_token) === 0) {
0278             return false;
0279         }
0280         if (null != $this->_id && !is_numeric($this->_id)) {
0281             return false;
0282         }
0283         return true;
0284     }
0285 }