HttpRequest::getBodyData()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 19
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 13
nc 5
nop 1
crap 56
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix;
14
15
use Apix\Request,
16
    Apix\Input\InputInterface,
17
    Apix\Input\Xml,
18
    Apix\Input\Json;
19
20
class HttpRequest extends Request
21
{
22
    /**
23
     * List of supported input formats.
24
     * @var array
25
     */
26
    protected $formats = array('post', 'json', 'xml');
27
28
    /**
29
     * Returns the format from an HTTP context.
30
     *
31
     * @param  string $context The context to extract the format from.
32
     * @return string The extracted format.
33
     */
34
    public static function getFormat($context)
35
    {
36
        switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strstr($context, '/html') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing strstr($context, '/json') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
37
            // text/html
38
            case (strstr($context, '/html')):
39
                return 'html';
40
41
            // application/json
42
            case (strstr($context, '/json')):
43
                return 'json';
44
45
            // text/xml, application/xml
46
            case (strstr($context, '/xml')
47
                && (!strstr($context, 'html'))):
48
                return 'xml';
49
50
            default:
51
                return null;
52
        }
53
    }
54
55
    /**
56
     * Returns the format from an HTTP Accept.
57
     *
58
     * @return string The output format
59
     */
60
    public function getAcceptFormat()
61
    {
62
        if ($this->hasHeader('HTTP_ACCEPT')) {
63
            $accept = $this->getHeader('HTTP_ACCEPT');
64
65
            if (!$format = self::getFormat($accept)) {
66
                // 'application/javascript'
67
                $format = strstr($accept, '/javascript') ? 'jsonp' : null;
68
            }
69
        }
70
71
        return isset($format) ? $format : false;
72
    }
73
74
    /**
75
     * Get & parse the request body-data.
76
     *
77
     * @param  boolean $assoc Wether to convert objects to associative arrays or not.
78
     * @return array
79
     * @link http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
80
     * @link http://www.w3.org/TR/html401/references.html#ref-RFC2388
81
     */
82
    public function getBodyData($assoc=true)
83
    {
84
        if ($this->hasBody() && $this->hasHeader('CONTENT_TYPE')) {
85
            $ctx = $this->getHeader('CONTENT_TYPE');
86
87
            if (
88
                in_array('post', $this->formats)
89
                // application/x-www-form-urlencoded
90
                && strstr($ctx, '/x-www-form-urlencoded')
91
                // TODO: shall we do multipart/form-data too?
92
            ) {
93
                // handles by PHP already. No need to parse the body.
94
                return $this->getParams();
95
            } elseif ($format = self::getFormat($ctx)) {
96
                if (in_array(strtolower($format), $this->formats)) {
97
                    $class = __NAMESPACE__ . '\Input\\' . ucfirst($format);
98
                    $input = new $class();
99
100
                    return $input->decode($this->getBody(), $assoc);
101
                    #$this->setParams($r); // TODO: maybe set as request params?
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
102
                }
103
            }
104
105
            return null;
106
        }
107
    }
108
109
    /**
110
     * Sets the input formats.
111
     *
112
     * @return void
113
     */
114
    public function setFormats(array $formats)
115
    {
116
        $this->formats = $formats;
117
    }
118
119
}
120