Passed
Push — master ( 7822c6...0f91e9 )
by Sebastian
03:25
created

QueryParam   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getToken() 0 3 1
A getValue() 0 5 1
A getName() 0 5 1
A splitString() 0 7 1
A setValue() 0 4 1
1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Traits\Commands\Validation\QueryParamsTrait\QueryParam}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\QueryParamsTrait\QueryParam
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation\QueryParamsTrait;
13
14
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
15
16
/**
17
 * Handles a single query parameter, by modifying the
18
 * matching tokenizer token directly. This ensures that
19
 * any changes are directly applied to the tokens.
20
 *
21
 * @package Mailcode
22
 * @subpackage Validation
23
 * @author Sebastian Mordziol <[email protected]>
24
 */
25
class QueryParam
26
{
27
    private Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $token;
28
29
    public function __construct(Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $token)
30
    {
31
        $this->token = $token;
32
    }
33
34
    /**
35
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
36
     */
37
    public function getToken() : Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
38
    {
39
        return $this->token;
40
    }
41
42
    public function getName() : string
43
    {
44
        $split = $this->splitString();
45
46
        return $split['name'];
47
    }
48
49
    public function getValue() : string
50
    {
51
        $split = $this->splitString();
52
53
        return $split['value'];
54
    }
55
56
    public function setValue(string $value) : self
57
    {
58
        $this->token->setText($this->getName().'='.$value);
59
        return $this;
60
    }
61
62
    /**
63
     * @return array{name:string,value:string}
64
     */
65
    private function splitString() : array
66
    {
67
        $tokens = explode('=', $this->token->getText());
68
69
        return array(
70
            'name' => trim(array_shift($tokens)),
71
            'value' => trim(implode('=', $tokens))
72
        );
73
    }
74
}
75