Passed
Push — master ( ecf04c...355586 )
by Vincent
08:10
created

ArrayOffsetHttpFields   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 9
c 1
b 0
f 0
dl 0
loc 52
ccs 13
cts 13
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 3 2
A format() 0 3 1
A contains() 0 3 1
A extract() 0 7 3
1
<?php
2
3
namespace Bdf\Form\Child\Http;
4
5
/**
6
 * Extract HTTP fields value using a simple array offset
7
 * This is the default http fields implementation
8
 *
9
 * <code>
10
 * $fields = new ArrayOffsetHttpFields('child');
11
 *
12
 * $fields->extract(['child' => 'value'], 'not found'); // => 'value'
13
 * $fields->extract(['other' => 'value'], 'not found'); // => 'not found'
14
 * </code>
15
 */
16
final class ArrayOffsetHttpFields implements HttpFieldsInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $offset;
22
23
24
    /**
25
     * ArrayOffsetHttpFields constructor.
26
     *
27
     * @param string $offset The field name
28
     */
29 315
    public function __construct(string $offset)
30
    {
31 315
        $this->offset = $offset;
32 315
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 114
    public function extract($httpFields)
38
    {
39 114
        if (!is_array($httpFields) || !isset($httpFields[$this->offset])) {
40 29
            return null;
41
        }
42
43 90
        return $httpFields[$this->offset];
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 11
    public function contains($httpFields): bool
50
    {
51 11
        return isset($httpFields[$this->offset]);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 7
    public function format($value)
58
    {
59 7
        return [$this->offset => $value];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 52
    public function get(?HttpFieldPath $path = null): HttpFieldPath
66
    {
67 52
        return $path === null ? HttpFieldPath::named($this->offset) : $path->add($this->offset);
68
    }
69
}
70