BindingParameter::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the puli/discovery package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Discovery\Api\Type;
13
14
use RuntimeException;
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * A parameter that can be set during binding.
19
 *
20
 * @since  1.0
21
 *
22
 * @author Bernhard Schussek <[email protected]>
23
 */
24
final class BindingParameter
25
{
26
    /**
27
     * Flag: The parameter is optional.
28
     */
29
    const OPTIONAL = 0;
30
31
    /**
32
     * Flag: The parameter is required.
33
     */
34
    const REQUIRED = 1;
35
36
    /**
37
     * @var string
38
     */
39
    private $name;
40
41
    /**
42
     * @var int
43
     */
44
    private $flags;
45
46
    /**
47
     * @var mixed
48
     */
49
    private $defaultValue;
50
51
    /**
52
     * Creates a new parameter.
53
     *
54
     * @param string $name         The parameter name.
55
     * @param int    $flags        A bitwise combination of the flag constants
56
     *                             in this class.
57
     * @param mixed  $defaultValue The parameter's default value.
58
     */
59 57
    public function __construct($name, $flags = self::OPTIONAL, $defaultValue = null)
60
    {
61 57
        Assert::stringNotEmpty($name, 'The parameter name must be a non-empty string. Got: %s');
62 55
        Assert::startsWithLetter($name, 'The parameter name must start with a letter. Got: %s');
63 53
        Assert::nullOrInteger($flags, 'The parameter "$flags" must be an integer or null. Got: %s');
64
65 53
        if (($flags & self::REQUIRED) && null !== $defaultValue) {
66 1
            throw new RuntimeException('Required parameters must not have default values.');
67
        }
68
69 52
        $this->name = $name;
70 52
        $this->flags = $flags;
71 52
        $this->defaultValue = $defaultValue;
72 52
    }
73
74
    /**
75
     * Returns the name of the parameter.
76
     *
77
     * @return string The parameter name.
78
     */
79 49
    public function getName()
80
    {
81 49
        return $this->name;
82
    }
83
84
    /**
85
     * Returns the flags passed to the constructor.
86
     *
87
     * @return int A bitwise combination of the flag constants in this class.
88
     */
89 19
    public function getFlags()
90
    {
91 19
        return $this->flags;
92
    }
93
94
    /**
95
     * Returns the default value of the parameter.
96
     *
97
     * @return mixed The default value.
98
     */
99 32
    public function getDefaultValue()
100
    {
101 32
        return $this->defaultValue;
102
    }
103
104
    /**
105
     * Returns whether the parameter is required.
106
     *
107
     * @return bool Returns `true` if the parameter is required and `false`
108
     *              otherwise.
109
     */
110 39
    public function isRequired()
111
    {
112 39
        return (bool) ($this->flags & self::REQUIRED);
113
    }
114
}
115