initial commit
[namibia] / module / Workspace / src / Workspace / Service / Workflow.php
1 <?php
2 namespace Workspace\Service;
3
4
5
6 /**
7  * Basic data node to provide global functionality to workflow services.
8  * @author andre.fourie
9  */
10 class Workflow
11 {
12         /**
13          * Contract types
14          */
15         const CONTRACT_TYPE_RECCURING   = 'Recurring';
16         const CONTRACT_TYPE_USE_ONCE    = 'UseOnce';
17         
18         /**
19          * Field types
20          */
21         const FIELD_TYPE_DEFAULT                        = 'Default';
22         const FIELD_TYPE_ARRAY                          = 'Array';
23         const FIELD_TYPE_DATE                           = 'Date';
24         const FIELD_TYPE_EMAIL                          = 'Email';
25         const FIELD_TYPE_MOBILE                         = 'Mobile';
26         const FIELD_TYPE_ID                                     = 'Id';
27         const FIELD_TYPE_BOOLEAN                        = 'Boolean';
28         const FIELD_TYPE_DEPARTMENT                     = 'Department';
29         const FIELD_TYPE_DEALER_TYPE            = 'DealerType';
30         const FIELD_TYPE_DEALER_CLIENT_TYPE     = 'DealerClientType';
31         const FIELD_TYPE_COMPANY_TYPE           = 'CompanyType';
32         const FIELD_TYPE_POSITION                       = 'Position';
33         const FIELD_TYPE_USERTYPE                       = 'UserType';
34         const FIELD_TYPE_ID_NUMBER_ZA           = 'ZaId';
35         const FIELD_TYPE_USERNAME                       = 'Username';
36         const FIELD_TYPE_PASSWORD                       = 'Password';
37         const FIELD_TYPE_ID_NUMBER                      = 'IdNumber';
38         const FIELD_TYPE_NAME                           = 'Name';
39         const FIELD_TYPE_PERSON_NAME            = 'PersonName';
40         const FIELD_TYPE_MONTH_DIGIT            = 'MonthDigit';
41         const FIELD_TYPE_COLLECTION                     = 'Collection';
42         
43         const FIELD_TYPE_DECIMAL                        = 'Decimal';
44         const FIELD_TYPE_INTEGER                        = 'Integer';
45         const FIELD_TYPE_DIGIT_3                        = 'Digit3';
46         const FIELD_TYPE_DIGIT_4                        = 'Digit4';
47         const FIELD_TYPE_DIGIT_10                       = 'Digit10';
48         
49         const FIELD_TYPE_STRING_2                       = 'String2';
50         const FIELD_TYPE_STRING_10                      = 'String10';
51         const FIELD_TYPE_STRING_12                      = 'String12';
52         const FIELD_TYPE_STRING_15                      = 'String15';
53         const FIELD_TYPE_STRING_20                      = 'String20';
54         const FIELD_TYPE_STRING_25                      = 'String25';
55         const FIELD_TYPE_STRING_40                      = 'String40';
56         const FIELD_TYPE_STRING_50                      = 'String50';
57         const FIELD_TYPE_STRING_75                      = 'String75';
58         const FIELD_TYPE_STRING_100                     = 'String100';
59         const FIELD_TYPE_STRING_250                     = 'String250';
60         const FIELD_TYPE_STRING_500                     = 'String500';
61         
62         /**
63          * @var \Workspace\Workflow
64          */
65         protected $workflowNode;
66         /**
67          * @var \Doctrine\ORM\EntityManager
68          */
69         protected $em;
70         /**
71          * @var array
72          */
73         protected $meta;
74
75
76         /**
77          * Standard workflow.
78          * @param \Workspace\Workflow $workflowNode
79          */
80         public function setWorkflow(\Workspace\Workflow $workflowNode)
81         {
82                 $this->workflowNode = $workflowNode;
83                 $this->em = $this->workflowNode->getEntityManager();
84         }
85
86
87         /**
88          * Utility to construct options object from meta data.
89          * @param array $meta
90          * @return \Workspace\UseCase\Options
91          */
92         protected function buildOptions(array $meta){
93                 $options = new \Workspace\UseCase\Options();
94                 if (isset($meta['Options']) && !empty($meta['Options']))
95                 {
96                         if (isset($meta['Options']['Boolean']) && !empty($meta['Options']['Boolean']))
97                         {
98                                 foreach ($meta['Options']['Boolean'] as $option => $default)
99                                 {
100                                         $options->optBoolean($option, $default);
101                                 }
102                         }
103                         isset($meta['Options']['OneFromList'])
104                         && !empty($meta['Options']['OneFromList'])
105                         && $options->optOneFromList(
106                                         $meta['Options']['OneFromList']['Name'],
107                                         $meta['Options']['OneFromList']['List'],
108                                         $meta['Options']['OneFromList']['Default']
109                         );
110                         isset($meta['Options']['ManyFromList'])
111                         && !empty($meta['Options']['ManyFromList'])
112                         && $options->optManyFromList(
113                                         $meta['Options']['ManyFromList']['Name'],
114                                         $meta['Options']['ManyFromList']['List'],
115                                         $meta['Options']['ManyFromList']['Default']
116                         );
117                 }
118                 return $options;
119         }
120
121         /**
122          * Utility to construct requirements object from meta data.
123          * @param array $meta
124          * @return \Workspace\UseCase\Requirement
125          */
126         protected function buildRequirements(array $meta){
127                 $requirement = new \Workspace\UseCase\Requirement();
128                 isset($meta['RequiredInput'])
129                         && !empty($meta['RequiredInput'])
130                         && $requirement->addRequiredInput($meta['RequiredInput']);
131                 isset($meta['OptionalInput'])
132                         && !empty($meta['OptionalInput'])
133                         && $requirement->addOptionalInput($meta['OptionalInput']);
134                 return $requirement;
135         }
136
137         /**
138          * Utility to construct conditional case object(s) from meta data and attach to contract.
139          * @param \Workspace\Contract\AbstractBase $contract
140          * @param array $meta
141          * @return \Workspace\Contract\AbstractBase
142          */
143         protected function buildConditions(\Workspace\Contract\AbstractBase $contract, array $meta)
144         {
145                 if (isset($meta['Conditions']) && !empty ($meta['Conditions']))
146                 {
147                         foreach ($meta['Conditions'] as $name => $conditionMeta)
148                         {
149                                 $condition = (isset($conditionMeta['Base']) && 'Or' == $conditionMeta['Base'])
150                                         ? new \Workspace\UseCase\ConditionOrBase()
151                                         : new \Workspace\UseCase\ConditionAndBase();
152                                 foreach ($conditionMeta['Condition'] as $newCondition)
153                                 {
154                                         isset($newCondition['Type'])
155                                                 || $newCondition['Type'] = 'equal';
156                                         switch ($newCondition['Type'])
157                                         {
158                                                 case 'notEqual':
159                                                         $condition->notEqual($newCondition['Field'], $newCondition['Value']);
160                                                         break;
161                                                 case 'smallerThan':
162                                                         $condition->smallerThan($newCondition['Field'], $newCondition['Value']);
163                                                         break;
164                                                 case 'greaterThan':
165                                                         $condition->greaterThan($newCondition['Field'], $newCondition['Value']);
166                                                         break;
167                                                 case 'oneOf':
168                                                         $condition->oneOf($newCondition['Field'], $newCondition['Value']);
169                                                         break;
170                                                 case 'notOneOf':
171                                                         $condition->notOneOf($newCondition['Field'], $newCondition['Value']);
172                                                         break;
173                                                 case 'equal':
174                                                 default:
175                                                         $condition->equal($newCondition['Field'], $newCondition['Value']);
176                                                         break;
177                                         }
178                                 }
179                                 $requirement = new \Workspace\UseCase\Requirement();
180                                 $requirement->addRequiredInput($conditionMeta['RequiredInput']);
181                                 isset($conditionMeta['OptionalInput'])
182                                         && !empty($conditionMeta['OptionalInput'])
183                                         && $requirement->addOptionalInput($conditionMeta['OptionalInput']);
184                                 $contract->addConditionalCase($name, $condition, $requirement);
185                         }
186                 }
187                 return $contract;
188         }
189
190 }