Advertisement
fruffl

Exception

Oct 9th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_Core
  6.      * @package    ILLI
  7.      * @link       http://illi.be
  8.      * @license    http://l.illi.be
  9.      * @copyright  ILLI Conference
  10.      */
  11.     NAMESPACE ILLI\Core\Exception;
  12.     USE ILLI\Core\Virtual\Member\iField as iField;
  13.     USE ReflectionClass     AS __ReflectionClass;
  14.     USE Exception           AS __Exception;
  15.  
  16.     /**
  17.      * ILLI Exception
  18.      *
  19.      * @category   ILLI_Core_Exception
  20.      * @package    ILLI
  21.      * @subpackage Core\Exception
  22.      * @namespace  ILLI\Core\Exception
  23.      * @link       http://illi.be
  24.      * @license    http://l.illi.be
  25.      * @copyright  ILLI Conference
  26.      * @since      2.0.1
  27.      * @version    3.0.1
  28.      */
  29.     CLASS Exception EXTENDS __Exception
  30.     {
  31.         USE \ILLI\Core\tObject;
  32.         USE \ILLI\Core\Virtual\Member\tField;
  33.        
  34.         private static $__hasCatched = [];
  35.        
  36.         public function __construct()
  37.         {
  38.             // define properties
  39.             $this
  40.             ->tField_define('previous', '\Exception', [])
  41.             ->tField_define('renderer', __NAMESPACE__.'\ExceptionMessage', [])
  42.             ->tField_define('arguments', 'array', [])
  43.             ->tField_define
  44.             (
  45.                 'unparsed', 'string',
  46.                 [
  47.                     iField::PROP_INIT       => 'Unknown Error.',
  48.                     iField::PROP_PRIVATE_SET    => TRUE,
  49.                     iField::PROP_PRIVATE_GET    => TRUE
  50.                 ]
  51.             )
  52.             ->tField_define
  53.             (
  54.                 'code', 'integer',
  55.                 [
  56.                     iField::PROP_INIT       => 0,
  57.                     iField::PROP_PRIVATE_SET    => TRUE,
  58.                     iField::PROP_PRIVATE_GET    => FALSE
  59.                 ]
  60.             )
  61.             ->tField_define
  62.             (
  63.                 'message', 'string',
  64.                 [
  65.                     iField::PROP_ON_GET => (function(array $data)
  66.                     {
  67.                         $data[iField::CALLBACK_PROP_CONTAINS] = $this->tField_get('renderer')->getParsed();
  68.                         return $data;
  69.                     }),
  70.                     iField::PROP_PRIVATE_SET    => TRUE,
  71.                     iField::PROP_PRIVATE_GET    => FALSE
  72.                 ]
  73.             );
  74.            
  75.             // parse arguments by type and store value
  76.             foreach(func_get_args() as $arg)
  77.             {
  78.                 if(is_integer($arg))
  79.                 {
  80.                     $this->tField_set('code', $arg);
  81.                 }
  82.                 else
  83.                 if(is_string($arg))
  84.                 {
  85.                     $this->tField_set('unparsed', $arg);
  86.                 }
  87.                 else
  88.                 if(is_array($arg))
  89.                 {
  90.                     $this->tField_set('arguments', $arg);
  91.                 }
  92.                 else
  93.                 if(is_object($arg))
  94.                 {
  95.                     if($arg instanceOf __Exception)
  96.                         $this->tField_set('previous', $arg);
  97.                 }
  98.             }
  99.            
  100.             // set message-parser
  101.             $this->tField_set
  102.             (
  103.                 'renderer',
  104.                 ExceptionMessage::create()
  105.                     ->setMessage($this->tField_get('unparsed'))
  106.                     ->setArguments($this->tField_get('arguments'))
  107.             );
  108.            
  109.             // load message-template by error-code
  110.             if(FALSE === $this->tField_isTouched('unparsed')
  111.             && TRUE  === $this->tField_isTouched('code'))
  112.             {
  113.                 $constants = (new __ReflectionClass(get_called_class()))->getConstants();
  114.                
  115.                 foreach($constants as $name => $value)
  116.                 {
  117.                     if($value !== $this->tField_get('code'))
  118.                         continue;
  119.                    
  120.                     $const = str_replace('CODE_', '', $name);
  121.                     if(!isset($constants[$const]))
  122.                         break;
  123.                        
  124.                     $this->tField_set('unparsed', $constants[$const]);
  125.                     $this->tField_get('renderer')->setMessage($this->tField_get('unparsed'));
  126.                 }
  127.             }
  128.             else
  129.             // load the exception default message
  130.             if(FALSE === $this->tField_isTouched('unparsed')
  131.             && FALSE === $this->tField_isTouched('code'))
  132.             {
  133.                 var_dump('load default message by exceptionclass: '.$this->tObject_getType());
  134.             }
  135.             else
  136.             // use the given message
  137.             if(TRUE === $this->tField_isTouched('unparsed'))
  138.             {
  139.                 var_dump('use message from constructor: '.$this->tField_get('unparsed'));
  140.             }
  141.            
  142.             self::$__hasCatched[] = get_called_class();
  143.            
  144.             parent::__construct
  145.             (
  146.                 $this->tField_get('message'),
  147.                 $this->tField_get('code'),
  148.                 $this->tField_get('previous')
  149.             );
  150.         }
  151.        
  152.         public function __set($key, $value)
  153.         {
  154.             if(FALSE === $this->tField_isPrivateSet($key))
  155.                 $this->tField_set($key, $value);
  156.         }
  157.        
  158.         public function __get($key)
  159.         {
  160.             if(FALSE === $this->tField_isPrivateGet($key))
  161.                 return $this->tField_get($key);
  162.         }
  163.        
  164.         public function getType()
  165.         {
  166.             return $this->tObject_getType();
  167.         }
  168.        
  169.         public function hasCatched($name)
  170.         {
  171.             return in_array($name, self::$__hasCatched);
  172.         }
  173.        
  174.         public function getHashCode()
  175.         {
  176.             return $this->tObject_getHashCode();
  177.         }
  178.        
  179.         public function toString()
  180.         {
  181.             return $this->tObject_toString();
  182.         }
  183.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement