Skip to content

Commit

Permalink
scalar id support
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bretterklieber committed May 7, 2021
1 parent 56e51a6 commit aba6bcf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 50 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
[![License](https://img.shields.io/badge/license-BSD-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![PHP 7.4](https://img.shields.io/badge/php-7.4-yellow.svg)](http://www.php.net)
[![PHP 8.0](https://img.shields.io/badge/php-8.0-yellow.svg)](http://www.php.net)
[![Build Status](https://travis-ci.org/mbretter/stk-mongodb.svg?branch=master)](https://travis-ci.org/mbretter/stk-mongodb)
[![Coverage](https://coveralls.io/repos/github/mbretter/stk-mongodb/badge.svg?branch=master)](https://coveralls.io/github/mbretter/stk-mongodb?branch=master)
[![Latest Stable Version](https://img.shields.io/packagist/v/mbretter/stk-mongodb.svg)](https://packagist.org/packages/mbretter/stk-mongodb)
[![Total Downloads](https://img.shields.io/packagist/dt/mbretter/stk-mongodb.svg)](https://packagist.org/packages/mbretter/stk-mongodb)
17 changes: 10 additions & 7 deletions src/Stk/MongoDB/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ public function upsert(array $criteria, ImmutableInterface $row, $fields = [], $
*/
public function find($query = [], $options = []): IteratorIterator
{
if (array_key_exists('_id', $query) && is_string($query['_id'])) {
$query['_id'] = new ObjectId($query['_id']);
if (array_key_exists('_id', $query)) {
$query['_id'] = is_string($query['_id']) ? new ObjectId($query['_id']) : $query['_id'];
}

$this->debug(__METHOD__ . ":" . $this->_collection . ":" . print_r($query,
Expand Down Expand Up @@ -346,16 +346,17 @@ public function fetch(IteratorIterator $cursor): ?ImmutableInterface
}

/**
* @param array|string $query
* @param array|string|mixed $query
* @param array $fields
*
* @return array|object|null|ImmutableInterface
*/
public function findOne($query = [], $fields = [])
{
if (is_string($query)) {
$id = $query;
$query = ['_id' => new ObjectId($id)];
$query = ['_id' => new ObjectId($query)];
} elseif (is_scalar($query)) {
$query = ['_id' => $query];
}

$this->debug(__METHOD__ . ":" . $this->_collection . ":" . print_r($query, true));
Expand All @@ -374,8 +375,10 @@ public function findOne($query = [], $fields = [])
*/
public function query($query = [], $options = []): CursorInterface
{
if (array_key_exists('_id', $query) && is_string($query['_id'])) {
$query['_id'] = new ObjectId($query['_id']);
if (array_key_exists('_id', $query)) {
if (is_string($query['_id'])) {
$query['_id'] = new ObjectId($query['_id']);
}
}

return $this->_collection->find($query, $options);
Expand Down
Loading

0 comments on commit aba6bcf

Please sign in to comment.