initial commit
[namibia] / module / Workspace / src / Workspace / Utility / ServiceInput.php
1 <?php
2 namespace Workspace\Utility;
3
4
5
6 /**
7  * Build a useful parameter object to be used by service nodes.
8  * @author andre.fourie
9  */
10 class ServiceInput
11 {
12
13         /**
14          * @var \Workspace\Utility\Param
15          */
16         protected $obj;
17
18
19         /**
20          * Initilize.
21          * @param array $fromArray
22          * @param array $booleanIfIsset
23          * @return \Workspace\Utility\ServiceInput
24          */
25         public function __construct($type = 'ParamSet', array $fromArray = array(), array $booleanIfIsset = array())
26         {
27                 $this->obj = 'ParamSet' == $type
28                         ? new Param()
29                         : new ServiceInputParams();
30                 if (!empty($fromArray))
31                 {
32                         $this->fromArray($fromArray);
33                 }
34                 if (!empty($booleanIfIsset))
35                 {
36                         $this->booleanIfIsset($booleanIfIsset);
37                 }
38         }
39
40
41         /**
42          * Add parameters from an array.
43          * @param array $data
44          * @return \Workspace\Utility\ServiceInput
45          */
46         public function fromArray(array $data)
47         {
48                 foreach ($data as $key => $item)
49                 {
50                         $this->obj->$key = $item;
51                 }
52                 return $this;
53         }
54
55         /**
56          * Add bolean parameters from an array.
57          * @param array|string $data
58          * @return \Workspace\Utility\ServiceInput
59          */
60         public function booleanIfIsset($data)
61         {
62                 is_array($data)
63                         || $data = array($data => true);
64                 foreach ($data as $key => $item)
65                 {
66                         $this->obj->$key = true;
67                 }
68                 return $this;
69         }
70
71         /**
72          * Pack parameter set and return parameter object.
73          * @return \Workspace\Utility\Param
74          */
75         public function pack()
76         {
77                 return $this->obj;
78         }
79
80 }
81
82
83
84 /**
85  * Service input parameter utility to keep options.
86  * @author andre.fourie
87  */
88 class ServiceInputParams
89 {
90
91         /**
92          * If value is not set, return false by default.
93          * @param string $name
94          * @return boolean
95          */
96         public function __get($name)
97         {
98                 return false;
99         }
100
101         public function success($message = '', array $data = array())
102         {
103                 return array(
104                                 'RequestId' => $this->requestId,
105                                 'Hash'      => $this->hash,
106                                 'Status'    => 'Success',
107                                 'Message'   => $message,
108                                 'Data'      => $data
109                 );
110         }
111
112         public function error($reason = '', $message = '', array $data = array())
113         {
114                 return array(
115                                 'RequestId'    => $this->requestId,
116                                 'Hash'         => $this->hash,
117                                 'Status'       => 'Error',
118                                 'StatusReason' => $reason,
119                                 'Message'      => $message,
120                                 'Data'         => $data
121                 );
122         }
123
124         public function deviceSuccess($message = '', array $data = array())
125         {
126                 return array(
127                                 'Meta' => array(
128                                                 'Status'    => 'Success',
129                                                 'Message'   => $message
130                                 ),
131                                 'Data' => $data
132                 );
133         }
134
135         public function deviceError($reason = '', $message = '', array $data = array())
136         {
137                 return array(
138                                 'Meta' => array(
139                                                 'Status'       => 'Error',
140                                                 'StatusReason' => $reason,
141                                                 'Message'      => $message
142                                 ),
143                                 'Data' => $data
144                 );
145         }
146
147 }
148
149