Completed
Push — master ( ce9662...2c1559 )
by Marco
15:00
created

Service::methodsToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
1
<?php namespace Comodojo\Dispatcher\Service;
2
3
use \Comodojo\Exception\DispatcherException;
4
use \Comodojo\Exception\IOException;
5
use \Comodojo\Dispatcher\Serialization;
6
use \Comodojo\Dispatcher\Deserialization;
7
8
/**
9
 * The Service base class, feel free to extend
10
 *
11
 * @package     Comodojo dispatcher
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     GPL-3.0+
14
 *
15
 * LICENSE:
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 */
30
31
abstract class Service {
32
33
    //###### Things a service should define ######//
34
35
    /**
36
     * The content type that service will return.
37
     * Default content is "text/plain"; each service can override,
38
     * both in setup phase or in method declaration.
39
     *
40
     * @var     string
41
     */
42
    private $content_type = "text/plain";
43
44
    /**
45
     * St status code that service will return (in case of no exceptions).
46
     * Default content is 200 - OK; each service can override,
47
     * both in setup phase or in method declaration.
48
     *
49
     * @var     integer
50
     */
51
    private $status_code = 200;
52
53
    /**
54
     * An array of headers that will be returned. This value is initially populated
55
     * with headers declared in routing phase (if any).
56
     *
57
     * @var     array
58
     */
59
    private $headers = array();
60
61
    /**
62
     * Supported HTTP methods (comma separated, not spaced)
63
     *
64
     * @var     string
65
     * @see     dispatcher-config.php
66
     */
67
    private $supported_http_methods = DISPATCHER_SUPPORTED_METHODS;
68
69
    /**
70
     * Result charset
71
     *
72
     * @var     string
73
     * @see     dispatcher-config.php
74
     */
75
    private $charset = DISPATCHER_DEFAULT_ENCODING;
76
77
    //###### Things a service could use for free ######//
78
79
    /**
80
     * Request attributes, populated at runtime by dispatcher
81
     *
82
     * @var     array
83
     */
84
    private $attributes = array();
85
86
    /**
87
     * Request parameters, populated at runtime by dispatcher
88
     *
89
     * @var     array
90
     */
91
    private $parameters = array();
92
93
    /**
94
     * Request raw parameters (php://input), populated at runtime by dispatcher
95
     *
96
     * @var     array
97
     */
98
    private $raw_parameters = array();
99
100
    /**
101
     * Request headers, injected by dispatcher
102
     *
103
     * @var array
104
     */
105
    private $request_headers = array();
106
107
    /**
108
     * Logger, injected by dispatcher
109
     *
110
     * @var Object
111
     */
112
    private $logger = null;
113
114
    /**
115
     * Cacher, injected by dispatcher
116
     *
117
     * @var Object
118
     */
119
    private $cacher = null;
120
121
    /**
122
     * An instance of dispatcher serializer
123
     *
124
     * @var     Object
125
     */
126
    public $serialize = null;
127
128
    /**
129
     * An instance of dispatcher deserializer
130
     *
131
     * @var     Object
132
     */
133
    public $deserialize = null;
134
135
    //###### Things a service may define ######//
136
137
    // Expected attributes
138
    private $expected_attributes = null;
139
140
    // Expected parameters
141
    private $expected_parameters = null;
142
143
    // Liked attributes
144
    private $liked_attributes = null;
145
146
    // Liked parameters
147
    private $liked_parameters = null;
148
149
    //###### Things dedicated to internal use ######//
150
151
    /**
152
     * Supported success code (as defined in ObjectSuccess)
153
     *
154
     * @var     array
155
     */
156
    private $supported_success_codes = array(200,202,204);
157
158
    /*************** HTTP METHODS IMPLEMENTATIONS **************/
159
160
    /**
161
     * Implement this method if your service should support
162
     * HTTP-GET requests
163
     */
164
    //public function get() {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
165
166
    /**
167
     * Implement this method if your service should support
168
     * HTTP-POST requests
169
     */
170
    // public function post() {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
171
172
    /**
173
     * Implement this method if your service should support
174
     * HTTP-PUT requests
175
     */
176
    // public function put() {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
177
178
    /**
179
     * Implement this method if your service should support
180
     * HTTP-DELETE requests
181
     */
182
    // public function delete() {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
183
184
    /**
185
     * Implement this method if your service should support
186
     * any HTTP requests (it's quite a wildcard, please be careful...)
187
     */
188
    // public function any() {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
189
190
    /*************** HTTP METHODS IMPLEMENTATIONS **************/
191
192
    /******************* OVERRIDABLE METHODS *******************/
193
194
    /**
195
     * Service setup.
196
     *
197
     * It is the first method that dispatcher will call and could be used
198
     * to define service parameters in global scope, such as contentType or
199
     * success HTTP code.
200
     *
201
     * PLEASE REMEMBER: setting same parameters in method declaration will
202
     * override first ones.
203
     *
204
     * @return null
205
     */
206
    public function setup() { }
207
208
    /**
209
     * Service constructor.
210
     *
211
     * Currently, only for init serialized/deserialized but can be extended to
212
     * do anything service needs.
213
     *
214
     * PLEASE REMEMBER to call parent::__construct() at the end of your method
215
     *
216
     * @return null
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
217
     */
218
    public function __construct($logger, $cacher) {
219
220
        $this->logger = $logger;
221
222
        $this->cacher = $cacher;
223
224
        $methods_to_array = $this->methodsToArray();
225
226
        $this->expected_attributes = $methods_to_array;
227
228
        $this->expected_parameters = $methods_to_array;
229
230
        $this->liked_attributes = $methods_to_array;
231
232
        $this->liked_parameters = $methods_to_array;
233
234
        $this->serialize = new Serialization();
235
236
        $this->deserialize = new Deserialization();
237
238
    }
239
240
    /******************* OVERRIDABLE METHODS *******************/
241
242
    /**
243
     * Expected attributes (i.e. ones that will build the URI)
244
     *
245
     * @param   string  $method     HTTP method for punctual attributes rematch or ANY
246
     * @param   array   $attributes array og attributes that service expects
247
     * @param   array   $parameters array of parameters that service expects
248
     *
249
     * @return  Object  $this
250
     */
251
    final public function expects($method, $attributes, $parameters=array()) {
252
253
        $method = strtoupper($method);
254
255
        $this->expected_attributes[$method] = $attributes;
256
        $this->expected_parameters[$method] = $parameters;
257
258
        return $this;
259
260
    }
261
262
    /**
263
     * Liked (optional) attributes
264
     *
265
     * @param   string  $method     HTTP method for punctual attributes rematch or ANY
266
     * @param   array   $attributes array og attributes that service likes
267
     * @param   array   $parameters array of parameters that service likes
268
     *
269
     * @return  Object  $this
270
     */
271
    final public function likes($method, $attributes, $parameters=array()) {
272
273
        $method = strtoupper($method);
274
275
        $this->liked_attributes[$method] = $attributes;
276
        $this->liked_parameters[$method] = $parameters;
277
278
        return $this;
279
280
    }
281
282
    /**
283
     * Set methods service will support.
284
     *
285
     * In can be misleading, but supported HTTP methods and implemented HTTP methods
286
     * are not the same thing.
287
     *
288
     * - If method is not SUPPORTED, service will not be initiated and a 405 - Not Allowed
289
     *   error will be returned.
290
     * - If method is not IMPLEMENTED - i.e. get() method is not defined - service will not
291
     *   be initiated and a 501 - Not Implemented error will be returned
292
     *
293
     * @param   string  $methods     HTTP methods, comma separated
294
     *
295
     * @return  Object  $this
296
     */
297
    final public function setSupportedMethods($methods) {
298
299
        $methods = preg_replace('/\s+/', '', $methods);
300
        $methods = explode(",", $methods);
301
302
        $supported_methods = array();
303
304
        foreach ($methods as $method) {
305
306
            array_push($supported_methods, strtoupper($method));
307
308
        }
309
310
        $this->supported_http_methods = implode(",", $supported_methods);
311
312
        return $this;
313
314
    }
315
316
    /**
317
     * Set service content type
318
     *
319
     * @param   string  $type   Content Type
320
     * @return  Object  $this
321
     */
322
    final public function setContentType($type) {
323
324
        $this->content_type = $type;
325
326
        return $this;
327
328
    }
329
330
    /**
331
     * Get service declared content type
332
     *
333
     * @return  string  Content Type
334
     */
335
    final public function getContentType() {
336
337
        return $this->content_type;
338
339
    }
340
341
    /**
342
     * Set service charset
343
     *
344
     * @param   string  $charset   Charset
345
     *
346
     * @return  Object  $this
347
     */
348
    final public function setCharset($charset) {
349
350
        $this->charset = $charset;
351
352
        return $this;
353
354
    }
355
356
    /**
357
     * Get service declared charset
358
     *
359
     * @return  string  Charset
360
     */
361
    final public function getCharset() {
362
363
        return $this->charset;
364
365
    }
366
367
    /**
368
     * Set success status code
369
     *
370
     * @param   integer $code   HTTP status code (in case of success)
371
     * @return  Object  $this
372
     */
373 View Code Duplication
    final public function setStatusCode($code) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
374
375
        $code = filter_var($code, FILTER_VALIDATE_INT);
376
377
        $this->status_code = in_array($code, $this->supported_success_codes) ? $code : $this->status_code;
378
379
        return $this;
380
381
    }
