hooked.php

Event hook dispatch utility
git clone git://git.finwo.net/lib/hooked.php
Log | Files | Refs

Event.php (1507B)


      1 <?php
      2 
      3 namespace Finwo\Hooked;
      4 
      5 use Finwo\PropertyAccessor\PropertyAccessor;
      6 
      7 class Event
      8 {
      9     /**
     10      * @var string
     11      */
     12     protected $name = '';
     13 
     14     /**
     15      * @var array
     16      */
     17     protected $param = array();
     18 
     19     /**
     20      * @var PropertyAccessor
     21      */
     22     private $accessor;
     23 
     24     /**
     25      * Event constructor.
     26      * @param string $name
     27      * @param array $parameters
     28      */
     29     public function __construct($name = '', &$parameters = array())
     30     {
     31         $this->name  = $name;
     32         $this->param = &$parameters;
     33     }
     34 
     35     /**
     36      * @return PropertyAccessor
     37      */
     38     private function getAccessor()
     39     {
     40         if (is_null($this->accessor)) {
     41             $this->accessor = new PropertyAccessor(true);
     42         }
     43         return $this->accessor;
     44     }
     45 
     46     /**
     47      * @param $key
     48      * @return array|mixed|null
     49      * @throws \Exception
     50      */
     51     public function get($key)
     52     {
     53         // Try directly, might be the quickest way
     54         if (isset($this->{$key})) {
     55             return $this->{$key};
     56         }
     57 
     58         // Look deeper
     59         $accessor = $this->getAccessor();
     60         return $accessor->get($this, $key, '.');
     61     }
     62 
     63     public function set($key, &$value)
     64     {
     65         // Try directly, might be the quickest way
     66         if (isset($this->{$key})) {
     67             $this->{$key} = &$value;
     68             return $this;
     69         }
     70 
     71         // Look deeper
     72         $accessor = $this->getAccessor();
     73         $accessor->set($this, $key, $value, '.');
     74         return $this;
     75     }
     76 }