-
Notifications
You must be signed in to change notification settings - Fork 0
/
dioncoffeemaker.php
291 lines (198 loc) · 5.88 KB
/
dioncoffeemaker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/*
*
*
DionCoffeeMaker
===============
Our poor coffee maker :(
We make it to brew some coffee in the office.
It was originally developed by a ASP sympathizer PHP developer. So, it works but god knows whats wrong with it!
So we are looking PHP developer to fix our coffee maker script and also join our team.
On every sip of coffee we drink, we are wishing for a better coffee maker.
Fork it, send your comments and if you are in Istanbul and looking for a job as PHP developer, contact us.
Regards
Bora Yalçın - Dion Adworks
www.dionadworks.com
info@dionadworks.com
*
*
*/
abstract class Coffee {
//final coffee
public $coffee;
//bool
public $milk;
//coffee type
public $coffee_type;
//milk type to use
public $milk_type;
//number of sugar cubes to use;
public $sugar;
//surup to use with coffee
public $surup;
}
interface CoffeeBean {
public function Make_The_Coffee();
}
class DionCoffeeMaker extends Coffee implements CoffeeBean {
//water temperature for coffee in celcius
const WATER_TEMPERATURE = 94;
public $errors = array();
protected $valid_arguments = array(
'coffee_type' => array('type'=>self::TYPE_STRING,'control_function'=>'coffee_type_check'),
'milk' => array('type'=>self::TYPE_BOOL,'control_function'=>''),
'milk_type' => array('type'=>self::TYPE_STRING,'control_function'=>'milk_type_check'),
'sugar' => array('type'=>self::TYPE_INTEGER,'control_function'=>''),
'surup' => array('type'=>self::TYPE_STRING,'control_function'=>'surup_type_check'),
);
protected $default_args = array(
'coffee_type' => 'dark',
'milk' => false,
"milk_type" => "regular",
'sugar' => 0,
'surup' => 'none',
);
const TYPE_STRING = 'string';
const TYPE_BOOL = 'bool';
const TYPE_INTEGER = 'integer';
function __construct( $args = array() ){
$this->preperations($args);
$this->Make_The_Coffee();
}
function preperations($args){
//check for arg types
$args = $this->validateArgTypes($args);
//use control function
foreach ($args as $k => $v)
if( isset($this->valid_arguments[$k]['control_function']) && !empty($this->valid_arguments[$k]['control_function'])){
$function = $this->valid_arguments[$k]['control_function'];
if(!$this->$function($v)){
unset($args[$k]);
}
}
$args = array_merge($this->default_args,$args);
foreach ($args as $argkey => $deger) {
//class variables
$this->$argkey = $deger;
}
}
function validateArgTypes( $args = array()){
//return if empty, don't even bother
if(empty($args))
{
return $args;
}
$valid_arg_keys = array_keys( $this->valid_arguments );
//this will be returned
$args2 = array();
foreach ($args as $argkey => $arg) {
if(in_array($argkey,$valid_arg_keys)){
$t = $this->valid_arguments[$argkey]['type'];
if( self::TYPE_STRING == $t ) {
if( is_string( $arg['type'] ) ) {
$args2[$argkey] = $arg;
}
}else if( self::TYPE_BOOL == $t ) {
if( is_bool( $arg ) ) {
$args2[$argkey] = $arg;
}
}
if( self::TYPE_INTEGER == $t ) {
//try typecasting
$arg = (int)$arg;
if( is_int( $arg ) ) {
$args2[$argkey] = $arg;
}
}
}
}
return $args2;
}
function milk_type_check($type){
$valid_milk_types = array('regular','non-fat','low-fat','soymilk');
if(in_array( $type,$valid_milk_types )){
return true;
}
array_push($this->errors,'Milk Type: '.$type.'<br/>- Neither our cows nor our soy plant gives this kind of milk :(');
return false;
}
function coffee_type_check($type) {
$valid_coffee_types = array('dark','blonde','medium');
if(in_array( $type,$valid_coffee_types ))
return true;
array_push($this->errors,'Coffee Type: '.$type.'<br/>- What kind of a coffee is that!');
return false;
}
function surup_type_check($type) {
$valid_coffee_types = array('none','vanilla','caramel','almond','hazelnut');
if(in_array( $type,$valid_coffee_types )){
return true;
}
array_push($this->errors,'surup Type: '.$type.'<br/>- Sorry! Just run out of that one! Really! Come next week, thanks!');
return false;
}
function errorHandler($echo = true){
if(count($this->errors) > 0) {
if($echo){
echo '<div style="font-weight:bold;">';
echo '<h2 style="color:crimson">Sorry! But I got some errors!</h2>';
foreach ($this->errors as $key => $error)
echo '<p>'.$error."</p>";
echo '<div>';
}
//yes it has errors, dont make the coffee
return true;
}
return false;
}
function boil_water()
{
$initialwater_temp = $this->check_initial_water_temp();
if($initialwater_temp < self::WATER_TEMPERATURE){
while($initialwater_temp < self::WATER_TEMPERATURE){
$initialwater_temp++;
}
return true;
}
}
function check_initial_water_temp(){
return rand(1,10);
}
function Make_The_Coffee(){
//it has errors! stop!
if($this->errorHandler(false)){
return $this->errorHandler();
}
//get ingrediants
$coffee_type = $this->coffee_type;
$milk = $this->milk;
$milk_type = $this->milk_type;
$sugar = $this->sugar;
$surup = $this->surup;
//boil the water
if( $this->boil_water() ) {
$this->coffee .= '<h2>Your coffee is ready! It\'s a ';
$this->coffee .= $coffee_type.' coffee ';
if($milk){
$this->coffee .= 'with '.$milk_type.' milk, ';
}else{
//no milk
$this->coffee .= 'without milk, ';
}
if($sugar > 0){
$this->coffee .= $sugar.' sugar added ';
}else{
$this->coffee .= 'no sugar added ';
}
if('none' == $surup){
$this->coffee .= 'and without any surups. ';
}else{
$this->coffee .= 'and flavored with '.$surup.' surup. ';
}
$this->coffee .= 'Enjoy it!<h2>';
}
echo $this->coffee;
return;
}
}
$my_morning_coffee = new DionCoffeeMaker(array('surup' => 'almond'));