AddressFinderTrait::findByName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Slince/China package.
4
 *
5
 * (c) Slince <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace China\Region;
12
13
use China\Region\Location\AddressInterface;
14
15
trait AddressFinderTrait
16
{
17
    /**
18
     * 根据Code查找地址
19
     *
20
     * @param string $code
21
     *
22
     * @return AddressInterface|false
23
     */
24
    public function findByCode($code)
25
    {
26
        return $this->filter(function(AddressInterface $address) use ($code){
27
            return $address->getCode() == $code;
28
        })->first();
29
    }
30
31
    /**
32
     * 根据Name查找地址
33
     *
34
     * @param string $name
35
     *
36
     * @return AddressInterface|false
37
     */
38
    public function findByName($name)
39
    {
40
        return $this->filter(function(AddressInterface $address) use ($name){
41
            return $address->getName() == $name;
42
        })->first();
43
    }
44
45
    /**
46
     * 筛选.
47
     *
48
     * @param \Closure $callback
49
     *
50
     * @return mixed
51
     */
52
    abstract public function filter(\Closure $callback);
53
}