Completed
Push — master ( 8b1d39...e6cddd )
by Jakob
01:31
created

Set::contains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 2
cc 2
eloc 2
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace JSKOS;
4
5
use InvalidArgumentException;
6
7
/**
8
 * A JSKOS Set as defined by JSKOS specification.
9
 *
10
 * A Set is a possibly empty array with all members being JSKOS Resources
11
 * with distinct values in field `uri` (if given), expect the last member
12
 * optionally being null.
13
 */
14
class Set extends Container
15
{
16
    /**
17
     * Check whether an equal member alredy exists in this Set.
18
     */
19
    public function contains($member): bool
20
    {
21
        return $member->uri ? !!$this->findURI($member->uri) : false;
22
    }
23
24
    /**
25
     * Return the offset of a member Resource with given URI, if it exists.
26
     */
27
    public function findURI(string $uri)
28
    {
29
        foreach ($this->members as $member) {
30
            if ($member->uri == $uri) {
31
                return $member;
32
            }
33
        }
34
    }
35
36
    /**
37
     * Return whether this set does not contain same resources.
38
     */
39
    public function isValid()
40
    {
41
        $uris = [];
42
        foreach ($this->members as $member) {
43
            $uri = $member->uri;
44
            if ($uri) {
45
                if (isset($uris[$uri])) {
46
                    return false;
47
                } else {
48
                    $uris[$uri] = true;
49
                }
50
            }
51
        }
52
        return true;
53
    }
54
}
55