Visibility::visibility()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Leaditin\Code;
4
5
use InvalidArgumentException;
6
7
/**
8
 * @package Leaditin\Code
9
 * @author Igor Vuckovic <[email protected]>
10
 * @license MIT
11
 */
12
class Visibility
13
{
14
    public const VISIBILITY_PUBLIC = 'public';
15
    public const VISIBILITY_PROTECTED = 'protected';
16
    public const VISIBILITY_PRIVATE = 'private';
17
18
    /**
19
     * @var string
20
     */
21
    protected $visibility;
22
23
    /**
24
     *
25
     * @param string $visibility
26
     */
27 8
    public function __construct(string $visibility)
28
    {
29 8
        $this->validate($visibility);
30 6
        $this->visibility = strtolower($visibility);
31 6
    }
32
33
    /**
34
     * @param Flag $flag
35
     *
36
     * @return Visibility
37
     */
38 3
    public static function fromFlag(Flag $flag): Visibility
39
    {
40 3
        if ($flag->hasFlag(Flag::FLAG_PRIVATE)) {
41 1
            return new Visibility(static::VISIBILITY_PRIVATE);
42
        }
43
44 2
        if ($flag->hasFlag(Flag::FLAG_PROTECTED)) {
45 1
            return new Visibility(static::VISIBILITY_PROTECTED);
46
        }
47
48 1
        return new Visibility(static::VISIBILITY_PUBLIC);
49
    }
50
51
    /**
52
     * @return string
53
     */
54 6
    public function visibility(): string
55
    {
56 6
        return $this->visibility;
57
    }
58
59
    /**
60
     * @param string $visibility
61
     *
62
     * @throws InvalidArgumentException
63
     */
64 8
    protected function validate(string $visibility): void
65
    {
66 8
        if (!in_array($visibility, [self::VISIBILITY_PUBLIC, self::VISIBILITY_PROTECTED, self::VISIBILITY_PRIVATE], true)) {
67 2
            throw new InvalidArgumentException("Visibility '$visibility' is not valid visibility case.");
68
        }
69 6
    }
70
}
71