"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Email/Address.php" (13 Dec 2019, 3204 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Address.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya\Email;
   16 
   17 /**
   18  * A single Email address, including properties for the parts and string casting.
   19  *
   20  * @package Papaya-Library
   21  * @subpackage Email
   22  *
   23  * @property string $address
   24  * @property string $email
   25  * @property string $name
   26  */
   27 class Address {
   28   /**
   29    * Recipient name
   30    *
   31    * @var string
   32    */
   33   private $_name = '';
   34 
   35   /**
   36    * Recipient email
   37    *
   38    * @var string
   39    */
   40   private $_email = '';
   41 
   42   /**
   43    * Initialize object wiht address if provided.
   44    *
   45    * @param string $address
   46    */
   47   public function __construct($address = NULL) {
   48     if (NULL !== $address) {
   49       $this->setAddress($address);
   50     }
   51   }
   52 
   53   /**
   54    * Cast object to string. Returns "email" or "name <email>".
   55    *
   56    * @return string
   57    */
   58   public function __toString() {
   59     if (empty($this->_name)) {
   60       return $this->_email;
   61     }
   62     return $this->_name.' <'.$this->_email.'>';
   63   }
   64 
   65   /**
   66    * Set address from string (can include a name)
   67    *
   68    * @param $address
   69    */
   70   protected function setAddress($address) {
   71     if (\preg_match('~^\s*(.*?)\s*<([^>]+)>~', $address, $matches)) {
   72       $this->_name = $matches[1];
   73       $this->_email = $matches[2];
   74     } else {
   75       $this->_email = $address;
   76     }
   77   }
   78 
   79   /**
   80    * Set recipient name
   81    *
   82    * @param string $name
   83    */
   84   protected function setName($name) {
   85     $this->_name = $name;
   86   }
   87 
   88   /**
   89    * @param $name
   90    *
   91    * @return bool
   92    */
   93   public function __isset($name) {
   94     switch ($name) {
   95       case 'name' :
   96       case 'email' :
   97       case 'address' :
   98         return TRUE;
   99     }
  100     return FALSE;
  101   }
  102 
  103   /**
  104    * Dynamic property setter
  105    *
  106    * @param string $name
  107    * @param string $value
  108    *
  109    * @throws \InvalidArgumentException
  110    */
  111   public function __set($name, $value) {
  112     switch ($name) {
  113       case 'name' :
  114         $this->setName($value);
  115         return;
  116       case 'email' :
  117       case 'address' :
  118         $this->setAddress($value);
  119         return;
  120     }
  121     throw new \InvalidArgumentException(
  122       \sprintf('InvalidArgumentException: Unknown property "%s".', $name)
  123     );
  124   }
  125 
  126   /**
  127    * Dynamic property getter
  128    *
  129    * @param string $name
  130    *
  131    * @throws \InvalidArgumentException
  132    *
  133    * @return string
  134    */
  135   public function __get($name) {
  136     switch ($name) {
  137       case 'name' :
  138         return $this->_name;
  139       case 'email' :
  140         return $this->_email;
  141       case 'address' :
  142         return $this->__toString();
  143     }
  144     throw new \InvalidArgumentException(
  145       \sprintf('InvalidArgumentException: Unknown property "%s".', $name)
  146     );
  147   }
  148 }