382
383
    /**
384
     * Get service-defined status code
385
     *
386
     * @return  integer     HTTP status code (in case of success)
387
     */
388
    final public function getStatusCode() {
389
390
        return $this->status_code;
391
392
    }
393
394
    /**
395
     * Get dispatcher logger
396
     *
397
     * @return  Object
398
     */
399
    final public function getLogger() {
400
401
        return $this->logger;
402
403
    }
404
405
    /**
406
     * Get dispatcher cacher
407
     *
408
     * @return  Object
409
     */
410
    final public function getCacher() {
411
412
        return $this->cacher;
413
414
    }
415
416
    /**
417
     * Set header component
418
     *
419
     * @param   string  $header     Header name
420
     * @param   string  $value      Header content (optional)
421
     *
422
     * @return  Object  $this
423
     */
424
    final public function setHeader($header, $value=null) {
425
426
        $this->headers[$header] = $value;
427
428
        return $this;
429
430
    }
431
432
    /**
433
     * Unset header component
434
     *
435
     * @param   string  $header     Header name
436
     *
437
     * @return  bool
438
     */
439 View Code Duplication
    final public function unsetHeader($header) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
440
441
        if ( isset($this->headers[$header]) ) {
442
443
            unset($this->headers[$header]);
444
445
            return true;
446
447
        }
