Skip to content

Commit

Permalink
Add a link attribute to the CollectionSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Taluu committed Jul 23, 2014
1 parent 2d63a4e commit c35f656
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Snapshot/CollectionSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
*/
class CollectionSnapshot extends AbstractSnapshot
{
/**
* Collection of the new keys (extracted from the primary key) => old keys
*
* @var integer[]
*/
private $link = [];

/**
* Construct the snapshot
*
Expand Down Expand Up @@ -97,7 +104,10 @@ public function __construct($data, $pkey, array $options = [])
throw new InvalidArgumentException(sprintf('The key "%s" is not defined or readable in one of the elements of the collection', $pkey));
}

$this->data[$accessor->getValue($value, $primary)] = $this->snapshot($value, $snapshot);
$primary = $accessor->getValue($value, $primary);

$this->link[$primary] = $key;
$this->data[$primary] = $this->snapshot($value, $snapshot);
}

parent::normalize();
Expand All @@ -122,5 +132,21 @@ private function snapshot($value, $class = null)

return new $class($value);
}

/**
* Returns the original key for the primary key $primary
*
* @param mixed $primary Primary key to search
*
* @return integer original key
* @throws InvalidArgumentException primary key not found
*/
public function getOriginalKey($primary) {
if (!isset($this->link[$primary])) {
throw new InvalidArgumentException(sprintf('The primary key "%s" is not in the computed dataset', $primary));
}

return $this->link[$primary];
}
}

17 changes: 17 additions & 0 deletions test/Snapshot/CollectionSnapshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,22 @@ public function allValidProvider()
return [[true],
[false]];
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The primary key "baz" is not in the computed dataset
*/
public function testOriginalKeyNotFound()
{
$snapshot = new CollectionSnapshot([['foo' => 'bar']], 'foo');
$snapshot->getOriginalKey('baz');
}

public function testOriginalKey()
{
$snapshot = new CollectionSnapshot([['foo' => 'bar']], 'foo');

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

0 comments on commit c35f656

Please sign in to comment.