HeaderFactoryTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 55
rs 10
c 1
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A getHeaderName() 0 14 2
A fromString() 0 10 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http\Header;
12
13
use ReflectionProperty;
14
15
/**
16
 * @author      Iqbal Maulana <[email protected]>
17
 * @created     8/5/15
18
 */
19
trait HeaderFactoryTrait
20
{
21
    /**
22
     * Factory create header from string.
23
     *
24
     * @param string $headerLine
25
     *
26
     * @return $this
27
     */
28
    public static function fromString($headerLine)
29
    {
30
        if (null === $headerName = static::getHeaderName()) {
31
            GenericHeader::assertEmptyHeaderFieldName($headerName);
32
        }
33
34
        list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine);
35
36
        return static::create($fieldName, $fieldValue);
37
    }
38
39
    /**
40
     * Factory create header.
41
     *
42
     * @param string $fieldName
43
     * @param string $fieldValue
44
     *
45
     * @return $this
46
     */
47
    public static function create($fieldName, $fieldValue)
48
    {
49
        GenericHeader::assertHeaderFieldName(static::getHeaderName(), $fieldName);
50
51
        return new static($fieldValue);
52
    }
53
54
    /**
55
     * Header name accessor.
56
     *
57
     * @return string|null
58
     */
59
    protected static function getHeaderName()
60
    {
61
        $class = get_called_class();
62
        $property = 'headerName';
63
64
        if (false === property_exists($class, $property)) {
65
            return null;
66
        }
67
68
        $reflection = new ReflectionProperty($class, $property);
69
        $reflection->setAccessible(true);
70
71
        return $reflection->getValue();
72
    }
73
}
74