CountableCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 48
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttributeName() 0 4 1
A __construct() 0 6 1
A getAttributeName() 0 4 1
A toArray() 0 18 3
1
<?php
2
3
/*
4
 * This file is part of gpupo/search
5
 *
6
 * (c) Gilmar Pupo <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gpupo\Search\Result;
13
14
/**
15
 * {@inheritDoc}. Collection de Contagens na busca Facetada
16
 */
17
class CountableCollection extends CollectionAbstract
18
{
19
    /**
20
     * Nome do atributo que recebeu a contagem.
21
     *
22
     * @type string
23
     */
24
    protected $attributeName;
25
26
    public function setAttributeName($name)
27
    {
28
        $this->attributeName = $name;
29
    }
30
    public function __construct(array $array, $attributeName)
31
    {
32
        $this->setAttributeName($attributeName);
33
34
        parent::__construct($array);
35
    }
36
    /**
37
     * Nome do atributo que recebeu a contagem.
38
     *
39
     * @return string
40
     */
41
    public function getAttributeName()
42
    {
43
        return $this->attributeName;
44
    }
45
46
    public function toArray()
47
    {
48
        $array = [];
49
50
        foreach (parent::toArray() as $item) {
51
            $name = $item->find($this->getAttributeName());
52
53
            if (!empty($name)) {
54
                $array[] = [
55
                    'field' => $this->getAttributeName(),
56
                    'name'  => $name,
57
                    'value' => $item->find('@count'),
58
                ];
59
            }
60
        }
61
62
        return $array;
63
    }
64
}
65