Completed
Push — master ( 5decb7...aee104 )
by Thibaud
14:37 queued 11:26
created

QueryFacet::getValues()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 4
nc 4
nop 0
crap 3
1
<?php
2
3
namespace PhraseanetSDK\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use PhraseanetSDK\Annotation\ApiField as ApiField;
7
use PhraseanetSDK\Annotation\ApiRelation as ApiRelation;
8
9
class QueryFacet
10
{
11
    /**
12
     * @param \stdClass[] $values
13
     * @return QueryFacet[]
14
     */
15
    public static function fromList(array $values)
16
    {
17
        $facets = array();
18
19
        foreach ($values as $value) {
20
            $facets[] = self::fromValue($value);
21
        }
22
23
        return $facets;
24
    }
25
26
    /**
27
     * @param \stdClass $value
28
     * @return QueryFacet
29
     */
30
    public static function fromValue(\stdClass $value)
31
    {
32
        return new self($value);
33
    }
34
35
    /**
36
     * @var \stdClass
37
     */
38
    protected $source;
39
40
    /**
41
     * @var QueryFacetValue[]|ArrayCollection
42
     */
43
    protected $values;
44
45
    /**
46
     * @param \stdClass $source
47
     */
48 3
    public function __construct(\stdClass $source = null)
49
    {
50 3
        $this->source = $source ?: new \stdClass();
51 3
    }
52
53
    /**
54
     * @return \stdClass
55
     */
56
    public function getRawData()
57
    {
58
        return $this->source;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 2
    public function getName()
65
    {
66 2
        return isset($this->source->name) ? $this->source->name : '';
67
    }
68
69
    /**
70
     * @return ArrayCollection|QueryFacetValue[]
71
     */
72 3
    public function getValues()
73
    {
74 3
        if (! isset($this->source->values)) {
75 1
            $this->values = new ArrayCollection();
76 1
        }
77
78 3
        return $this->values ?: $this->values = new ArrayCollection(QueryFacetValue::fromList($this->source->values));
79
    }
80
81
    /**
82
     * @param ArrayCollection|QueryFacetValue[] $values
83
     */
84 1
    public function setValues($values)
85
    {
86 1
        if (is_array($values)) {
87
            $values = new ArrayCollection($values);
88
        }
89
90 1
        $this->values = $values;
91 1
    }
92
}
93