448
449
        return false;
450
451
    }
452
453
    /**
454
     * Get header component
455
     *
456
     * @param   string  $header     Header name
457
     *
458
     * @return  string  Header component in case of success, false otherwise
459
     */
460
    final public function getHeader($header) {
461
462
        if ( isset($this->headers[$header]) ) return $this->headers[$header];
463
464
        return null;
465
466
    }
467
468
    /**
469
     * Set headers
470
     *
471
     * @param   array   $headers    Headers array
472
     *
473
     * @return  Object  $this
474
     */
475
    final public function setHeaders($headers) {
476
477
        $this->headers = is_array($headers) ? $headers : $this->headers;
478
479
        return $this;
480
481
    }
482
483
    /**
484
     * Unset headers
485
     *
486
     * @return  Object  $this
487
     */
488
    final public function unsetHeaders() {
489
490
        $this->headers = array();
491
492
        return $this;
493
494
    }
495
496
    /**
497
     * Get headers
498
     *
499
     * @return  array   Headers array
500
     */
501
    final public function getHeaders() {
502
503
        return $this->headers;
504
505
    }
506
507
    /**
508
     * Get service-supported HTTP methods
509
     *
510
     * @return  array   Headers array
511
     */
512
    final public function getSupportedMethods() {
513
514
        return $this->supported_http_methods;
515
516
    }
517
518
    /**
519
     * Get service-implemented HTTP methods
520
     *
521
     * @return  array   Headers array
522
     */
523
    final public function getImplementedMethods() {
524
525
        if ( method_exists($this, 'any') ) return explode(",",$this->supported_http_methods);
526
527
        $supported_methods = explode(',',$this->supported_http_methods);
528
529
        $implemented_methods = array();
530
531
        foreach ( $supported_methods as $method ) {
532
533
            if ( method_exists($this, strtolower($method)) ) array_push($implemented_methods,$method);
534
535
        }
536
537
        return $implemented_methods;
538
539
    }
540
541
    /**
542
     * Return the callable class method that reflect the requested one
543
     *
544
     * @return  array   Headers array
545
     */
546
    final public function getCallableMethod($method) {
547
548
        if ( method_exists($this, strtolower($method)) ) return strtolower($method);
549
550
        else return "any";
551
552
    }
