From fe278ac62e1f14a7c1bd03da0355244f781af1c1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 2 Aug 2016 15:03:00 +0430 Subject: [PATCH 01/20] Support laravel 5.3 New Laravel queries always return collection. this is a backward compatible enhancement as Collections can be used as Arrays! --- src/Jenssegers/Mongodb/Query/Builder.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index d7a60d4d1..f299b654d 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -146,7 +146,7 @@ public function find($id, $columns = []) * Execute the query as a "select" statement. * * @param array $columns - * @return array|static[] + * @return array|static[]|Collection */ public function get($columns = []) { @@ -157,7 +157,7 @@ public function get($columns = []) * Execute the query as a fresh "select" statement. * * @param array $columns - * @return array|static[] + * @return array|static[]|Collection */ public function getFresh($columns = []) { @@ -259,7 +259,7 @@ public function getFresh($columns = []) $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); // Return results - return $results; + return new Collection($results); } // Distinct query @@ -274,7 +274,7 @@ public function getFresh($columns = []) $result = $this->collection->distinct($column); } - return $result; + return new Collection($result); } // Normal query @@ -317,7 +317,7 @@ public function getFresh($columns = []) $cursor = $this->collection->find($wheres, $options); // Return results as an array with numeric keys - return iterator_to_array($cursor, false); + return new Collection(iterator_to_array($cursor, false)); } } From 76b86559ffd78ce167474f95a2da1675d88921d2 Mon Sep 17 00:00:00 2001 From: pi0 Date: Tue, 2 Aug 2016 17:39:08 +0430 Subject: [PATCH 02/20] Backward compability --- .../Mongodb/MongodbServiceProvider.php | 3 + src/Jenssegers/Mongodb/Query/Builder.php | 197 +++++++++--------- 2 files changed, 97 insertions(+), 103 deletions(-) diff --git a/src/Jenssegers/Mongodb/MongodbServiceProvider.php b/src/Jenssegers/Mongodb/MongodbServiceProvider.php index bba2f6fd0..5fca7990e 100644 --- a/src/Jenssegers/Mongodb/MongodbServiceProvider.php +++ b/src/Jenssegers/Mongodb/MongodbServiceProvider.php @@ -14,6 +14,9 @@ public function boot() Model::setConnectionResolver($this->app['db']); Model::setEventDispatcher($this->app['events']); + + $s=explode('.',\Illuminate\Foundation\Application::VERSION); + define('SHOULD_RETURN_COLLECTION',(10*$s[0]+$s[1])>=53); } /** diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index f299b654d..8a4372b71 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -11,6 +11,7 @@ use MongoDB\BSON\Regex; use MongoDB\BSON\UTCDateTime; + class Builder extends BaseBuilder { /** @@ -69,12 +70,12 @@ class Builder extends BaseBuilder * @var array */ protected $conversion = [ - '=' => '=', + '=' => '=', '!=' => '$ne', '<>' => '$ne', - '<' => '$lt', + '<' => '$lt', '<=' => '$lte', - '>' => '$gt', + '>' => '$gt', '>=' => '$gte', ]; @@ -82,7 +83,7 @@ class Builder extends BaseBuilder * Create a new query builder instance. * * @param Connection $connection - * @param Processor $processor + * @param Processor $processor */ public function __construct(Connection $connection, Processor $processor) { @@ -94,7 +95,7 @@ public function __construct(Connection $connection, Processor $processor) /** * Set the projections. * - * @param array $columns + * @param array $columns * @return $this */ public function project($columns) @@ -133,8 +134,8 @@ public function hint($index) /** * Execute a query for a single record by ID. * - * @param mixed $id - * @param array $columns + * @param mixed $id + * @param array $columns * @return mixed */ public function find($id, $columns = []) @@ -145,7 +146,7 @@ public function find($id, $columns = []) /** * Execute the query as a "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function get($columns = []) @@ -156,7 +157,7 @@ public function get($columns = []) /** * Execute the query as a fresh "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function getFresh($columns = []) @@ -207,8 +208,7 @@ public function getFresh($columns = []) // Translate count into sum. if ($function == 'count') { $group['aggregate'] = ['$sum' => 1]; - } - // Pass other functions directly. + } // Pass other functions directly. else { $group['aggregate'] = ['$' . $function => '$' . $column]; } @@ -259,10 +259,8 @@ public function getFresh($columns = []) $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); // Return results - return new Collection($results); - } - - // Distinct query + return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; + } // Distinct query elseif ($this->distinct) { // Return distinct results directly $column = isset($this->columns[0]) ? $this->columns[0] : '_id'; @@ -274,10 +272,8 @@ public function getFresh($columns = []) $result = $this->collection->distinct($column); } - return new Collection($result); - } - - // Normal query + return SHOULD_RETURN_COLLECTION ? new Collection($result) : $result; + } // Normal query else { $columns = []; @@ -317,7 +313,8 @@ public function getFresh($columns = []) $cursor = $this->collection->find($wheres, $options); // Return results as an array with numeric keys - return new Collection(iterator_to_array($cursor, false)); + $results = iterator_to_array($cursor, false); + return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; } } @@ -331,13 +328,13 @@ public function generateCacheKey() $key = [ 'connection' => $this->collection->getDatabaseName(), 'collection' => $this->collection->getCollectionName(), - 'wheres' => $this->wheres, - 'columns' => $this->columns, - 'groups' => $this->groups, - 'orders' => $this->orders, - 'offset' => $this->offset, - 'limit' => $this->limit, - 'aggregate' => $this->aggregate, + 'wheres' => $this->wheres, + 'columns' => $this->columns, + 'groups' => $this->groups, + 'orders' => $this->orders, + 'offset' => $this->offset, + 'limit' => $this->limit, + 'aggregate' => $this->aggregate, ]; return md5(serialize(array_values($key))); @@ -346,8 +343,8 @@ public function generateCacheKey() /** * Execute an aggregate function on the database. * - * @param string $function - * @param array $columns + * @param string $function + * @param array $columns * @return mixed */ public function aggregate($function, $columns = []) @@ -363,7 +360,7 @@ public function aggregate($function, $columns = []) $this->aggregate = null; if (isset($results[0])) { - $result = (array) $results[0]; + $result = (array)$results[0]; return $result['aggregate']; } @@ -376,7 +373,7 @@ public function aggregate($function, $columns = []) */ public function exists() { - return ! is_null($this->first()); + return !is_null($this->first()); } /** @@ -398,8 +395,8 @@ public function distinct($column = false) /** * Add an "order by" clause to the query. * - * @param string $column - * @param string $direction + * @param string $column + * @param string $direction * @return Builder */ public function orderBy($column, $direction = 'asc') @@ -420,10 +417,10 @@ public function orderBy($column, $direction = 'asc') /** * Add a where between statement to the query. * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not * @return Builder */ public function whereBetween($column, array $values, $boolean = 'and', $not = false) @@ -438,8 +435,8 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa /** * Set the limit and offset for a given page. * - * @param int $page - * @param int $perPage + * @param int $page + * @param int $perPage * @return \Illuminate\Database\Query\Builder|static */ public function forPage($page, $perPage = 15) @@ -452,7 +449,7 @@ public function forPage($page, $perPage = 15) /** * Insert a new record into the database. * - * @param array $values + * @param array $values * @return bool */ public function insert(array $values) @@ -464,34 +461,34 @@ public function insert(array $values) foreach ($values as $value) { // As soon as we find a value that is not an array we assume the user is // inserting a single document. - if (! is_array($value)) { + if (!is_array($value)) { $batch = false; break; } } - if (! $batch) { + if (!$batch) { $values = [$values]; } // Batch insert $result = $this->collection->insertMany($values); - return (1 == (int) $result->isAcknowledged()); + return (1 == (int)$result->isAcknowledged()); } /** * Insert a new record and get the value of the primary key. * - * @param array $values - * @param string $sequence + * @param array $values + * @param string $sequence * @return int */ public function insertGetId(array $values, $sequence = null) { $result = $this->collection->insertOne($values); - if (1 == (int) $result->isAcknowledged()) { + if (1 == (int)$result->isAcknowledged()) { if (is_null($sequence)) { $sequence = '_id'; } @@ -504,14 +501,14 @@ public function insertGetId(array $values, $sequence = null) /** * Update a record in the database. * - * @param array $values - * @param array $options + * @param array $values + * @param array $options * @return int */ public function update(array $values, array $options = []) { // Use $set as default operator. - if (! starts_with(key($values), '$')) { + if (!starts_with(key($values), '$')) { $values = ['$set' => $values]; } @@ -521,16 +518,16 @@ public function update(array $values, array $options = []) /** * Increment a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = [], array $options = []) { $query = ['$inc' => [$column => $amount]]; - if (! empty($extra)) { + if (!empty($extra)) { $query['$set'] = $extra; } @@ -547,9 +544,9 @@ public function increment($column, $amount = 1, array $extra = [], array $option /** * Decrement a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = [], array $options = []) @@ -560,8 +557,8 @@ public function decrement($column, $amount = 1, array $extra = [], array $option /** * Get an array with the values of a given column. * - * @param string $column - * @param string|null $key + * @param string $column + * @param string|null $key * @return array */ public function pluck($column, $key = null) @@ -581,14 +578,14 @@ public function pluck($column, $key = null) /** * Delete a record from the database. * - * @param mixed $id + * @param mixed $id * @return int */ public function delete($id = null) { $wheres = $this->compileWheres(); $result = $this->collection->DeleteMany($wheres); - if (1 == (int) $result->isAcknowledged()) { + if (1 == (int)$result->isAcknowledged()) { return $result->getDeletedCount(); } @@ -598,7 +595,7 @@ public function delete($id = null) /** * Set the collection which the query is targeting. * - * @param string $collection + * @param string $collection * @return Builder */ public function from($collection) @@ -617,14 +614,14 @@ public function truncate() { $result = $this->collection->drop(); - return (1 == (int) $result->ok); + return (1 == (int)$result->ok); } /** * Get an array with the values of a given column. * - * @param string $column - * @param string $key + * @param string $column + * @param string $key * @return array */ public function lists($column, $key = null) @@ -634,7 +631,7 @@ public function lists($column, $key = null) // Convert ObjectID's to strings so that lists can do its work. $results = $results->map(function ($item) { - $item['_id'] = (string) $item['_id']; + $item['_id'] = (string)$item['_id']; return $item; }); @@ -648,7 +645,7 @@ public function lists($column, $key = null) /** * Create a raw database expression. * - * @param closure $expression + * @param closure $expression * @return mixed */ public function raw($expression = null) @@ -656,10 +653,8 @@ public function raw($expression = null) // Execute the closure on the mongodb collection if ($expression instanceof Closure) { return call_user_func($expression, $this->collection); - } - - // Create an expression for the given value - elseif (! is_null($expression)) { + } // Create an expression for the given value + elseif (!is_null($expression)) { return new Expression($expression); } @@ -670,8 +665,8 @@ public function raw($expression = null) /** * Append one or more values to an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function push($column, $value = null, $unique = false) @@ -696,8 +691,8 @@ public function push($column, $value = null, $unique = false) /** * Remove one or more values from an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function pull($column, $value = null) @@ -725,7 +720,7 @@ public function pull($column, $value = null) */ public function drop($columns) { - if (! is_array($columns)) { + if (!is_array($columns)) { $columns = [$columns]; } @@ -753,20 +748,20 @@ public function newQuery() /** * Perform an update query. * - * @param array $query - * @param array $options + * @param array $query + * @param array $options * @return int */ protected function performUpdate($query, array $options = []) { // Update multiple items by default. - if (! array_key_exists('multiple', $options)) { + if (!array_key_exists('multiple', $options)) { $options['multiple'] = true; } $wheres = $this->compileWheres(); $result = $this->collection->UpdateMany($wheres, $query, $options); - if (1 == (int) $result->isAcknowledged()) { + if (1 == (int)$result->isAcknowledged()) { return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount(); } @@ -791,10 +786,10 @@ public function convertKey($id) /** * Add a basic where clause to the query. * - * @param string $column - * @param string $operator - * @param mixed $value - * @param string $boolean + * @param string $column + * @param string $operator + * @param mixed $value + * @param string $boolean * @return \Illuminate\Database\Query\Builder|static * * @throws \InvalidArgumentException @@ -835,14 +830,14 @@ protected function compileWheres() // Operator conversions $convert = [ - 'regexp' => 'regex', - 'elemmatch' => 'elemMatch', + 'regexp' => 'regex', + 'elemmatch' => 'elemMatch', 'geointersects' => 'geoIntersects', - 'geowithin' => 'geoWithin', - 'nearsphere' => 'nearSphere', - 'maxdistance' => 'maxDistance', - 'centersphere' => 'centerSphere', - 'uniquedocs' => 'uniqueDocs', + 'geowithin' => 'geoWithin', + 'nearsphere' => 'nearSphere', + 'maxdistance' => 'maxDistance', + 'centersphere' => 'centerSphere', + 'uniquedocs' => 'uniqueDocs', ]; if (array_key_exists($where['operator'], $convert)) { @@ -857,9 +852,7 @@ protected function compileWheres() foreach ($where['values'] as &$value) { $value = $this->convertKey($value); } - } - - // Single value. + } // Single value. elseif (isset($where['value'])) { $where['value'] = $this->convertKey($where['value']); } @@ -911,20 +904,18 @@ protected function compileWhereBasic($where) $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); // Convert like to regular expression. - if (! starts_with($value, '%')) { + if (!starts_with($value, '%')) { $regex = '^' . $regex; } - if (! ends_with($value, '%')) { + if (!ends_with($value, '%')) { $regex = $regex . '$'; } $value = new Regex($regex, 'i'); - } - - // Manipulate regexp operations. + } // Manipulate regexp operations. elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) { // Automatically convert regular expression strings to Regex objects. - if (! $value instanceof Regex) { + if (!$value instanceof Regex) { $e = explode('/', $value); $flag = end($e); $regstr = substr($value, 1, -(strlen($flag) + 1)); @@ -938,7 +929,7 @@ protected function compileWhereBasic($where) } } - if (! isset($operator) or $operator == '=') { + if (!isset($operator) or $operator == '=') { $query = [$column => $value]; } elseif (array_key_exists($operator, $this->conversion)) { $query = [$column => [$this->conversion[$operator] => $value]]; @@ -1023,8 +1014,8 @@ protected function compileWhereRaw($where) /** * Handle dynamic method calls into the method. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) @@ -1035,4 +1026,4 @@ public function __call($method, $parameters) return parent::__call($method, $parameters); } -} +} \ No newline at end of file From 5706ed380bbf301ceffb9a5eee205b841677ff0c Mon Sep 17 00:00:00 2001 From: pi0 Date: Tue, 2 Aug 2016 17:56:15 +0430 Subject: [PATCH 03/20] Style CI --- .../Mongodb/MongodbServiceProvider.php | 4 +- src/Jenssegers/Mongodb/Query/Builder.php | 188 +++++++++--------- 2 files changed, 101 insertions(+), 91 deletions(-) diff --git a/src/Jenssegers/Mongodb/MongodbServiceProvider.php b/src/Jenssegers/Mongodb/MongodbServiceProvider.php index 5fca7990e..ee72bf034 100644 --- a/src/Jenssegers/Mongodb/MongodbServiceProvider.php +++ b/src/Jenssegers/Mongodb/MongodbServiceProvider.php @@ -15,8 +15,8 @@ public function boot() Model::setEventDispatcher($this->app['events']); - $s=explode('.',\Illuminate\Foundation\Application::VERSION); - define('SHOULD_RETURN_COLLECTION',(10*$s[0]+$s[1])>=53); + $s = explode('.', \Illuminate\Foundation\Application::VERSION); + define('SHOULD_RETURN_COLLECTION', (10 * $s[0] + $s[1]) >= 53); } /** diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 8a4372b71..dabca2a2b 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -11,7 +11,6 @@ use MongoDB\BSON\Regex; use MongoDB\BSON\UTCDateTime; - class Builder extends BaseBuilder { /** @@ -70,12 +69,12 @@ class Builder extends BaseBuilder * @var array */ protected $conversion = [ - '=' => '=', + '=' => '=', '!=' => '$ne', '<>' => '$ne', - '<' => '$lt', + '<' => '$lt', '<=' => '$lte', - '>' => '$gt', + '>' => '$gt', '>=' => '$gte', ]; @@ -83,7 +82,7 @@ class Builder extends BaseBuilder * Create a new query builder instance. * * @param Connection $connection - * @param Processor $processor + * @param Processor $processor */ public function __construct(Connection $connection, Processor $processor) { @@ -95,7 +94,7 @@ public function __construct(Connection $connection, Processor $processor) /** * Set the projections. * - * @param array $columns + * @param array $columns * @return $this */ public function project($columns) @@ -134,8 +133,8 @@ public function hint($index) /** * Execute a query for a single record by ID. * - * @param mixed $id - * @param array $columns + * @param mixed $id + * @param array $columns * @return mixed */ public function find($id, $columns = []) @@ -146,7 +145,7 @@ public function find($id, $columns = []) /** * Execute the query as a "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function get($columns = []) @@ -157,7 +156,7 @@ public function get($columns = []) /** * Execute the query as a fresh "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function getFresh($columns = []) @@ -208,7 +207,8 @@ public function getFresh($columns = []) // Translate count into sum. if ($function == 'count') { $group['aggregate'] = ['$sum' => 1]; - } // Pass other functions directly. + } + // Pass other functions directly. else { $group['aggregate'] = ['$' . $function => '$' . $column]; } @@ -260,7 +260,9 @@ public function getFresh($columns = []) // Return results return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; - } // Distinct query + } + + // Distinct query elseif ($this->distinct) { // Return distinct results directly $column = isset($this->columns[0]) ? $this->columns[0] : '_id'; @@ -273,7 +275,9 @@ public function getFresh($columns = []) } return SHOULD_RETURN_COLLECTION ? new Collection($result) : $result; - } // Normal query + } + + // Normal query else { $columns = []; @@ -328,13 +332,13 @@ public function generateCacheKey() $key = [ 'connection' => $this->collection->getDatabaseName(), 'collection' => $this->collection->getCollectionName(), - 'wheres' => $this->wheres, - 'columns' => $this->columns, - 'groups' => $this->groups, - 'orders' => $this->orders, - 'offset' => $this->offset, - 'limit' => $this->limit, - 'aggregate' => $this->aggregate, + 'wheres' => $this->wheres, + 'columns' => $this->columns, + 'groups' => $this->groups, + 'orders' => $this->orders, + 'offset' => $this->offset, + 'limit' => $this->limit, + 'aggregate' => $this->aggregate, ]; return md5(serialize(array_values($key))); @@ -343,8 +347,8 @@ public function generateCacheKey() /** * Execute an aggregate function on the database. * - * @param string $function - * @param array $columns + * @param string $function + * @param array $columns * @return mixed */ public function aggregate($function, $columns = []) @@ -360,7 +364,7 @@ public function aggregate($function, $columns = []) $this->aggregate = null; if (isset($results[0])) { - $result = (array)$results[0]; + $result = (array) $results[0]; return $result['aggregate']; } @@ -373,7 +377,7 @@ public function aggregate($function, $columns = []) */ public function exists() { - return !is_null($this->first()); + return ! is_null($this->first()); } /** @@ -395,8 +399,8 @@ public function distinct($column = false) /** * Add an "order by" clause to the query. * - * @param string $column - * @param string $direction + * @param string $column + * @param string $direction * @return Builder */ public function orderBy($column, $direction = 'asc') @@ -417,10 +421,10 @@ public function orderBy($column, $direction = 'asc') /** * Add a where between statement to the query. * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not * @return Builder */ public function whereBetween($column, array $values, $boolean = 'and', $not = false) @@ -435,8 +439,8 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa /** * Set the limit and offset for a given page. * - * @param int $page - * @param int $perPage + * @param int $page + * @param int $perPage * @return \Illuminate\Database\Query\Builder|static */ public function forPage($page, $perPage = 15) @@ -449,7 +453,7 @@ public function forPage($page, $perPage = 15) /** * Insert a new record into the database. * - * @param array $values + * @param array $values * @return bool */ public function insert(array $values) @@ -461,34 +465,34 @@ public function insert(array $values) foreach ($values as $value) { // As soon as we find a value that is not an array we assume the user is // inserting a single document. - if (!is_array($value)) { + if (! is_array($value)) { $batch = false; break; } } - if (!$batch) { + if (! $batch) { $values = [$values]; } // Batch insert $result = $this->collection->insertMany($values); - return (1 == (int)$result->isAcknowledged()); + return (1 == (int) $result->isAcknowledged()); } /** * Insert a new record and get the value of the primary key. * - * @param array $values - * @param string $sequence + * @param array $values + * @param string $sequence * @return int */ public function insertGetId(array $values, $sequence = null) { $result = $this->collection->insertOne($values); - if (1 == (int)$result->isAcknowledged()) { + if (1 == (int) $result->isAcknowledged()) { if (is_null($sequence)) { $sequence = '_id'; } @@ -501,14 +505,14 @@ public function insertGetId(array $values, $sequence = null) /** * Update a record in the database. * - * @param array $values - * @param array $options + * @param array $values + * @param array $options * @return int */ public function update(array $values, array $options = []) { // Use $set as default operator. - if (!starts_with(key($values), '$')) { + if (! starts_with(key($values), '$')) { $values = ['$set' => $values]; } @@ -518,16 +522,16 @@ public function update(array $values, array $options = []) /** * Increment a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = [], array $options = []) { $query = ['$inc' => [$column => $amount]]; - if (!empty($extra)) { + if (! empty($extra)) { $query['$set'] = $extra; } @@ -544,9 +548,9 @@ public function increment($column, $amount = 1, array $extra = [], array $option /** * Decrement a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = [], array $options = []) @@ -557,8 +561,8 @@ public function decrement($column, $amount = 1, array $extra = [], array $option /** * Get an array with the values of a given column. * - * @param string $column - * @param string|null $key + * @param string $column + * @param string|null $key * @return array */ public function pluck($column, $key = null) @@ -578,14 +582,14 @@ public function pluck($column, $key = null) /** * Delete a record from the database. * - * @param mixed $id + * @param mixed $id * @return int */ public function delete($id = null) { $wheres = $this->compileWheres(); $result = $this->collection->DeleteMany($wheres); - if (1 == (int)$result->isAcknowledged()) { + if (1 == (int) $result->isAcknowledged()) { return $result->getDeletedCount(); } @@ -595,7 +599,7 @@ public function delete($id = null) /** * Set the collection which the query is targeting. * - * @param string $collection + * @param string $collection * @return Builder */ public function from($collection) @@ -614,14 +618,14 @@ public function truncate() { $result = $this->collection->drop(); - return (1 == (int)$result->ok); + return (1 == (int) $result->ok); } /** * Get an array with the values of a given column. * - * @param string $column - * @param string $key + * @param string $column + * @param string $key * @return array */ public function lists($column, $key = null) @@ -631,7 +635,7 @@ public function lists($column, $key = null) // Convert ObjectID's to strings so that lists can do its work. $results = $results->map(function ($item) { - $item['_id'] = (string)$item['_id']; + $item['_id'] = (string) $item['_id']; return $item; }); @@ -645,7 +649,7 @@ public function lists($column, $key = null) /** * Create a raw database expression. * - * @param closure $expression + * @param closure $expression * @return mixed */ public function raw($expression = null) @@ -653,8 +657,10 @@ public function raw($expression = null) // Execute the closure on the mongodb collection if ($expression instanceof Closure) { return call_user_func($expression, $this->collection); - } // Create an expression for the given value - elseif (!is_null($expression)) { + } + + // Create an expression for the given value + elseif (! is_null($expression)) { return new Expression($expression); } @@ -665,8 +671,8 @@ public function raw($expression = null) /** * Append one or more values to an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function push($column, $value = null, $unique = false) @@ -691,8 +697,8 @@ public function push($column, $value = null, $unique = false) /** * Remove one or more values from an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function pull($column, $value = null) @@ -720,7 +726,7 @@ public function pull($column, $value = null) */ public function drop($columns) { - if (!is_array($columns)) { + if (! is_array($columns)) { $columns = [$columns]; } @@ -748,20 +754,20 @@ public function newQuery() /** * Perform an update query. * - * @param array $query - * @param array $options + * @param array $query + * @param array $options * @return int */ protected function performUpdate($query, array $options = []) { // Update multiple items by default. - if (!array_key_exists('multiple', $options)) { + if (! array_key_exists('multiple', $options)) { $options['multiple'] = true; } $wheres = $this->compileWheres(); $result = $this->collection->UpdateMany($wheres, $query, $options); - if (1 == (int)$result->isAcknowledged()) { + if (1 == (int) $result->isAcknowledged()) { return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount(); } @@ -786,10 +792,10 @@ public function convertKey($id) /** * Add a basic where clause to the query. * - * @param string $column - * @param string $operator - * @param mixed $value - * @param string $boolean + * @param string $column + * @param string $operator + * @param mixed $value + * @param string $boolean * @return \Illuminate\Database\Query\Builder|static * * @throws \InvalidArgumentException @@ -830,14 +836,14 @@ protected function compileWheres() // Operator conversions $convert = [ - 'regexp' => 'regex', - 'elemmatch' => 'elemMatch', + 'regexp' => 'regex', + 'elemmatch' => 'elemMatch', 'geointersects' => 'geoIntersects', - 'geowithin' => 'geoWithin', - 'nearsphere' => 'nearSphere', - 'maxdistance' => 'maxDistance', - 'centersphere' => 'centerSphere', - 'uniquedocs' => 'uniqueDocs', + 'geowithin' => 'geoWithin', + 'nearsphere' => 'nearSphere', + 'maxdistance' => 'maxDistance', + 'centersphere' => 'centerSphere', + 'uniquedocs' => 'uniqueDocs', ]; if (array_key_exists($where['operator'], $convert)) { @@ -852,7 +858,9 @@ protected function compileWheres() foreach ($where['values'] as &$value) { $value = $this->convertKey($value); } - } // Single value. + } + + // Single value. elseif (isset($where['value'])) { $where['value'] = $this->convertKey($where['value']); } @@ -904,18 +912,20 @@ protected function compileWhereBasic($where) $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); // Convert like to regular expression. - if (!starts_with($value, '%')) { + if (! starts_with($value, '%')) { $regex = '^' . $regex; } - if (!ends_with($value, '%')) { + if (! ends_with($value, '%')) { $regex = $regex . '$'; } $value = new Regex($regex, 'i'); - } // Manipulate regexp operations. + } + + // Manipulate regexp operations. elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) { // Automatically convert regular expression strings to Regex objects. - if (!$value instanceof Regex) { + if (! $value instanceof Regex) { $e = explode('/', $value); $flag = end($e); $regstr = substr($value, 1, -(strlen($flag) + 1)); @@ -929,7 +939,7 @@ protected function compileWhereBasic($where) } } - if (!isset($operator) or $operator == '=') { + if (! isset($operator) or $operator == '=') { $query = [$column => $value]; } elseif (array_key_exists($operator, $this->conversion)) { $query = [$column => [$this->conversion[$operator] => $value]]; @@ -1014,8 +1024,8 @@ protected function compileWhereRaw($where) /** * Handle dynamic method calls into the method. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) From 49eb85cb1775d18112c8c831ab6110d1244fad5c Mon Sep 17 00:00:00 2001 From: pi0 Date: Tue, 2 Aug 2016 17:58:54 +0430 Subject: [PATCH 04/20] Check SHOULD_RETURN_COLLECTION is not defined --- src/Jenssegers/Mongodb/MongodbServiceProvider.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/MongodbServiceProvider.php b/src/Jenssegers/Mongodb/MongodbServiceProvider.php index ee72bf034..6c0376e9a 100644 --- a/src/Jenssegers/Mongodb/MongodbServiceProvider.php +++ b/src/Jenssegers/Mongodb/MongodbServiceProvider.php @@ -15,8 +15,10 @@ public function boot() Model::setEventDispatcher($this->app['events']); - $s = explode('.', \Illuminate\Foundation\Application::VERSION); - define('SHOULD_RETURN_COLLECTION', (10 * $s[0] + $s[1]) >= 53); + if(!defined('SHOULD_RETURN_COLLECTION')) { + $s = explode('.', \Illuminate\Foundation\Application::VERSION); + define('SHOULD_RETURN_COLLECTION', (10 * $s[0] + $s[1]) >= 53); + } } /** From c0e80b4bd698e238dfab6cf2dfbb51ae4eb843f7 Mon Sep 17 00:00:00 2001 From: pi0 Date: Tue, 2 Aug 2016 18:02:00 +0430 Subject: [PATCH 05/20] Style CI --- src/Jenssegers/Mongodb/MongodbServiceProvider.php | 2 +- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/MongodbServiceProvider.php b/src/Jenssegers/Mongodb/MongodbServiceProvider.php index 6c0376e9a..dee5ac076 100644 --- a/src/Jenssegers/Mongodb/MongodbServiceProvider.php +++ b/src/Jenssegers/Mongodb/MongodbServiceProvider.php @@ -15,7 +15,7 @@ public function boot() Model::setEventDispatcher($this->app['events']); - if(!defined('SHOULD_RETURN_COLLECTION')) { + if (!defined('SHOULD_RETURN_COLLECTION')) { $s = explode('.', \Illuminate\Foundation\Application::VERSION); define('SHOULD_RETURN_COLLECTION', (10 * $s[0] + $s[1]) >= 53); } diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index dabca2a2b..076b42719 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -1036,4 +1036,4 @@ public function __call($method, $parameters) return parent::__call($method, $parameters); } -} \ No newline at end of file +} From 9616f091e0d064ae7318b4437db86235d5d71521 Mon Sep 17 00:00:00 2001 From: pi0 Date: Thu, 25 Aug 2016 23:49:29 +0430 Subject: [PATCH 06/20] Closes jenssegers/laravel-mongodb#905 From 9133ba462f7a5a338ecd408d2737edf900c41367 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 01:25:23 +0430 Subject: [PATCH 07/20] Remove deprecated lists --- src/Jenssegers/Mongodb/Eloquent/Builder.php | 52 ++++++++++----------- src/Jenssegers/Mongodb/Query/Builder.php | 14 ++---- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index 3e21468a6..599bd526f 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -13,15 +13,15 @@ class Builder extends EloquentBuilder * @var array */ protected $passthru = [ - 'toSql', 'lists', 'insert', 'insertGetId', 'pluck', + 'toSql', 'insert', 'insertGetId', 'pluck', 'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull', ]; /** * Update a record in the database. * - * @param array $values - * @param array $options + * @param array $values + * @param array $options * @return int */ public function update(array $values, array $options = []) @@ -40,7 +40,7 @@ public function update(array $values, array $options = []) /** * Insert a new record into the database. * - * @param array $values + * @param array $values * @return bool */ public function insert(array $values) @@ -59,8 +59,8 @@ public function insert(array $values) /** * Insert a new record and get the value of the primary key. * - * @param array $values - * @param string $sequence + * @param array $values + * @param string $sequence * @return int */ public function insertGetId(array $values, $sequence = null) @@ -97,9 +97,9 @@ public function delete() /** * Increment a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = []) @@ -127,9 +127,9 @@ public function increment($column, $amount = 1, array $extra = []) /** * Decrement a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = []) @@ -155,11 +155,11 @@ public function decrement($column, $amount = 1, array $extra = []) /** * Add the "has" condition where clause to the query. * - * @param \Illuminate\Database\Eloquent\Builder $hasQuery - * @param \Illuminate\Database\Eloquent\Relations\Relation $relation - * @param string $operator - * @param int $count - * @param string $boolean + * @param \Illuminate\Database\Eloquent\Builder $hasQuery + * @param \Illuminate\Database\Eloquent\Relations\Relation $relation + * @param string $operator + * @param int $count + * @param string $boolean * @return \Illuminate\Database\Eloquent\Builder */ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $operator, $count, $boolean) @@ -167,7 +167,9 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o $query = $hasQuery->getQuery(); // Get the number of related objects for each possible parent. - $relationCount = array_count_values($query->lists($relation->getHasCompareKey())); + $relationCount = array_count_values(array_map(function ($id) { + return (string)$id; // Convert Back ObjectIds to Strings + }, $query->pluck($relation->getHasCompareKey()))); // Remove unwanted related objects based on the operator and count. $relationCount = array_filter($relationCount, function ($counted) use ($count, $operator) { @@ -207,7 +209,7 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o /** * Create a raw database expression. * - * @param closure $expression + * @param closure $expression * @return mixed */ public function raw($expression = null) @@ -219,17 +221,13 @@ public function raw($expression = null) if ($results instanceof Cursor) { $results = iterator_to_array($results, false); return $this->model->hydrate($results); - } - - // Convert Mongo BSONDocument to a single object. + } // Convert Mongo BSONDocument to a single object. elseif ($results instanceof BSONDocument) { $results = $results->getArrayCopy(); - return $this->model->newFromBuilder((array) $results); - } - - // The result is a single object. + return $this->model->newFromBuilder((array)$results); + } // The result is a single object. elseif (is_array($results) and array_key_exists('_id', $results)) { - return $this->model->newFromBuilder((array) $results); + return $this->model->newFromBuilder((array)$results); } return $results; diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 076b42719..c580ceb5d 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -569,14 +569,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - // If the columns are qualified with a table or have an alias, we cannot use - // those directly in the "pluck" operations since the results from the DB - // are only keyed by the column itself. We'll strip the table out here. - return Arr::pluck( - $results, - $column, - $key - ); + return $results->pluck($column,$key); } /** @@ -624,6 +617,7 @@ public function truncate() /** * Get an array with the values of a given column. * + * @deprecated * @param string $column * @param string $key * @return array @@ -640,10 +634,10 @@ public function lists($column, $key = null) return $item; }); - return $results->lists($column, $key)->all(); + return $results->pluck($column, $key)->all(); } - return parent::lists($column, $key); + return parent::pluck($column, $key); } /** From cb42e3b9883061df938c9af8b9613a0cd77e3e0a Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 01:28:15 +0430 Subject: [PATCH 08/20] Style CI --- src/Jenssegers/Mongodb/Eloquent/Builder.php | 6 +++--- src/Jenssegers/Mongodb/Query/Builder.php | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index 599bd526f..fe03acf05 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -168,7 +168,7 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o // Get the number of related objects for each possible parent. $relationCount = array_count_values(array_map(function ($id) { - return (string)$id; // Convert Back ObjectIds to Strings + return (string) $id; // Convert Back ObjectIds to Strings }, $query->pluck($relation->getHasCompareKey()))); // Remove unwanted related objects based on the operator and count. @@ -224,10 +224,10 @@ public function raw($expression = null) } // Convert Mongo BSONDocument to a single object. elseif ($results instanceof BSONDocument) { $results = $results->getArrayCopy(); - return $this->model->newFromBuilder((array)$results); + return $this->model->newFromBuilder((array) $results); } // The result is a single object. elseif (is_array($results) and array_key_exists('_id', $results)) { - return $this->model->newFromBuilder((array)$results); + return $this->model->newFromBuilder((array) $results); } return $results; diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index c580ceb5d..6fb787b47 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -4,7 +4,6 @@ use DateTime; use Illuminate\Database\Query\Builder as BaseBuilder; use Illuminate\Database\Query\Expression; -use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Jenssegers\Mongodb\Connection; use MongoDB\BSON\ObjectID; @@ -569,7 +568,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - return $results->pluck($column,$key); + return $results->pluck($column, $key); } /** From 624b294ce4996f8b7b136d797b4e544d66bfac65 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 01:52:11 +0430 Subject: [PATCH 09/20] Cleanup service provider --- .../Mongodb/MongodbServiceProvider.php | 5 - src/Jenssegers/Mongodb/Query/Builder.php | 203 +++++++++--------- 2 files changed, 101 insertions(+), 107 deletions(-) diff --git a/src/Jenssegers/Mongodb/MongodbServiceProvider.php b/src/Jenssegers/Mongodb/MongodbServiceProvider.php index dee5ac076..bba2f6fd0 100644 --- a/src/Jenssegers/Mongodb/MongodbServiceProvider.php +++ b/src/Jenssegers/Mongodb/MongodbServiceProvider.php @@ -14,11 +14,6 @@ public function boot() Model::setConnectionResolver($this->app['db']); Model::setEventDispatcher($this->app['events']); - - if (!defined('SHOULD_RETURN_COLLECTION')) { - $s = explode('.', \Illuminate\Foundation\Application::VERSION); - define('SHOULD_RETURN_COLLECTION', (10 * $s[0] + $s[1]) >= 53); - } } /** diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 6fb787b47..7504c123d 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -4,6 +4,7 @@ use DateTime; use Illuminate\Database\Query\Builder as BaseBuilder; use Illuminate\Database\Query\Expression; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Jenssegers\Mongodb\Connection; use MongoDB\BSON\ObjectID; @@ -68,32 +69,41 @@ class Builder extends BaseBuilder * @var array */ protected $conversion = [ - '=' => '=', + '=' => '=', '!=' => '$ne', '<>' => '$ne', - '<' => '$lt', + '<' => '$lt', '<=' => '$lte', - '>' => '$gt', + '>' => '$gt', '>=' => '$gte', ]; + /** + * Check if we need to return Collections instead of plain arrays (laravel >= 5.3 ) + * + * @var boolean + */ + protected $use_collection; + /** * Create a new query builder instance. * * @param Connection $connection - * @param Processor $processor + * @param Processor $processor */ public function __construct(Connection $connection, Processor $processor) { $this->grammar = new Grammar; $this->connection = $connection; $this->processor = $processor; + $s = explode('.', \Illuminate\Foundation\Application::VERSION); + $this->use_collection = (10 * $s[0] + $s[1]) >= 53; } /** * Set the projections. * - * @param array $columns + * @param array $columns * @return $this */ public function project($columns) @@ -132,8 +142,8 @@ public function hint($index) /** * Execute a query for a single record by ID. * - * @param mixed $id - * @param array $columns + * @param mixed $id + * @param array $columns * @return mixed */ public function find($id, $columns = []) @@ -144,7 +154,7 @@ public function find($id, $columns = []) /** * Execute the query as a "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function get($columns = []) @@ -155,7 +165,7 @@ public function get($columns = []) /** * Execute the query as a fresh "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function getFresh($columns = []) @@ -206,8 +216,7 @@ public function getFresh($columns = []) // Translate count into sum. if ($function == 'count') { $group['aggregate'] = ['$sum' => 1]; - } - // Pass other functions directly. + } // Pass other functions directly. else { $group['aggregate'] = ['$' . $function => '$' . $column]; } @@ -258,10 +267,8 @@ public function getFresh($columns = []) $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); // Return results - return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; - } - - // Distinct query + return $this->use_collection ? new Collection($results) : $results; + } // Distinct query elseif ($this->distinct) { // Return distinct results directly $column = isset($this->columns[0]) ? $this->columns[0] : '_id'; @@ -273,10 +280,8 @@ public function getFresh($columns = []) $result = $this->collection->distinct($column); } - return SHOULD_RETURN_COLLECTION ? new Collection($result) : $result; - } - - // Normal query + return $this->use_collection ? new Collection($result) : $result; + } // Normal query else { $columns = []; @@ -317,7 +322,7 @@ public function getFresh($columns = []) // Return results as an array with numeric keys $results = iterator_to_array($cursor, false); - return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; + return $this->use_collection ? new Collection($results) : $results; } } @@ -331,13 +336,13 @@ public function generateCacheKey() $key = [ 'connection' => $this->collection->getDatabaseName(), 'collection' => $this->collection->getCollectionName(), - 'wheres' => $this->wheres, - 'columns' => $this->columns, - 'groups' => $this->groups, - 'orders' => $this->orders, - 'offset' => $this->offset, - 'limit' => $this->limit, - 'aggregate' => $this->aggregate, + 'wheres' => $this->wheres, + 'columns' => $this->columns, + 'groups' => $this->groups, + 'orders' => $this->orders, + 'offset' => $this->offset, + 'limit' => $this->limit, + 'aggregate' => $this->aggregate, ]; return md5(serialize(array_values($key))); @@ -346,8 +351,8 @@ public function generateCacheKey() /** * Execute an aggregate function on the database. * - * @param string $function - * @param array $columns + * @param string $function + * @param array $columns * @return mixed */ public function aggregate($function, $columns = []) @@ -363,7 +368,7 @@ public function aggregate($function, $columns = []) $this->aggregate = null; if (isset($results[0])) { - $result = (array) $results[0]; + $result = (array)$results[0]; return $result['aggregate']; } @@ -376,7 +381,7 @@ public function aggregate($function, $columns = []) */ public function exists() { - return ! is_null($this->first()); + return !is_null($this->first()); } /** @@ -398,8 +403,8 @@ public function distinct($column = false) /** * Add an "order by" clause to the query. * - * @param string $column - * @param string $direction + * @param string $column + * @param string $direction * @return Builder */ public function orderBy($column, $direction = 'asc') @@ -420,10 +425,10 @@ public function orderBy($column, $direction = 'asc') /** * Add a where between statement to the query. * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not * @return Builder */ public function whereBetween($column, array $values, $boolean = 'and', $not = false) @@ -438,8 +443,8 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa /** * Set the limit and offset for a given page. * - * @param int $page - * @param int $perPage + * @param int $page + * @param int $perPage * @return \Illuminate\Database\Query\Builder|static */ public function forPage($page, $perPage = 15) @@ -452,7 +457,7 @@ public function forPage($page, $perPage = 15) /** * Insert a new record into the database. * - * @param array $values + * @param array $values * @return bool */ public function insert(array $values) @@ -464,34 +469,34 @@ public function insert(array $values) foreach ($values as $value) { // As soon as we find a value that is not an array we assume the user is // inserting a single document. - if (! is_array($value)) { + if (!is_array($value)) { $batch = false; break; } } - if (! $batch) { + if (!$batch) { $values = [$values]; } // Batch insert $result = $this->collection->insertMany($values); - return (1 == (int) $result->isAcknowledged()); + return (1 == (int)$result->isAcknowledged()); } /** * Insert a new record and get the value of the primary key. * - * @param array $values - * @param string $sequence + * @param array $values + * @param string $sequence * @return int */ public function insertGetId(array $values, $sequence = null) { $result = $this->collection->insertOne($values); - if (1 == (int) $result->isAcknowledged()) { + if (1 == (int)$result->isAcknowledged()) { if (is_null($sequence)) { $sequence = '_id'; } @@ -504,14 +509,14 @@ public function insertGetId(array $values, $sequence = null) /** * Update a record in the database. * - * @param array $values - * @param array $options + * @param array $values + * @param array $options * @return int */ public function update(array $values, array $options = []) { // Use $set as default operator. - if (! starts_with(key($values), '$')) { + if (!starts_with(key($values), '$')) { $values = ['$set' => $values]; } @@ -521,16 +526,16 @@ public function update(array $values, array $options = []) /** * Increment a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = [], array $options = []) { $query = ['$inc' => [$column => $amount]]; - if (! empty($extra)) { + if (!empty($extra)) { $query['$set'] = $extra; } @@ -547,9 +552,9 @@ public function increment($column, $amount = 1, array $extra = [], array $option /** * Decrement a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = [], array $options = []) @@ -560,8 +565,8 @@ public function decrement($column, $amount = 1, array $extra = [], array $option /** * Get an array with the values of a given column. * - * @param string $column - * @param string|null $key + * @param string $column + * @param string|null $key * @return array */ public function pluck($column, $key = null) @@ -574,14 +579,14 @@ public function pluck($column, $key = null) /** * Delete a record from the database. * - * @param mixed $id + * @param mixed $id * @return int */ public function delete($id = null) { $wheres = $this->compileWheres(); $result = $this->collection->DeleteMany($wheres); - if (1 == (int) $result->isAcknowledged()) { + if (1 == (int)$result->isAcknowledged()) { return $result->getDeletedCount(); } @@ -591,7 +596,7 @@ public function delete($id = null) /** * Set the collection which the query is targeting. * - * @param string $collection + * @param string $collection * @return Builder */ public function from($collection) @@ -610,15 +615,15 @@ public function truncate() { $result = $this->collection->drop(); - return (1 == (int) $result->ok); + return (1 == (int)$result->ok); } /** * Get an array with the values of a given column. * * @deprecated - * @param string $column - * @param string $key + * @param string $column + * @param string $key * @return array */ public function lists($column, $key = null) @@ -628,7 +633,7 @@ public function lists($column, $key = null) // Convert ObjectID's to strings so that lists can do its work. $results = $results->map(function ($item) { - $item['_id'] = (string) $item['_id']; + $item['_id'] = (string)$item['_id']; return $item; }); @@ -642,7 +647,7 @@ public function lists($column, $key = null) /** * Create a raw database expression. * - * @param closure $expression + * @param closure $expression * @return mixed */ public function raw($expression = null) @@ -650,10 +655,8 @@ public function raw($expression = null) // Execute the closure on the mongodb collection if ($expression instanceof Closure) { return call_user_func($expression, $this->collection); - } - - // Create an expression for the given value - elseif (! is_null($expression)) { + } // Create an expression for the given value + elseif (!is_null($expression)) { return new Expression($expression); } @@ -664,8 +667,8 @@ public function raw($expression = null) /** * Append one or more values to an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function push($column, $value = null, $unique = false) @@ -690,8 +693,8 @@ public function push($column, $value = null, $unique = false) /** * Remove one or more values from an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function pull($column, $value = null) @@ -719,7 +722,7 @@ public function pull($column, $value = null) */ public function drop($columns) { - if (! is_array($columns)) { + if (!is_array($columns)) { $columns = [$columns]; } @@ -747,20 +750,20 @@ public function newQuery() /** * Perform an update query. * - * @param array $query - * @param array $options + * @param array $query + * @param array $options * @return int */ protected function performUpdate($query, array $options = []) { // Update multiple items by default. - if (! array_key_exists('multiple', $options)) { + if (!array_key_exists('multiple', $options)) { $options['multiple'] = true; } $wheres = $this->compileWheres(); $result = $this->collection->UpdateMany($wheres, $query, $options); - if (1 == (int) $result->isAcknowledged()) { + if (1 == (int)$result->isAcknowledged()) { return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount(); } @@ -785,10 +788,10 @@ public function convertKey($id) /** * Add a basic where clause to the query. * - * @param string $column - * @param string $operator - * @param mixed $value - * @param string $boolean + * @param string $column + * @param string $operator + * @param mixed $value + * @param string $boolean * @return \Illuminate\Database\Query\Builder|static * * @throws \InvalidArgumentException @@ -829,14 +832,14 @@ protected function compileWheres() // Operator conversions $convert = [ - 'regexp' => 'regex', - 'elemmatch' => 'elemMatch', + 'regexp' => 'regex', + 'elemmatch' => 'elemMatch', 'geointersects' => 'geoIntersects', - 'geowithin' => 'geoWithin', - 'nearsphere' => 'nearSphere', - 'maxdistance' => 'maxDistance', - 'centersphere' => 'centerSphere', - 'uniquedocs' => 'uniqueDocs', + 'geowithin' => 'geoWithin', + 'nearsphere' => 'nearSphere', + 'maxdistance' => 'maxDistance', + 'centersphere' => 'centerSphere', + 'uniquedocs' => 'uniqueDocs', ]; if (array_key_exists($where['operator'], $convert)) { @@ -851,9 +854,7 @@ protected function compileWheres() foreach ($where['values'] as &$value) { $value = $this->convertKey($value); } - } - - // Single value. + } // Single value. elseif (isset($where['value'])) { $where['value'] = $this->convertKey($where['value']); } @@ -905,20 +906,18 @@ protected function compileWhereBasic($where) $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); // Convert like to regular expression. - if (! starts_with($value, '%')) { + if (!starts_with($value, '%')) { $regex = '^' . $regex; } - if (! ends_with($value, '%')) { + if (!ends_with($value, '%')) { $regex = $regex . '$'; } $value = new Regex($regex, 'i'); - } - - // Manipulate regexp operations. + } // Manipulate regexp operations. elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) { // Automatically convert regular expression strings to Regex objects. - if (! $value instanceof Regex) { + if (!$value instanceof Regex) { $e = explode('/', $value); $flag = end($e); $regstr = substr($value, 1, -(strlen($flag) + 1)); @@ -932,7 +931,7 @@ protected function compileWhereBasic($where) } } - if (! isset($operator) or $operator == '=') { + if (!isset($operator) or $operator == '=') { $query = [$column => $value]; } elseif (array_key_exists($operator, $this->conversion)) { $query = [$column => [$this->conversion[$operator] => $value]]; @@ -1017,8 +1016,8 @@ protected function compileWhereRaw($where) /** * Handle dynamic method calls into the method. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) From fb239b5322107bf03ea01f2bb56bc91e40ad1476 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 01:56:17 +0430 Subject: [PATCH 10/20] Reformat --- src/Jenssegers/Mongodb/Query/Builder.php | 197 ++++++++++++----------- 1 file changed, 104 insertions(+), 93 deletions(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 7504c123d..e7c48765e 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -69,12 +69,12 @@ class Builder extends BaseBuilder * @var array */ protected $conversion = [ - '=' => '=', + '=' => '=', '!=' => '$ne', '<>' => '$ne', - '<' => '$lt', + '<' => '$lt', '<=' => '$lte', - '>' => '$gt', + '>' => '$gt', '>=' => '$gte', ]; @@ -84,12 +84,12 @@ class Builder extends BaseBuilder * @var boolean */ protected $use_collection; - + /** * Create a new query builder instance. * * @param Connection $connection - * @param Processor $processor + * @param Processor $processor */ public function __construct(Connection $connection, Processor $processor) { @@ -103,7 +103,7 @@ public function __construct(Connection $connection, Processor $processor) /** * Set the projections. * - * @param array $columns + * @param array $columns * @return $this */ public function project($columns) @@ -142,8 +142,8 @@ public function hint($index) /** * Execute a query for a single record by ID. * - * @param mixed $id - * @param array $columns + * @param mixed $id + * @param array $columns * @return mixed */ public function find($id, $columns = []) @@ -154,7 +154,7 @@ public function find($id, $columns = []) /** * Execute the query as a "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function get($columns = []) @@ -165,7 +165,7 @@ public function get($columns = []) /** * Execute the query as a fresh "select" statement. * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function getFresh($columns = []) @@ -216,7 +216,8 @@ public function getFresh($columns = []) // Translate count into sum. if ($function == 'count') { $group['aggregate'] = ['$sum' => 1]; - } // Pass other functions directly. + } + // Pass other functions directly. else { $group['aggregate'] = ['$' . $function => '$' . $column]; } @@ -267,8 +268,10 @@ public function getFresh($columns = []) $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); // Return results - return $this->use_collection ? new Collection($results) : $results; - } // Distinct query + return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; + } + + // Distinct query elseif ($this->distinct) { // Return distinct results directly $column = isset($this->columns[0]) ? $this->columns[0] : '_id'; @@ -280,8 +283,10 @@ public function getFresh($columns = []) $result = $this->collection->distinct($column); } - return $this->use_collection ? new Collection($result) : $result; - } // Normal query + return SHOULD_RETURN_COLLECTION ? new Collection($result) : $result; + } + + // Normal query else { $columns = []; @@ -322,7 +327,7 @@ public function getFresh($columns = []) // Return results as an array with numeric keys $results = iterator_to_array($cursor, false); - return $this->use_collection ? new Collection($results) : $results; + return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; } } @@ -336,13 +341,13 @@ public function generateCacheKey() $key = [ 'connection' => $this->collection->getDatabaseName(), 'collection' => $this->collection->getCollectionName(), - 'wheres' => $this->wheres, - 'columns' => $this->columns, - 'groups' => $this->groups, - 'orders' => $this->orders, - 'offset' => $this->offset, - 'limit' => $this->limit, - 'aggregate' => $this->aggregate, + 'wheres' => $this->wheres, + 'columns' => $this->columns, + 'groups' => $this->groups, + 'orders' => $this->orders, + 'offset' => $this->offset, + 'limit' => $this->limit, + 'aggregate' => $this->aggregate, ]; return md5(serialize(array_values($key))); @@ -351,8 +356,8 @@ public function generateCacheKey() /** * Execute an aggregate function on the database. * - * @param string $function - * @param array $columns + * @param string $function + * @param array $columns * @return mixed */ public function aggregate($function, $columns = []) @@ -368,7 +373,7 @@ public function aggregate($function, $columns = []) $this->aggregate = null; if (isset($results[0])) { - $result = (array)$results[0]; + $result = (array) $results[0]; return $result['aggregate']; } @@ -381,7 +386,7 @@ public function aggregate($function, $columns = []) */ public function exists() { - return !is_null($this->first()); + return ! is_null($this->first()); } /** @@ -403,8 +408,8 @@ public function distinct($column = false) /** * Add an "order by" clause to the query. * - * @param string $column - * @param string $direction + * @param string $column + * @param string $direction * @return Builder */ public function orderBy($column, $direction = 'asc') @@ -425,10 +430,10 @@ public function orderBy($column, $direction = 'asc') /** * Add a where between statement to the query. * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not * @return Builder */ public function whereBetween($column, array $values, $boolean = 'and', $not = false) @@ -443,8 +448,8 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa /** * Set the limit and offset for a given page. * - * @param int $page - * @param int $perPage + * @param int $page + * @param int $perPage * @return \Illuminate\Database\Query\Builder|static */ public function forPage($page, $perPage = 15) @@ -457,7 +462,7 @@ public function forPage($page, $perPage = 15) /** * Insert a new record into the database. * - * @param array $values + * @param array $values * @return bool */ public function insert(array $values) @@ -469,34 +474,34 @@ public function insert(array $values) foreach ($values as $value) { // As soon as we find a value that is not an array we assume the user is // inserting a single document. - if (!is_array($value)) { + if (! is_array($value)) { $batch = false; break; } } - if (!$batch) { + if (! $batch) { $values = [$values]; } // Batch insert $result = $this->collection->insertMany($values); - return (1 == (int)$result->isAcknowledged()); + return (1 == (int) $result->isAcknowledged()); } /** * Insert a new record and get the value of the primary key. * - * @param array $values - * @param string $sequence + * @param array $values + * @param string $sequence * @return int */ public function insertGetId(array $values, $sequence = null) { $result = $this->collection->insertOne($values); - if (1 == (int)$result->isAcknowledged()) { + if (1 == (int) $result->isAcknowledged()) { if (is_null($sequence)) { $sequence = '_id'; } @@ -509,14 +514,14 @@ public function insertGetId(array $values, $sequence = null) /** * Update a record in the database. * - * @param array $values - * @param array $options + * @param array $values + * @param array $options * @return int */ public function update(array $values, array $options = []) { // Use $set as default operator. - if (!starts_with(key($values), '$')) { + if (! starts_with(key($values), '$')) { $values = ['$set' => $values]; } @@ -526,16 +531,16 @@ public function update(array $values, array $options = []) /** * Increment a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = [], array $options = []) { $query = ['$inc' => [$column => $amount]]; - if (!empty($extra)) { + if (! empty($extra)) { $query['$set'] = $extra; } @@ -552,9 +557,9 @@ public function increment($column, $amount = 1, array $extra = [], array $option /** * Decrement a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = [], array $options = []) @@ -565,28 +570,28 @@ public function decrement($column, $amount = 1, array $extra = [], array $option /** * Get an array with the values of a given column. * - * @param string $column - * @param string|null $key + * @param string $column + * @param string|null $key * @return array */ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - return $results->pluck($column, $key); + return $results->pluck($column,$key); } /** * Delete a record from the database. * - * @param mixed $id + * @param mixed $id * @return int */ public function delete($id = null) { $wheres = $this->compileWheres(); $result = $this->collection->DeleteMany($wheres); - if (1 == (int)$result->isAcknowledged()) { + if (1 == (int) $result->isAcknowledged()) { return $result->getDeletedCount(); } @@ -596,7 +601,7 @@ public function delete($id = null) /** * Set the collection which the query is targeting. * - * @param string $collection + * @param string $collection * @return Builder */ public function from($collection) @@ -615,15 +620,15 @@ public function truncate() { $result = $this->collection->drop(); - return (1 == (int)$result->ok); + return (1 == (int) $result->ok); } /** * Get an array with the values of a given column. * * @deprecated - * @param string $column - * @param string $key + * @param string $column + * @param string $key * @return array */ public function lists($column, $key = null) @@ -633,7 +638,7 @@ public function lists($column, $key = null) // Convert ObjectID's to strings so that lists can do its work. $results = $results->map(function ($item) { - $item['_id'] = (string)$item['_id']; + $item['_id'] = (string) $item['_id']; return $item; }); @@ -647,7 +652,7 @@ public function lists($column, $key = null) /** * Create a raw database expression. * - * @param closure $expression + * @param closure $expression * @return mixed */ public function raw($expression = null) @@ -655,8 +660,10 @@ public function raw($expression = null) // Execute the closure on the mongodb collection if ($expression instanceof Closure) { return call_user_func($expression, $this->collection); - } // Create an expression for the given value - elseif (!is_null($expression)) { + } + + // Create an expression for the given value + elseif (! is_null($expression)) { return new Expression($expression); } @@ -667,8 +674,8 @@ public function raw($expression = null) /** * Append one or more values to an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function push($column, $value = null, $unique = false) @@ -693,8 +700,8 @@ public function push($column, $value = null, $unique = false) /** * Remove one or more values from an array. * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function pull($column, $value = null) @@ -722,7 +729,7 @@ public function pull($column, $value = null) */ public function drop($columns) { - if (!is_array($columns)) { + if (! is_array($columns)) { $columns = [$columns]; } @@ -750,20 +757,20 @@ public function newQuery() /** * Perform an update query. * - * @param array $query - * @param array $options + * @param array $query + * @param array $options * @return int */ protected function performUpdate($query, array $options = []) { // Update multiple items by default. - if (!array_key_exists('multiple', $options)) { + if (! array_key_exists('multiple', $options)) { $options['multiple'] = true; } $wheres = $this->compileWheres(); $result = $this->collection->UpdateMany($wheres, $query, $options); - if (1 == (int)$result->isAcknowledged()) { + if (1 == (int) $result->isAcknowledged()) { return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount(); } @@ -788,10 +795,10 @@ public function convertKey($id) /** * Add a basic where clause to the query. * - * @param string $column - * @param string $operator - * @param mixed $value - * @param string $boolean + * @param string $column + * @param string $operator + * @param mixed $value + * @param string $boolean * @return \Illuminate\Database\Query\Builder|static * * @throws \InvalidArgumentException @@ -832,14 +839,14 @@ protected function compileWheres() // Operator conversions $convert = [ - 'regexp' => 'regex', - 'elemmatch' => 'elemMatch', + 'regexp' => 'regex', + 'elemmatch' => 'elemMatch', 'geointersects' => 'geoIntersects', - 'geowithin' => 'geoWithin', - 'nearsphere' => 'nearSphere', - 'maxdistance' => 'maxDistance', - 'centersphere' => 'centerSphere', - 'uniquedocs' => 'uniqueDocs', + 'geowithin' => 'geoWithin', + 'nearsphere' => 'nearSphere', + 'maxdistance' => 'maxDistance', + 'centersphere' => 'centerSphere', + 'uniquedocs' => 'uniqueDocs', ]; if (array_key_exists($where['operator'], $convert)) { @@ -854,7 +861,9 @@ protected function compileWheres() foreach ($where['values'] as &$value) { $value = $this->convertKey($value); } - } // Single value. + } + + // Single value. elseif (isset($where['value'])) { $where['value'] = $this->convertKey($where['value']); } @@ -906,18 +915,20 @@ protected function compileWhereBasic($where) $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); // Convert like to regular expression. - if (!starts_with($value, '%')) { + if (! starts_with($value, '%')) { $regex = '^' . $regex; } - if (!ends_with($value, '%')) { + if (! ends_with($value, '%')) { $regex = $regex . '$'; } $value = new Regex($regex, 'i'); - } // Manipulate regexp operations. + } + + // Manipulate regexp operations. elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) { // Automatically convert regular expression strings to Regex objects. - if (!$value instanceof Regex) { + if (! $value instanceof Regex) { $e = explode('/', $value); $flag = end($e); $regstr = substr($value, 1, -(strlen($flag) + 1)); @@ -931,7 +942,7 @@ protected function compileWhereBasic($where) } } - if (!isset($operator) or $operator == '=') { + if (! isset($operator) or $operator == '=') { $query = [$column => $value]; } elseif (array_key_exists($operator, $this->conversion)) { $query = [$column => [$this->conversion[$operator] => $value]]; @@ -1016,8 +1027,8 @@ protected function compileWhereRaw($where) /** * Handle dynamic method calls into the method. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) From b0e2a184fa0be06ee1c9cbc4ab6edb84e1d14ab0 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:02:07 +0430 Subject: [PATCH 11/20] Cleanup --- src/Jenssegers/Mongodb/Query/Builder.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index e7c48765e..bd883aace 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -4,7 +4,6 @@ use DateTime; use Illuminate\Database\Query\Builder as BaseBuilder; use Illuminate\Database\Query\Expression; -use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Jenssegers\Mongodb\Connection; use MongoDB\BSON\ObjectID; @@ -84,7 +83,7 @@ class Builder extends BaseBuilder * @var boolean */ protected $use_collection; - + /** * Create a new query builder instance. * @@ -578,7 +577,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - return $results->pluck($column,$key); + return $results->pluck($column, $key); } /** @@ -1039,4 +1038,4 @@ public function __call($method, $parameters) return parent::__call($method, $parameters); } -} +} \ No newline at end of file From ab5b384412327c0e2fee96517ccb3b011a1d85ea Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:09:29 +0430 Subject: [PATCH 12/20] Reformat and FIX --- src/Jenssegers/Mongodb/Eloquent/Builder.php | 48 +++++++++++---------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index fe03acf05..a9ca0778f 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -20,8 +20,8 @@ class Builder extends EloquentBuilder /** * Update a record in the database. * - * @param array $values - * @param array $options + * @param array $values + * @param array $options * @return int */ public function update(array $values, array $options = []) @@ -40,7 +40,7 @@ public function update(array $values, array $options = []) /** * Insert a new record into the database. * - * @param array $values + * @param array $values * @return bool */ public function insert(array $values) @@ -59,8 +59,8 @@ public function insert(array $values) /** * Insert a new record and get the value of the primary key. * - * @param array $values - * @param string $sequence + * @param array $values + * @param string $sequence * @return int */ public function insertGetId(array $values, $sequence = null) @@ -97,9 +97,9 @@ public function delete() /** * Increment a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function increment($column, $amount = 1, array $extra = []) @@ -127,9 +127,9 @@ public function increment($column, $amount = 1, array $extra = []) /** * Decrement a column's value by a given amount. * - * @param string $column - * @param int $amount - * @param array $extra + * @param string $column + * @param int $amount + * @param array $extra * @return int */ public function decrement($column, $amount = 1, array $extra = []) @@ -155,11 +155,11 @@ public function decrement($column, $amount = 1, array $extra = []) /** * Add the "has" condition where clause to the query. * - * @param \Illuminate\Database\Eloquent\Builder $hasQuery - * @param \Illuminate\Database\Eloquent\Relations\Relation $relation - * @param string $operator - * @param int $count - * @param string $boolean + * @param \Illuminate\Database\Eloquent\Builder $hasQuery + * @param \Illuminate\Database\Eloquent\Relations\Relation $relation + * @param string $operator + * @param int $count + * @param string $boolean * @return \Illuminate\Database\Eloquent\Builder */ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $operator, $count, $boolean) @@ -168,7 +168,7 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o // Get the number of related objects for each possible parent. $relationCount = array_count_values(array_map(function ($id) { - return (string) $id; // Convert Back ObjectIds to Strings + return (string)$id; // Convert Back ObjectIds to Strings }, $query->pluck($relation->getHasCompareKey()))); // Remove unwanted related objects based on the operator and count. @@ -209,7 +209,7 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o /** * Create a raw database expression. * - * @param closure $expression + * @param closure $expression * @return mixed */ public function raw($expression = null) @@ -221,13 +221,17 @@ public function raw($expression = null) if ($results instanceof Cursor) { $results = iterator_to_array($results, false); return $this->model->hydrate($results); - } // Convert Mongo BSONDocument to a single object. + } + + // Convert Mongo BSONDocument to a single object. elseif ($results instanceof BSONDocument) { $results = $results->getArrayCopy(); - return $this->model->newFromBuilder((array) $results); - } // The result is a single object. + return $this->model->newFromBuilder((array)$results); + } + + // The result is a single object. elseif (is_array($results) and array_key_exists('_id', $results)) { - return $this->model->newFromBuilder((array) $results); + return $this->model->newFromBuilder((array)$results); } return $results; From 0e62cdb31ab67c57b8386ed86b1e5c83c1b18bad Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:11:37 +0430 Subject: [PATCH 13/20] Reformat and FIX --- src/Jenssegers/Mongodb/Eloquent/Builder.php | 4 ++-- src/Jenssegers/Mongodb/Query/Builder.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index a9ca0778f..96399fda4 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -226,12 +226,12 @@ public function raw($expression = null) // Convert Mongo BSONDocument to a single object. elseif ($results instanceof BSONDocument) { $results = $results->getArrayCopy(); - return $this->model->newFromBuilder((array)$results); + return $this->model->newFromBuilder((array) $results); } // The result is a single object. elseif (is_array($results) and array_key_exists('_id', $results)) { - return $this->model->newFromBuilder((array)$results); + return $this->model->newFromBuilder((array) $results); } return $results; diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index bd883aace..ddbe8520c 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -267,7 +267,7 @@ public function getFresh($columns = []) $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); // Return results - return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; + return $this->use_collection ? new Collection($results) : $results; } // Distinct query @@ -282,7 +282,7 @@ public function getFresh($columns = []) $result = $this->collection->distinct($column); } - return SHOULD_RETURN_COLLECTION ? new Collection($result) : $result; + return $this->use_collection ? new Collection($result) : $result; } // Normal query @@ -326,7 +326,7 @@ public function getFresh($columns = []) // Return results as an array with numeric keys $results = iterator_to_array($cursor, false); - return SHOULD_RETURN_COLLECTION ? new Collection($results) : $results; + return $this->use_collection ? new Collection($results) : $results; } } @@ -1038,4 +1038,4 @@ public function __call($method, $parameters) return parent::__call($method, $parameters); } -} \ No newline at end of file +} From 80d7376454a8ef8427a30cb2d6baf7cd36718f09 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:14:33 +0430 Subject: [PATCH 14/20] Backward compability --- src/Jenssegers/Mongodb/Query/Builder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index ddbe8520c..8702c29d7 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -4,6 +4,7 @@ use DateTime; use Illuminate\Database\Query\Builder as BaseBuilder; use Illuminate\Database\Query\Expression; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Jenssegers\Mongodb\Connection; use MongoDB\BSON\ObjectID; @@ -577,7 +578,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - return $results->pluck($column, $key); + return $this->use_collection?$results->pluck($column, $key): Arr::pluck($results,$column,$key); } /** From e2322b1fbc4159b0a47a25259e3dd0cb6404a054 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:15:35 +0430 Subject: [PATCH 15/20] Listing Laravel 5.3 version Compatibility --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 36d8e4152..0bcae6671 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ composer require jenssegers/mongodb 5.0.x | 2.1.x 5.1.x | 2.2.x or 3.0.x 5.2.x | 2.3.x or 3.0.x + 5.3.x | 2.3.x or 3.0.x And add the service provider in `config/app.php`: From f86ba50918df6c5aa013fdfe2892056740978eb8 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:17:42 +0430 Subject: [PATCH 16/20] Style CI --- src/Jenssegers/Mongodb/Eloquent/Builder.php | 2 +- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index 96399fda4..745c73476 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -168,7 +168,7 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o // Get the number of related objects for each possible parent. $relationCount = array_count_values(array_map(function ($id) { - return (string)$id; // Convert Back ObjectIds to Strings + return (string) $id; // Convert Back ObjectIds to Strings }, $query->pluck($relation->getHasCompareKey()))); // Remove unwanted related objects based on the operator and count. diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 8702c29d7..195e1478c 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -578,7 +578,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - return $this->use_collection?$results->pluck($column, $key): Arr::pluck($results,$column,$key); + return $this->use_collection ? $results->pluck($column, $key) : Arr::pluck($results,$column,$key); } /** From 1f91e9d07a93d5da2dd23e7400b1bbd8ff8983d0 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:18:42 +0430 Subject: [PATCH 17/20] Style CI AGAIN :( --- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 195e1478c..482711aef 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -578,7 +578,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - return $this->use_collection ? $results->pluck($column, $key) : Arr::pluck($results,$column,$key); + return $this->use_collection ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key); } /** From fa8f9aa2bdc6cee9d8bf4e1761ad423deda72353 Mon Sep 17 00:00:00 2001 From: pi0 Date: Fri, 26 Aug 2016 02:21:10 +0430 Subject: [PATCH 18/20] 2.x won't work with 5.3.x --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bcae6671..8996c7aba 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ composer require jenssegers/mongodb 5.0.x | 2.1.x 5.1.x | 2.2.x or 3.0.x 5.2.x | 2.3.x or 3.0.x - 5.3.x | 2.3.x or 3.0.x + 5.3.x | 3.0.x And add the service provider in `config/app.php`: From dfc16134ef05b1df5679dc183a164a2552a9b517 Mon Sep 17 00:00:00 2001 From: pi0 Date: Sat, 27 Aug 2016 13:20:15 +0430 Subject: [PATCH 19/20] Better useCollections --- src/Jenssegers/Mongodb/Query/Builder.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 482711aef..8f81e9f40 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -83,7 +83,7 @@ class Builder extends BaseBuilder * * @var boolean */ - protected $use_collection; + protected $useCollections; /** * Create a new query builder instance. @@ -96,8 +96,7 @@ public function __construct(Connection $connection, Processor $processor) $this->grammar = new Grammar; $this->connection = $connection; $this->processor = $processor; - $s = explode('.', \Illuminate\Foundation\Application::VERSION); - $this->use_collection = (10 * $s[0] + $s[1]) >= 53; + $this->useCollections = version_compare(\Illuminate\Foundation\Application::VERSION, '5.3.0', '>='); } /** @@ -268,7 +267,7 @@ public function getFresh($columns = []) $results = iterator_to_array($this->collection->aggregate($pipeline, $options)); // Return results - return $this->use_collection ? new Collection($results) : $results; + return $this->useCollections ? new Collection($results) : $results; } // Distinct query @@ -283,7 +282,7 @@ public function getFresh($columns = []) $result = $this->collection->distinct($column); } - return $this->use_collection ? new Collection($result) : $result; + return $this->useCollections ? new Collection($result) : $result; } // Normal query @@ -327,7 +326,7 @@ public function getFresh($columns = []) // Return results as an array with numeric keys $results = iterator_to_array($cursor, false); - return $this->use_collection ? new Collection($results) : $results; + return $this->useCollections ? new Collection($results) : $results; } } @@ -578,7 +577,7 @@ public function pluck($column, $key = null) { $results = $this->get(is_null($key) ? [$column] : [$column, $key]); - return $this->use_collection ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key); + return $this->useCollections ? $results->pluck($column, $key) : Arr::pluck($results, $column, $key); } /** From 0f1421293ab20d8dfa4540659ea0cd2287e44029 Mon Sep 17 00:00:00 2001 From: pi0 Date: Sat, 27 Aug 2016 13:29:39 +0430 Subject: [PATCH 20/20] Version Compare Bug --- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 8f81e9f40..709c373ce 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -96,7 +96,7 @@ public function __construct(Connection $connection, Processor $processor) $this->grammar = new Grammar; $this->connection = $connection; $this->processor = $processor; - $this->useCollections = version_compare(\Illuminate\Foundation\Application::VERSION, '5.3.0', '>='); + $this->useCollections = version_compare(\Illuminate\Foundation\Application::VERSION, '5.3', '>='); } /**