Completed
Push — master ( 315c47...bac1eb )
by Patrick
04:00 queued 02:13
created

ContactCollection::toAPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Date: 20/08/15
4
 */
5
6
namespace Mailxpert\Model;
7
8
/**
9
 * Class ContactCollection
10
 * @package Mailxpert\Model
11
 */
12
class ContactCollection extends ArrayCollection
13
{
14
    /**
15
     * @param mixed $offset
16
     *
17
     * @return Contact|null
18
     */
19
    public function offsetGet($offset)
20
    {
21
        return parent::offsetGet($offset);
22
    }
23
24
    /**
25
     * @param Contact $contact
26
     *
27
     * @return void
28
     */
29
    public function add($contact)
30
    {
31
        if (!$this->exists(
32
            function ($key, Contact $element) use ($contact) {
33
                return $contact->getEmail() == $element->getEmail();
34
            }
35
        )
36
        ) {
37
            parent::add($contact);
38
        }
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function toAPI()
45
    {
46
        return array_map(function(Contact $contact) {
47
            return $contact->toAPI();
48
        }, $this->getValues());
49
    }
50
51
    /**
52
     * @param string $email
53
     *
54
     * @return Contact|null
55
     */
56
    public function findByEmail($email)
57
    {
58
        $contacts = $this->filter(function (Contact $element) use ($email) {
59
            return $element->getEmail() == strtolower($email);
60
        });
61
62
        return $contacts->first();
63
    }
64
}
65