Completed
Pull Request — master (#72)
by Thibaud
08:29
created

Result::getRecords()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[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 PhraseanetSDK\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use PhraseanetSDK\Annotation\ApiField as ApiField;
16
use PhraseanetSDK\Annotation\ApiRelation as ApiRelation;
17
use PhraseanetSDK\EntityManager;
18
19
class Result
20
{
21
    /**
22
     * @param EntityManager $entityManager
23
     * @param \stdClass[] $values
24
     * @return Result[]
25
     */
26
    public static function fromList(EntityManager $entityManager, array $values)
27
    {
28
        $results = array();
29
30
        foreach ($values as $value) {
31
            $results[] = self::fromValue($entityManager, $value);
32
        }
33
34
        return $results;
35
    }
36
37
    /**
38
     * @param EntityManager $entityManager
39
     * @param \stdClass $value
40
     * @return Result
41
     */
42 3
    public static function fromValue(EntityManager $entityManager, \stdClass $value)
43
    {
44 3
        return new self($entityManager, $value);
45
    }
46
47
    /**
48
     * @var EntityManager
49
     */
50
    protected $entityManager;
51
52
    /**
53
     * @var \stdClass
54
     */
55
    protected $source;
56
57
    /**
58
     * @var ArrayCollection|Record[]
59
     */
60
    protected $records;
61
62
    /**
63
     * @var ArrayCollection|Story[]
64
     */
65
    protected $stories;
66
67
    /**
68
     * @param EntityManager $entityManager
69
     * @param \stdClass $source
70
     */
71 3
    public function __construct(EntityManager $entityManager, \stdClass $source)
72
    {
73 3
        $this->entityManager = $entityManager;
74 3
        $this->source = $source;
75 3
    }
76
    /**
77
     * @return \stdClass
78
     */
79
    public function getRawData()
80
    {
81
        return $this->source;
82
    }
83
84
    /**
85
     * @return ArrayCollection|Record[]
86
     */
87
    public function getRecords()
88 1
    {
89
        return $this->records ?: $this->records = new ArrayCollection(Record::fromList($this->source->records));
90 1
    }
91 1
92 1
    /**
93 1
     * @return ArrayCollection|Story[]
94
     */
95
    public function getStories()
96
    {
97
        return $this->stories ?: $this->stories = new ArrayCollection(Story::fromList(
98
            $this->entityManager,
99
            $this->source->stories
100
        ));
101
    }
102
}
103