553
554
    /**
555
     * Get attributes and parameters that service expects
556
     *
557
     * @return  array
558
     */
559 View Code Duplication
    final public function getExpected($method) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
560
561
        $method = strtoupper($method);
562
563
        return array(
564
565
            ( sizeof($this->expected_attributes[$method]) == 0 AND sizeof($this->expected_attributes["ANY"]) != 0 ) ? $this->expected_attributes["ANY"] : $this->expected_attributes[$method],
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
566
567
            ( sizeof($this->expected_parameters[$method]) == 0 AND sizeof($this->expected_parameters["ANY"]) != 0 ) ? $this->expected_parameters["ANY"] : $this->expected_parameters[$method]
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
568
569
        );
570
571
    }
572
573
    /**
574
     * Get attributes and parameters that service likes
575
     *
576
     * @return  array
577
     */
578 View Code Duplication
    final public function getLiked($method) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
579
580
        $method = strtoupper($method);
581
582
        return array(
583
584
            ( sizeof($this->liked_attributes[$method]) == 0 AND sizeof($this->liked_attributes["ANY"]) != 0 ) ? $this->liked_attributes["ANY"] : $this->liked_attributes[$method],
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
585
586
            ( sizeof($this->liked_parameters[$method]) == 0 AND sizeof($this->liked_parameters["ANY"]) != 0 ) ? $this->liked_parameters["ANY"] : $this->liked_parameters[$method]
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
587
588
            );
589
590
    }
591
592
    /**
593
     * Set request attributes (used by dispatcher)
594
     *
595
     */
596
    final public function setAttributes($attributes) {
597
598
        $this->attributes = $attributes;
599
600
    }
601
602
    /**
603
     * Get request attributes, populated by dispatcher
604
     *
605
     * @return  array
606
     */
607
    final public function getAttributes() {
608
609
        return $this->attributes;
610
611
    }
612
613
    /**
614
     * Get request attribute, populated by dispatcher
615
     *
616
     * @param   string  $attribute  Attribute to search for
617
     *
618
     * @return  mixed
619
     */
620
    final public function getAttribute($attribute) {
621
622
        if ( isset($this->attributes[$attribute]) ) return $this->attributes[$attribute];
623
624
        return null;
625
626
    }
627
628
    /**
629
     * Set raw parameters  (used by dispatcher)
630
     *
631
     */
632
    final public function setRawParameters($raw_parameters) {
633
634
        $this->raw_parameters = $raw_parameters;
635
636
    }
637
638
    /**
639
     * Set request parameters (used by dispatcher)
640
     *
641
     */
642
    final public function setParameters($parameters) {
643
644
        $this->parameters = $parameters;
645
646
    }
647
648
    /**
649
     * Get request parameters, populated by dispatcher
650
     *
651
     * @return  array
652
     */
653
    final public function getParameters($raw=false) {
654
655
        return $raw ? $this->raw_parameters : $this->parameters;
656
657
    }
658
659
    /**
660
     * Get request parameter, populated by dispatcher
661
     *
662
     * @param   string  $parameter  Parameter to search for
663
     *
664
     * @return  array
665
     */
666
    final public function getParameter($parameter) {
667
668
        if ( isset($this->parameters[$parameter]) ) return $this->parameters[$parameter];
669
670
        return null;
671
672
    }
673
674
    /**
675
     * Get request parameters, populated by dispatcher
676
     *
677
     */
678
    final public function setRequestHeaders($headers) {
679
680
        $this->request_headers = is_array($headers) ? $headers : array();
681
682
    }
683
684
    /**
685
     * Get request header component
686
     *
687
     * @param   string  $header     Header to search for
688
     *
689
     * @return  string              Header component in case of success, null otherwise
690
     */
691
    final public function getRequestHeader($header) {
692
693
        if ( isset($this->request_headers[$header]) ) return $this->request_headers[$header];
694
695
        return null;
696
697
    }
698
    /**
699
     * Get headers
700
     *
701
     * @return  array   Headers array
702
     */
703
    final public function getRequestHeaders() {
704
705
        return $this->request_headers;
706
707
    }
708
709
    private function methodsToArray() {
710
711
        $methods = explode(",",$this->supported_http_methods);
712
713
        $methods_array = array();
714
715
        foreach ($methods as $method) $methods_array[$method] = array();
716
717
        $methods_array['ANY'] = array();
718
719
        return $methods_array;
720
721
    }
722
723
}
724