Completed
Pull Request — develop (#9)
by Daan van
09:54 queued 05:17
created

ShibbolethMetadataScope::serialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 4
nop 0
1
<?php
2
3
namespace OpenConext\Value\Saml\Metadata;
4
5
use OpenConext\Value\Assert\Assertion;
6
use OpenConext\Value\RegularExpression;
7
use OpenConext\Value\Serializable;
8
9
final class ShibbolethMetadataScope implements Serializable
10
{
11
    /**
12
     * @var string|null
13
     */
14
    private $literal;
15
16
    /**
17
     * @var RegularExpression|null
18
     */
19
    private $regexp;
20
21
    /**
22
     * @param string $literal
23
     * @return ShibbolethMetadataScope
24
     */
25
    public static function literal($literal)
26
    {
27
        Assertion::nonEmptyString($literal, 'literal');
28
29
        $scope          = new ShibbolethMetadataScope();
30
        $scope->literal = $literal;
31
32
        return $scope;
33
    }
34
35
    /**
36
     * @param string $regexp
37
     * @return ShibbolethMetadataScope
38
     */
39
    public static function regexp($regexp)
40
    {
41
        Assertion::nonEmptyString($regexp, 'regexp');
42
43
        $scope         = new ShibbolethMetadataScope();
44
        $scope->regexp = new RegularExpression($regexp);
45
46
        return $scope;
47
    }
48
49
    private function __construct()
50
    {
51
    }
52
53
    /**
54
     * @param string $string
55
     * @return bool
56
     */
57
    public function allows($string)
58
    {
59
        Assertion::string($string, 'Scope to check should be a string, "%s" given');
60
61
        if ($this->literal !== null) {
62
            return $this->literal === $string;
63
        }
64
65
        return $this->regexp->matches($string);
66
    }
67
68
    /**
69
     * @param ShibbolethMetadataScope $other
70
     * @return bool
71
     */
72
    public function equals(ShibbolethMetadataScope $other)
73
    {
74
        return ($this->literal !== null && $this->literal === $other->literal)
75
                || ($this->regexp && $other->regexp && $this->regexp->equals($other->regexp));
76
    }
77
78
    public static function deserialize($data)
79
    {
80
        Assertion::isArray($data);
81
        Assertion::keysExist($data, array('type', 'scope'));
82
        Assertion::choice($data['type'], array('literal', 'regexp'));
83
84
        $type = $data['type'];
85
86
        return self::$type($data['scope']);
87
    }
88
89
    public function serialize()
90
    {
91
        return array(
92
            'type' => ($this->literal !== null ? 'literal' : 'regexp'),
93
            'scope' => ($this->literal ?: $this->regexp->serialize())
94
        );
95
    }
96
97
    public function __toString()
98
    {
99
        if ($this->literal !== null) {
100
            return sprintf('ShibbolethMetadataScope(literal=%s)', $this->literal);
101
        } else {
102
            return sprintf('ShibbolethMetadataScope(regexp=%s)', $this->regexp);
103
        }
104
    }
105
}
106