Skip to content

Commit 5921656

Browse files
committed
Merge pull request #3 from GoIntegro/feature/add-collection
Add collection of the same entities
2 parents 4602614 + 8fd9b5f commit 5921656

2 files changed

Lines changed: 150 additions & 1 deletion

File tree

Application/Content/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(array $record)
1919

2020
public function getType()
2121
{
22-
return $this->record['type'];
22+
return isset($this->record['type']) ? $this->record['type'] : $this->record[0]['type'];
2323
}
2424

2525
public function toArray()

Application/Model/Collection.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace GoIntegro\Bundle\EndPointBundle\Application\Model;
4+
5+
use Iterator;
6+
7+
class Collection implements Iterator, ApiEntity
8+
{
9+
/**
10+
* @var ApiEntity[]
11+
*/
12+
private $entities = [];
13+
14+
public function addEntity(ApiEntity $apiEntity)
15+
{
16+
if (in_array($apiEntity, $this->entities, true)) {
17+
return;
18+
}
19+
20+
$this->entities[] = $apiEntity;
21+
}
22+
23+
public function removeEntity(ApiEntity $apiEntity)
24+
{
25+
$this->entities = array_udiff(
26+
$this->entities,
27+
array($apiEntity), function($a, $b) {
28+
return ($a === $b) ? 0 : 1;
29+
}
30+
);
31+
}
32+
33+
/**
34+
* return the type of the resource
35+
*
36+
* @return string
37+
*/
38+
public function getResourceType()
39+
{
40+
if (!isset($this->entities[0])) {
41+
return null;
42+
}
43+
44+
return $this->entities[0]->getResourceType();
45+
}
46+
47+
/**
48+
* return the id
49+
*
50+
* @return int
51+
*/
52+
public function getId()
53+
{
54+
$ids = [];
55+
foreach ($this->entities as $entity) {
56+
$ids[] = $entity->getId();
57+
}
58+
59+
return $ids;
60+
}
61+
62+
/**
63+
* return all the attributes that has the entity
64+
*
65+
* @param array $fields
66+
* @return array
67+
*/
68+
public function getData(array $fields)
69+
{
70+
$data = [];
71+
foreach ($this->entities as $entity) {
72+
$data[] = $entity->getData($fields);
73+
}
74+
75+
return $data;
76+
}
77+
78+
/**
79+
* return which type of relationship is
80+
*
81+
* @param $entity
82+
* @return int|null
83+
*/
84+
public function getRelationshipsMapperType($entity)
85+
{
86+
$mapper = [];
87+
foreach ($this->entities as $ent) {
88+
$mapper[] = $ent->getRelationshipsMapperType($entity);
89+
}
90+
91+
return $mapper;
92+
}
93+
94+
/**
95+
* (PHP 5 &gt;= 5.0.0)<br/>
96+
* Return the current element
97+
* @link http://php.net/manual/en/iterator.current.php
98+
* @return mixed Can return any type.
99+
*/
100+
public function current()
101+
{
102+
return current($this->entities);
103+
}
104+
105+
/**
106+
* (PHP 5 &gt;= 5.0.0)<br/>
107+
* Move forward to next element
108+
* @link http://php.net/manual/en/iterator.next.php
109+
* @return void Any returned value is ignored.
110+
*/
111+
public function next()
112+
{
113+
next($this->entities);
114+
}
115+
116+
/**
117+
* (PHP 5 &gt;= 5.0.0)<br/>
118+
* Return the key of the current element
119+
* @link http://php.net/manual/en/iterator.key.php
120+
* @return mixed scalar on success, or null on failure.
121+
*/
122+
public function key()
123+
{
124+
return key($this->entities);
125+
}
126+
127+
/**
128+
* (PHP 5 &gt;= 5.0.0)<br/>
129+
* Checks if current position is valid
130+
* @link http://php.net/manual/en/iterator.valid.php
131+
* @return boolean The return value will be casted to boolean and then evaluated.
132+
* Returns true on success or false on failure.
133+
*/
134+
public function valid()
135+
{
136+
return key($this->entities) !== null;
137+
}
138+
139+
/**
140+
* (PHP 5 &gt;= 5.0.0)<br/>
141+
* Rewind the Iterator to the first element
142+
* @link http://php.net/manual/en/iterator.rewind.php
143+
* @return void Any returned value is ignored.
144+
*/
145+
public function rewind()
146+
{
147+
reset($this->entities);
148+
}
149+
}

0 commit comments

Comments
 (0)