Skip to content

Commit 0f4406e

Browse files
committed
Merge pull request #29 from Taluu/property-path-collections
Use PropertyPath instead of a wild guess for CollectionSnapshot
2 parents 4ae716d + 81695b4 commit 0f4406e

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

src/Snapshot/CollectionSnapshot.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
ReflectionClass,
1717
InvalidArgumentException;
1818

19-
use Symfony\Component\PropertyAccess\PropertyAccess;
19+
use Symfony\Component\PropertyAccess\PropertyPath,
20+
Symfony\Component\PropertyAccess\PropertyAccess;
2021

2122
use Totem\AbstractSnapshot;
2223

@@ -50,20 +51,22 @@ class CollectionSnapshot extends AbstractSnapshot
5051
* on the situation.
5152
*
5253
* @param mixed $data Either an array or a traversable, data to take a snapshot of
53-
* @param mixed $pkey Key to use as a primary key
54+
* @param mixed $primary Property path compatible value to use to get the primary key of each elements
5455
* @param array $options Array of options
5556
*
5657
* @throws InvalidArgumentException the $data is not an array or a Traversable
5758
* @throws InvalidArgumentException the $data is not an at least 2 dimensional array
5859
* @throws InvalidArgumentException the snapshotClass in the options is not loadable
5960
* @throws InvalidArgumentException the snapshotClass in the options is not a valid snapshot class
60-
* @throws InvalidArgumentException one of the elements of the collection does not have a $pkey key
61+
* @throws InvalidArgumentException one of the elements of the collection does not have a $primary key
6162
*/
62-
public function __construct($data, $pkey, array $options = [])
63+
public function __construct($data, $primary, array $options = [])
6364
{
6465
$this->data = [];
6566
$this->raw = $data;
6667

68+
$primary = new PropertyPath($primary);
69+
6770
$snapshot = null;
6871
$accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
6972

@@ -81,33 +84,23 @@ public function __construct($data, $pkey, array $options = [])
8184
$snapshot = $options['snapshotClass'];
8285
}
8386

84-
if ($data instanceof Traversable) {
85-
$data = iterator_to_array($data);
86-
}
87-
88-
if (!is_array($data)) {
87+
if (!is_array($data) && !$data instanceof Traversable) {
8988
throw new InvalidArgumentException(sprintf('An array or a Traversable was expected to take a snapshot of a collection, "%s" given', is_object($data) ? get_class($data) : gettype($data)));
9089
}
9190

9291
foreach ($data as $key => $value) {
93-
$primary = $pkey;
94-
95-
if (!is_object($value)) {
96-
$primary = '[' . $primary . ']';
97-
}
98-
9992
if (!is_int($key)) {
10093
throw new InvalidArgumentException('The given array / Traversable is not a collection as it contains non numeric keys');
10194
}
10295

10396
if (!$accessor->isReadable($value, $primary)) {
104-
throw new InvalidArgumentException(sprintf('The key "%s" is not defined or readable in one of the elements of the collection', $pkey));
97+
throw new InvalidArgumentException(sprintf('The key "%s" is not defined or readable in one of the elements of the collection', $primary));
10598
}
10699

107-
$primary = $accessor->getValue($value, $primary);
100+
$primaryKey = $accessor->getValue($value, $primary);
108101

109-
$this->link[$primary] = $key;
110-
$this->data[$primary] = $this->snapshot($value, $snapshot);
102+
$this->link[$primaryKey] = $key;
103+
$this->data[$primaryKey] = $this->snapshot($value, $snapshot);
111104
}
112105

113106
parent::normalize();

test/SetTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ public function testComputeCollections()
187187
$old = $new = [['foo' => 'bar', 'baz' => 'fubar'], ['foo' => 'baz', 'baz' => 'fubar']];
188188
$new[0]['baz'] = 'fubaz';
189189

190-
$old = new CollectionSnapshot($old, 'foo');
191-
$new = new CollectionSnapshot($new, 'foo');
190+
$old = new CollectionSnapshot($old, '[foo]');
191+
$new = new CollectionSnapshot($new, '[foo]');
192192

193193
$set = new Set;
194194
$set->compute($old, $new);
195195

196-
$this->assertContainsOnly('integer',array_keys(iterator_to_array($set)));
196+
$this->assertContainsOnly('integer', array_keys(iterator_to_array($set)));
197197
}
198198

199199
/** @dataProvider unaffectedSnapshotComputerProvider */
@@ -209,8 +209,8 @@ public function unaffectedSnapshotComputerProvider()
209209
{
210210
$old = ['foo' => 'bar', 'baz' => 'fubar'];
211211

212-
return [[new CollectionSnapshot([$old], 'foo'), new ArraySnapshot($old)],
213-
[new ArraySnapshot($old), new CollectionSnapshot([$old], 'foo')],
212+
return [[new CollectionSnapshot([$old], '[foo]'), new ArraySnapshot($old)],
213+
[new ArraySnapshot($old), new CollectionSnapshot([$old], '[foo]')],
214214
[new ArraySnapshot($old), new ArraySnapshot(array_merge($old, ['baz' => 'fubaz']))]];
215215
}
216216
}

test/Snapshot/CollectionSnapshotTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testAllValid($hasSnapshot)
8282
$options['snapshotClass'] = 'Totem\\Snapshot';
8383
}
8484

85-
$snapshot = new CollectionSnapshot([['foo' => 'bar', 'baz' => 'fubar']], 'foo', $options);
85+
$snapshot = new CollectionSnapshot([['foo' => 'bar', 'baz' => 'fubar']], '[foo]', $options);
8686

8787
$refl = new ReflectionProperty('Totem\\AbstractSnapshot', 'data');
8888
$refl->setAccessible(true);
@@ -103,13 +103,13 @@ public function allValidProvider()
103103
*/
104104
public function testOriginalKeyNotFound()
105105
{
106-
$snapshot = new CollectionSnapshot([['foo' => 'bar']], 'foo');
106+
$snapshot = new CollectionSnapshot([['foo' => 'bar']], '[foo]');
107107
$snapshot->getOriginalKey('baz');
108108
}
109109

110110
public function testOriginalKey()
111111
{
112-
$snapshot = new CollectionSnapshot([['foo' => 'bar']], 'foo');
112+
$snapshot = new CollectionSnapshot([['foo' => 'bar']], '[foo]');
113113

114114
$this->assertSame(0, $snapshot->getOriginalKey('bar'));
115115
}

0 commit comments

Comments
 (0)