Skip to content

Commit

Permalink
Allow overriding saver id only via id parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Oct 14, 2020
1 parent 9d02c13 commit 9ba0c01
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Xhgui/Saver/MongoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(MongoCollection $collection)
$this->_collection = $collection;
}

public function save(array $data): string
public function save(array $data, string $id = null): string
{
// build 'request_ts' and 'request_date' from 'request_ts_micro'
$ts = $data['meta']['request_ts_micro'];
Expand All @@ -36,7 +36,7 @@ public function save(array $data): string
'request_date' => date('Y-m-d', $sec),
];

$id = $data['_id'] ?? new MongoId();
$id = $id ?? new MongoId();
$a = [
'_id' => $id,
'meta' => $meta,
Expand Down
4 changes: 2 additions & 2 deletions src/Xhgui/Saver/PdoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(PdoRepository $db)
$this->db = $db;
}

public function save(array $data): string
public function save(array $data, string $id = null): string
{
$main = $data['profile']['main()'];

Expand All @@ -25,7 +25,7 @@ public function save(array $data): string
$sec = $ts['sec'];
$usec = $ts['usec'];

$id = $data['_id'] ?? Util::generateId();
$id = $id ?? Util::generateId();
$this->db->saveProfile([
'id' => $id,
'profile' => json_encode($data['profile']),
Expand Down
2 changes: 1 addition & 1 deletion src/Xhgui/Saver/SaverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface SaverInterface
/**
* Returns id of the saved profile
*/
public function save(array $data): string;
public function save(array $data, string $id = null): string;
}
2 changes: 1 addition & 1 deletion tests/Saver/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testSave()
$saver = new MongoSaver($collection);

foreach ($data as $profile) {
$saver->save($profile);
$saver->save($profile, $profile['_id'] ?? null);
}
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ function loadFixture(SaverInterface $saver, $file)
if (isset($record['_id'])) {
$record['_id'] = new MongoId($record['_id']);
}
$saver->save($record);
$saver->save($record, $record['_id'] ?? null);
}
}

0 comments on commit 9ba0c01

Please sign in to comment.