Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 5687380

Browse files
author
Jens Schulze
authored
fix(Collection): fix iterator for unset elements of collections (#307)
1 parent a1cfc99 commit 5687380

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

src/Model/Common/Collection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class Collection extends AbstractJsonDeserializeObject implements \Iterator, \Js
1414
{
1515
const ID = 'id';
1616
use ContextTrait;
17+
const COLLECTION_TYPE = Collection::TYPE_LIST;
18+
const TYPE_LIST = 'List';
19+
const TYPE_MAP = 'Map';
1720

1821
/**
1922
* @var string
@@ -367,5 +370,20 @@ public function offsetUnset($offset)
367370
{
368371
unset($this->rawData[$offset]);
369372
unset($this->typeData[$offset]);
373+
$rawKeys = array_keys($this->rawData);
374+
$typeKeys = array_keys($this->typeData);
375+
$keys = array_merge($rawKeys, $typeKeys);
376+
$this->keys = array_unique($keys);
377+
}
378+
379+
protected function toJson()
380+
{
381+
$data = parent::toJson();
382+
383+
if (static::COLLECTION_TYPE == self::TYPE_LIST) {
384+
return array_values($data);
385+
}
386+
387+
return $data;
370388
}
371389
}

src/Model/Product/FacetResultCollection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
class FacetResultCollection extends Collection
1717
{
1818
const OFFSET = 'offset';
19+
const COLLECTION_TYPE = Collection::TYPE_MAP;
1920

2021
protected $type = FacetResult::class;
2122

src/Model/Product/LocalizedSearchKeywords.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
class LocalizedSearchKeywords extends Collection
2020
{
21+
const COLLECTION_TYPE = Collection::TYPE_MAP;
22+
2123
protected $type = SearchKeywords::class;
2224

2325
/**

src/Model/Product/LocalizedSuggestionCollection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
class LocalizedSuggestionCollection extends Collection
2121
{
22+
const COLLECTION_TYPE = Collection::TYPE_MAP;
2223
protected $type = SuggestionCollection::class;
2324

2425
/**

tests/unit/Model/Common/CollectionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,17 @@ public function testAdd()
235235

236236
$this->assertCount(2, $collection);
237237
}
238+
239+
public function testUnsetIterator()
240+
{
241+
$collection = Collection::fromArray([1]);
242+
$collection[] = 2;
243+
unset($collection[0]);
244+
245+
$this->assertCount(1, $collection);
246+
$collection->rewind();
247+
$this->assertSame(1, $collection->key());
248+
$this->assertSame(2, $collection->current());
249+
$this->assertJsonStringEqualsJsonString('[2]', json_encode($collection));
250+
}
238251
}

tests/unit/Model/Product/LocalizedSearchKeywordsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,25 @@ public function testFromArray()
9595
$this->assertSame('Hello World', $keywords->en->getAt(0)->getText());
9696
$this->assertSame('custom', $keywords->de->getAt(1)->getSuggestTokenizer()->getType());
9797
}
98+
99+
public function testUnsetIterator()
100+
{
101+
$keywords = LocalizedSearchKeywords::fromArray(
102+
[
103+
'de' => [
104+
['text'=>'Hallo Welt'],
105+
],
106+
'en' => [
107+
['text' => 'Hello World'],
108+
],
109+
],
110+
Context::of()->setLanguages(['de', 'en'])
111+
);
112+
unset($keywords['de']);
113+
114+
$this->assertCount(1, $keywords);
115+
$keywords->rewind();
116+
$this->assertSame('en', $keywords->key());
117+
$this->assertJsonStringEqualsJsonString('{"en": [{"text": "Hello World"}]}', json_encode($keywords));
118+
}
98119
}

0 commit comments

Comments
 (0)