Completed
Push — master ( 874d6a...5decb7 )
by Thibaud
10s
created

QueryFacet::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
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 2
    public function getRawData()
57
    {
58 2
        return $this->source;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 3
    public function getName()
65
    {
66 3
        return isset($this->source->name) ? $this->source->name : '';
67 1
    }
68 1
69
    /**
70 3
     * @return ArrayCollection|QueryFacetValue[]
71
     */
72
    public function getValues()
73
    {
74
        if (! isset($this->source->values)) {
75
            $this->values = new ArrayCollection();
76 1
        }
77
78 1
        return $this->values ?: $this->values = new ArrayCollection(QueryFacetValue::fromList($this->source->values));
79
    }
80
81
    /**
82 1
     * @param ArrayCollection|QueryFacetValue[] $values
83 1
     */
84
    public function setValues($values)
85
    {
86
        if (is_array($values)) {
87
            $values = new ArrayCollection($values);
88
        }
89
90
        $this->values = $values;
91
    }
92
}
93