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

Commit

Permalink
fix(Collection): fix iterator for unset elements of collections (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Schulze authored Apr 12, 2017
1 parent a1cfc99 commit 5687380
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Model/Common/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Collection extends AbstractJsonDeserializeObject implements \Iterator, \Js
{
const ID = 'id';
use ContextTrait;
const COLLECTION_TYPE = Collection::TYPE_LIST;
const TYPE_LIST = 'List';
const TYPE_MAP = 'Map';

/**
* @var string
Expand Down Expand Up @@ -367,5 +370,20 @@ public function offsetUnset($offset)
{
unset($this->rawData[$offset]);
unset($this->typeData[$offset]);
$rawKeys = array_keys($this->rawData);
$typeKeys = array_keys($this->typeData);
$keys = array_merge($rawKeys, $typeKeys);
$this->keys = array_unique($keys);
}

protected function toJson()
{
$data = parent::toJson();

if (static::COLLECTION_TYPE == self::TYPE_LIST) {
return array_values($data);
}

return $data;
}
}
1 change: 1 addition & 0 deletions src/Model/Product/FacetResultCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class FacetResultCollection extends Collection
{
const OFFSET = 'offset';
const COLLECTION_TYPE = Collection::TYPE_MAP;

protected $type = FacetResult::class;

Expand Down
2 changes: 2 additions & 0 deletions src/Model/Product/LocalizedSearchKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
class LocalizedSearchKeywords extends Collection
{
const COLLECTION_TYPE = Collection::TYPE_MAP;

protected $type = SearchKeywords::class;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Model/Product/LocalizedSuggestionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
class LocalizedSuggestionCollection extends Collection
{
const COLLECTION_TYPE = Collection::TYPE_MAP;
protected $type = SuggestionCollection::class;

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Model/Common/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,17 @@ public function testAdd()

$this->assertCount(2, $collection);
}

public function testUnsetIterator()
{
$collection = Collection::fromArray([1]);
$collection[] = 2;
unset($collection[0]);

$this->assertCount(1, $collection);
$collection->rewind();
$this->assertSame(1, $collection->key());
$this->assertSame(2, $collection->current());
$this->assertJsonStringEqualsJsonString('[2]', json_encode($collection));
}
}
21 changes: 21 additions & 0 deletions tests/unit/Model/Product/LocalizedSearchKeywordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,25 @@ public function testFromArray()
$this->assertSame('Hello World', $keywords->en->getAt(0)->getText());
$this->assertSame('custom', $keywords->de->getAt(1)->getSuggestTokenizer()->getType());
}

public function testUnsetIterator()
{
$keywords = LocalizedSearchKeywords::fromArray(
[
'de' => [
['text'=>'Hallo Welt'],
],
'en' => [
['text' => 'Hello World'],
],
],
Context::of()->setLanguages(['de', 'en'])
);
unset($keywords['de']);

$this->assertCount(1, $keywords);
$keywords->rewind();
$this->assertSame('en', $keywords->key());
$this->assertJsonStringEqualsJsonString('{"en": [{"text": "Hello World"}]}', json_encode($keywords));
}
}

0 comments on commit 5687380

Please # to comment.