text changes to registration mail content
[namibia] / module / Workspace / src / Workspace / UseCase / Options.php
1 <?php
2 namespace Workspace\UseCase;
3
4
5
6 /**
7  * Contract execution options.
8  * @author andre.fourie
9  */
10 class Options
11 {
12
13         /**
14          * @var array
15          */
16         protected $options;
17         /**
18          * @var array
19          */
20         protected $optionData;
21
22
23         /**
24          * Initialize options.
25          * @param array $packagedOptions
26          * @return \Workspace\UseCase\Options
27          */
28         public function __construct(array $packagedOptions = array(), array $optionData = array())
29         {
30                 $this->options = array();
31                 $this->optionData = array();
32                 if (!empty($packagedOptions))
33                 {
34                         $this->hydrate($packagedOptions);
35                 }
36                 if (!empty($optionData))
37                 {
38                         $this->setOptions($optionData);
39                 }
40         }
41
42         /**
43          * Boolean option, accepts 0/1, true/false, yes/no.
44          * @param string $optionName
45          * @return \Workspace\UseCase\Options
46          */
47         public function optBoolean($optionName, $default = true)
48         {
49                 $this->options[$optionName] = array(
50                                 'type'    => 'boolean',
51                                 'default' => $default
52                 );
53                 return $this;
54         }
55
56         /**
57          * Option allowing selection of single item from list.
58          * @param string $optionName
59          * @param array $optionList
60          * @param integer $default
61          * @return \Workspace\UseCase\Options
62          */
63         public function optOneFromList($optionName, array $optionList, $default = null)
64         {
65                 $this->options[$optionName] = array(
66                                 'type'    => 'select-one',
67                                 'list'    => $optionList,
68                                 'default' => $default
69                 );
70                 return $this;
71         }
72
73         /**
74          * Option allowing selection of multiple items from list.
75          * @param string $optionName
76          * @param array $optionList
77          * @param array $defaults
78          * @return \Workspace\UseCase\Options
79          */
80         public function optManyFromList($optionName, array $optionList, array $defaults = array())
81         {
82                 $this->options[$optionName] = array(
83                                 'type'    => 'select-many',
84                                 'list'    => $optionList,
85                                 'default' => $defaults
86                 );
87                 return $this;
88         }
89
90         /**
91          * Package options into an array.
92          * @return array
93          */
94         public function package()
95         {
96                 return $this->options;
97         }
98
99         /**
100          * Hydrate options from package.
101          * @param array $packagedOptions
102          * @return \Workspace\Contract\Options
103          */
104         public function hydrate(array $packagedOptions)
105         {
106                 $this->options = $packagedOptions;
107                 return $this;
108         }
109
110         /**
111          * Set option inputs.
112          * @param array $optionData
113          * @return \Workspace\UseCase\Options
114          */
115         public function setOptions(array $optionData)
116         {
117                 foreach ($this->options as $option => $spec)
118                 {
119                         if (!isset($optionData[$option]))
120                         {
121                                 $this->optionData[$option] = $spec['default'];
122                                 continue;
123                         }
124                         switch ($this->options[$option]['type'])
125                         {
126                                 case 'boolean':
127                                         $this->optionData[$option] = (
128                                                         1 == $optionData[$option]
129                                                         || true === $optionData[$option]
130                                                         || 'true' == $optionData[$option]
131                                                         || 'yes' == $optionData[$option]
132                                                         )
133                                                 ? true
134                                                 : false;
135                                         break;
136                                 case 'select-one':
137                                         $this->optionData[$option] = isset($spec['list'][$optionData[$option]])
138                                                 ? $optionData[$option]
139                                                 : $spec['default'];
140                                         break;
141                                 case 'select-many':
142                                         if (is_array($optionData[$option]))
143                                         {
144                                                 $this->optionData[$option] = array();
145                                                 foreach ($optionData[$option] as $opt)
146                                                 {
147                                                         isset($spec['list'][$optionData[$option]])
148                                                                 && $this->optionData[$option][$opt] = $opt;
149                                                 }
150                                         }
151                                         else
152                                         {
153                                                 $this->optionData[$option] = $spec['default'];
154                                         }
155                                         break;
156                         }
157                 }
158                 return $this;
159         }
160
161         /**
162          * Retrieve filtered options.
163          * @return array
164          */
165         public function getOptions()
166         {
167                 return $this->optionData;
168         }
169
170 }