1 <?php
  2 
  3 /**
  4  * Extension Point Class.
  5  *
  6  * @author gharlan
  7  *
  8  * @package redaxo\core
  9  */
 10 class rex_extension_point
 11 {
 12     private $name;
 13     private $subject;
 14     private $params = [];
 15     private $extensionParams = [];
 16     private $readonly = false;
 17 
 18     /**
 19      * Constructor.
 20      *
 21      * @param string $name
 22      * @param mixed  $subject
 23      * @param array  $params
 24      * @param bool   $readonly
 25      */
 26     public function __construct($name, $subject = null, array $params = [], $readonly = false)
 27     {
 28         $this->name = $name;
 29         $this->subject = $subject;
 30         $this->params = $params;
 31         $this->readonly = $readonly;
 32     }
 33 
 34     /**
 35      * Returns the name.
 36      *
 37      * @return string
 38      */
 39     public function getName()
 40     {
 41         return $this->name;
 42     }
 43 
 44     /**
 45      * Sets the subject.
 46      *
 47      * @param mixed $subject
 48      *
 49      * @throws rex_exception
 50      */
 51     public function setSubject($subject)
 52     {
 53         if ($this->isReadonly()) {
 54             throw new rex_exception('Subject can\'t be adjusted in readonly extension points');
 55         }
 56         $this->subject = $subject;
 57     }
 58 
 59     /**
 60      * Returns the subject.
 61      *
 62      * @return mixed
 63      */
 64     public function getSubject()
 65     {
 66         return $this->subject;
 67     }
 68 
 69     /**
 70      * Sets a param.
 71      *
 72      * @param string $key
 73      * @param mixed  $value
 74      *
 75      * @throws rex_exception
 76      */
 77     public function setParam($key, $value)
 78     {
 79         if ($this->isReadonly()) {
 80             throw new rex_exception('Params can\'t be adjusted in readonly extension points');
 81         }
 82         $this->params[$key] = $value;
 83     }
 84 
 85     /**
 86      * Sets the specific params for the next extension.
 87      *
 88      * @param array $params
 89      */
 90     public function setExtensionParams(array $params)
 91     {
 92         $this->extensionParams = $params;
 93     }
 94 
 95     /**
 96      * Returns whether the given param exists.
 97      *
 98      * @param string $key
 99      *
100      * @return bool
101      */
102     public function hasParam($key)
103     {
104         return isset($this->params[$key]) || isset($this->extensionParams[$key]);
105     }
106 
107     /**
108      * Returns the param for the given key.
109      *
110      * @param string $key
111      * @param mixed  $default
112      *
113      * @return mixed
114      */
115     public function getParam($key, $default = null)
116     {
117         if (isset($this->extensionParams[$key])) {
118             return $this->extensionParams[$key];
119         }
120         if (isset($this->params[$key])) {
121             return $this->params[$key];
122         }
123         return $default;
124     }
125 
126     /**
127      * Returns all params.
128      *
129      * @return array
130      */
131     public function getParams()
132     {
133         return array_merge($this->params, $this->extensionParams);
134     }
135 
136     /**
137      * Returns whether the extension point is readonly.
138      *
139      * @return bool
140      */
141     public function isReadonly()
142     {
143         return $this->readonly;
144     }
145 }
146