Completed
Push — master ( ab502e...07a33d )
by Zaahid
06:14
created

ParameterHeader::getValueFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Header;
8
9
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
10
use ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer;
11
use ZBateson\MailMimeParser\Header\Part\ParameterPart;
12
13
/**
14
 * Represents a header containing a primary value part and subsequent name/value
15
 * parts using a ParameterConsumer.
16
 * 
17
 * @author Zaahid Bateson
18
 */
19
class ParameterHeader extends AbstractHeader
20
{
21
    /**
22
     * @var \ZBateson\MailMimeParser\Header\Part\ParameterPart[] key map of
23
     * lower-case parameter names and associated ParameterParts.
24
     */
25
    protected $parameters = [];
26
    
27
    /**
28
     * Returns a ParameterConsumer.
29
     * 
30
     * @param ConsumerService $consumerService
31
     * @return \ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer
32
     */
33 4
    protected function getConsumer(ConsumerService $consumerService)
34
    {
35 4
        return $consumerService->getParameterConsumer();
36
    }
37
    
38
    /**
39
     * Overridden to assign ParameterParts to a map of lower-case parameter
40
     * names to ParameterParts.
41
     * 
42
     * @param AbstractConsumer $consumer
43
     */
44 4
    protected function setParseHeaderValue(AbstractConsumer $consumer)
45
    {
46 4
        parent::setParseHeaderValue($consumer);
47 4
        foreach ($this->parts as $part) {
48 4
            if ($part instanceof ParameterPart) {
49 4
                $this->parameters[strtolower($part->getName())] = $part;
50 4
            }
51 4
        }
52 4
    }
53
    
54
    /**
55
     * Returns true if a parameter exists with the passed name.
56
     * 
57
     * @param string $name
58
     * @return boolean
59
     */
60 3
    public function hasParameter($name)
61
    {
62 3
        return isset($this->parameters[strtolower($name)]);
63
    }
64
    
65
    /**
66
     * Returns the value of the parameter with the given name, or $defaultValue
67
     * if not set.
68
     * 
69
     * @param string $name
70
     * @param string $defaultValue
71
     * @return string
72
     */
73 3
    public function getValueFor($name, $defaultValue = null)
74
    {
75 3
        if (!$this->hasParameter($name)) {
76 1
            return $defaultValue;
77
        }
78 2
        return $this->parameters[strtolower($name)]->getValue();
79
    }
80
}
81