Completed
Pull Request — develop (#38)
by A.
03:48 queued 01:19
created

ContactPersonList   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 61
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A initializeWith() 0 4 1
A filter() 0 11 1
A getFirstContactPerson() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\Profile\Value;
20
21
use ArrayIterator;
22
use Countable;
23
use IteratorAggregate;
24
25
final class ContactPersonList implements IteratorAggregate, Countable
26
{
27
    /**
28
     * @var ContactPerson[]
29
     */
30
    private $contactPersons;
31
32
    public function __construct(array $contactPersons)
33
    {
34
        foreach ($contactPersons as $contactPerson) {
35
            $this->initializeWith($contactPerson);
36
        }
37
    }
38
39
    /**
40
     * @param ContactPerson $contactPerson
41
     */
42
    private function initializeWith(ContactPerson $contactPerson)
43
    {
44
        $this->contactPersons[] = $contactPerson;
45
    }
46
47
    /**
48
     * @param callable $predicate
49
     * @return ContactPersonList
50
     */
51
    public function filter(callable $predicate)
52
    {
53
        return new ContactPersonList(
54
            array_filter(
55
                $this->contactPersons,
56
                function (ContactPerson $contactPerson) use ($predicate) {
57
                    return $predicate($contactPerson);
58
                }
59
            )
60
        );
61
    }
62
63
    /**
64
     * @return ContactPerson
65
     */
66
    public function getFirstContactPerson()
67
    {
68
        return $this->contactPersons[0];
69
    }
70
71
    public function getIterator()
72
    {
73
        return new ArrayIterator($this->contactPersons);
74
    }
75
76
    public function count()
77
    {
78
        return count($this->contactPersons);
79
    }
80
81
    public function __toString()
82
    {
83
        return implode(', ', $this->contactPersons);
84
    }
85
}
86