Completed
Push — master ( e83455...d2af9f )
by Andrii
06:07 queued 04:16
created

ChargeType::ensureValidValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge\modifiers\addons;
12
13
use hiqdev\php\billing\charge\modifiers\AddonInterface;
14
15
/**
16
 * Charge type addon
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class ChargeType implements AddonInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $value;
26
27
    public function __construct($value)
28
    {
29
        $this->value = $this->ensureValidValue($value);
30
    }
31
32
    public function getValue()
33
    {
34
        return $this->value;
35
    }
36
37
    private function ensureValidValue($value): string
38
    {
39
        if (!preg_match('/^[\w_]+$/', $value)) {
40
            throw new \InvalidArgumentException('Charge type is not valid');
41
        }
42
43
        return (string) $value;
44
    }
45
}
46