EntityCollection::firstByAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the slince/smartqq 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 Slince\SmartQQ;
12
13
use Cake\Collection\Collection;
14
15
class EntityCollection extends Collection
16
{
17
    /**
18
     * 根据实体属性筛选指定一个实体.
19
     *
20
     * @param string $attributeName
21
     * @param mixed  $attributeValue
22
     *
23
     * @return object|null
24
     */
25
    public function firstByAttribute($attributeName, $attributeValue)
26
    {
27
        $callback = function ($entity) use ($attributeName, $attributeValue) {
28
            $method = 'get'.ucfirst($attributeName);
29
30
            return $entity->$method() == $attributeValue;
31
        };
32
33
        return  $this->filter($callback)->first();
34
    }
35
}
36