Skip to content

Commit 5ecbf0b

Browse files
authored
Merge pull request #1300 from MCMatters/master
Add L5.5 support
2 parents c0cae3e + a411bfa commit 5ecbf0b

25 files changed

+172
-80
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.6
54
- 7
65
- 7.1
76

composer.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
1111
],
1212
"license" : "MIT",
1313
"require": {
14-
"illuminate/support": "^5.1",
15-
"illuminate/container": "^5.1",
16-
"illuminate/database": "^5.1",
17-
"illuminate/events": "^5.1",
14+
"illuminate/support": "^5.5",
15+
"illuminate/container": "^5.5",
16+
"illuminate/database": "^5.5",
17+
"illuminate/events": "^5.5",
1818
"mongodb/mongodb": "^1.0.0"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "^5.0|^6.0",
21+
"phpunit/phpunit": "^6.0",
2222
"orchestra/testbench": "^3.1",
2323
"mockery/mockery": "^0.9",
24-
"satooshi/php-coveralls": "^1.0"
24+
"satooshi/php-coveralls": "^1.0",
25+
"doctrine/dbal": "^2.5"
2526
},
2627
"autoload": {
2728
"psr-0": {

src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function tokenExpired($token)
2727
$date = $token['created_at']->toDateTime();
2828
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
2929
$token['created_at'] = $date->format('Y-m-d H:i:s');
30-
} elseif (is_array($token['created_at']) and isset($token['created_at']['date'])) {
30+
} elseif (is_array($token['created_at']) && isset($token['created_at']['date'])) {
3131
$date = new DateTime($token['created_at']['date'], new DateTimeZone(isset($token['created_at']['timezone']) ? $token['created_at']['timezone'] : 'UTC'));
3232
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
3333
$token['created_at'] = $date->format('Y-m-d H:i:s');

src/Jenssegers/Mongodb/Connection.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jenssegers\Mongodb;
44

55
use Illuminate\Database\Connection as BaseConnection;
6+
use Illuminate\Support\Arr;
67
use MongoDB\Client;
78

89
class Connection extends BaseConnection
@@ -34,7 +35,7 @@ public function __construct(array $config)
3435
$dsn = $this->getDsn($config);
3536

3637
// You can pass options directly to the MongoDB constructor
37-
$options = array_get($config, 'options', []);
38+
$options = Arr::get($config, 'options', []);
3839

3940
// Create the connection
4041
$this->connection = $this->createConnection($dsn, $config, $options);

src/Jenssegers/Mongodb/Eloquent/Builder.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,18 @@ public function raw($expression = null)
162162

163163
return $this->model->newFromBuilder((array) $results);
164164
} // The result is a single object.
165-
elseif (is_array($results) and array_key_exists('_id', $results)) {
165+
elseif (is_array($results) && array_key_exists('_id', $results)) {
166166
return $this->model->newFromBuilder((array) $results);
167167
}
168168

169169
return $results;
170170
}
171+
172+
/**
173+
* @return \Illuminate\Database\ConnectionInterface
174+
*/
175+
public function getConnection()
176+
{
177+
return $this->query->getConnection();
178+
}
171179
}

src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Jenssegers\Mongodb\Eloquent;
44

5+
use Illuminate\Support\Str;
56
use Jenssegers\Mongodb\Relations\EmbedsMany;
67
use Jenssegers\Mongodb\Relations\EmbedsOne;
78

@@ -32,7 +33,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
3233
}
3334

3435
if (is_null($foreignKey)) {
35-
$foreignKey = snake_case(class_basename($this));
36+
$foreignKey = Str::snake(class_basename($this));
3637
}
3738

3839
$query = $this->newQuery();
@@ -67,7 +68,7 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
6768
}
6869

6970
if (is_null($foreignKey)) {
70-
$foreignKey = snake_case(class_basename($this));
71+
$foreignKey = Str::snake(class_basename($this));
7172
}
7273

7374
$query = $this->newQuery();

src/Jenssegers/Mongodb/Eloquent/HybridRelations.php

+30-4
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,20 @@ public function morphTo($name = null, $type = null, $id = null)
214214
* @param string $collection
215215
* @param string $foreignKey
216216
* @param string $otherKey
217+
* @param string $parentKey
218+
* @param string $relatedKey
217219
* @param string $relation
218220
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
219221
*/
220-
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
221-
{
222+
public function belongsToMany(
223+
$related,
224+
$collection = null,
225+
$foreignKey = null,
226+
$otherKey = null,
227+
$parentKey = null,
228+
$relatedKey = null,
229+
$relation = null
230+
) {
222231
// If no relationship name was passed, we will pull backtraces to get the
223232
// name of the calling function. We will use that function name as the
224233
// title of this relation since that is a great convention to apply.
@@ -228,7 +237,15 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
228237

229238
// Check if it is a relation with an original model.
230239
if (!is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
231-
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
240+
return parent::belongsToMany(
241+
$related,
242+
$collection,
243+
$foreignKey,
244+
$otherKey,
245+
$parentKey,
246+
$relatedKey,
247+
$relation
248+
);
232249
}
233250

234251
// First, we'll need to determine the foreign key and "other key" for the
@@ -252,7 +269,16 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
252269
// appropriate query constraint and entirely manages the hydrations.
253270
$query = $instance->newQuery();
254271

255-
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
272+
return new BelongsToMany(
273+
$query,
274+
$this,
275+
$collection,
276+
$foreignKey,
277+
$otherKey,
278+
$parentKey ?: $this->getKeyName(),
279+
$relatedKey ?: $instance->getKeyName(),
280+
$relation
281+
);
256282
}
257283

258284
/**

src/Jenssegers/Mongodb/Eloquent/Model.php

+45-32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DateTime;
77
use Illuminate\Database\Eloquent\Model as BaseModel;
88
use Illuminate\Database\Eloquent\Relations\Relation;
9+
use Illuminate\Support\Arr;
910
use Illuminate\Support\Str;
1011
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1112
use MongoDB\BSON\ObjectID;
@@ -46,7 +47,7 @@ public function getIdAttribute($value = null)
4647
{
4748
// If we don't have a value for 'id', we will use the Mongo '_id' value.
4849
// This allows us to work with models in a more sql-like way.
49-
if (!$value and array_key_exists('_id', $this->attributes)) {
50+
if (!$value && array_key_exists('_id', $this->attributes)) {
5051
$value = $this->attributes['_id'];
5152
}
5253

@@ -131,12 +132,12 @@ public function getAttribute($key)
131132
}
132133

133134
// Dot notation support.
134-
if (str_contains($key, '.') and array_has($this->attributes, $key)) {
135+
if (Str::contains($key, '.') && Arr::has($this->attributes, $key)) {
135136
return $this->getAttributeValue($key);
136137
}
137138

138139
// This checks for embedded relation support.
139-
if (method_exists($this, $key) and !method_exists(self::class, $key)) {
140+
if (method_exists($this, $key) && !method_exists(self::class, $key)) {
140141
return $this->getRelationValue($key);
141142
}
142143

@@ -149,8 +150,8 @@ public function getAttribute($key)
149150
protected function getAttributeFromArray($key)
150151
{
151152
// Support keys in dot notation.
152-
if (str_contains($key, '.')) {
153-
return array_get($this->attributes, $key);
153+
if (Str::contains($key, '.')) {
154+
return Arr::get($this->attributes, $key);
154155
}
155156

156157
return parent::getAttributeFromArray($key);
@@ -162,17 +163,17 @@ protected function getAttributeFromArray($key)
162163
public function setAttribute($key, $value)
163164
{
164165
// Convert _id to ObjectID.
165-
if ($key == '_id' and is_string($value)) {
166+
if ($key == '_id' && is_string($value)) {
166167
$builder = $this->newBaseQueryBuilder();
167168

168169
$value = $builder->convertKey($value);
169170
} // Support keys in dot notation.
170-
elseif (str_contains($key, '.')) {
171+
elseif (Str::contains($key, '.')) {
171172
if (in_array($key, $this->getDates()) && $value) {
172173
$value = $this->fromDateTime($value);
173174
}
174175

175-
array_set($this->attributes, $key, $value);
176+
Arr::set($this->attributes, $key, $value);
176177

177178
return;
178179
}
@@ -199,8 +200,8 @@ public function attributesToArray()
199200

200201
// Convert dot-notation dates.
201202
foreach ($this->getDates() as $key) {
202-
if (str_contains($key, '.') and array_has($attributes, $key)) {
203-
array_set($attributes, $key, (string) $this->asDateTime(array_get($attributes, $key)));
203+
if (Str::contains($key, '.') && Arr::has($attributes, $key)) {
204+
Arr::set($attributes, $key, (string) $this->asDateTime(Arr::get($attributes, $key)));
204205
}
205206
}
206207

@@ -218,20 +219,36 @@ public function getCasts()
218219
/**
219220
* @inheritdoc
220221
*/
221-
protected function originalIsNumericallyEquivalent($key)
222+
protected function originalIsEquivalent($key, $current)
222223
{
223-
$current = $this->attributes[$key];
224-
$original = $this->original[$key];
224+
if (!array_key_exists($key, $this->original)) {
225+
return false;
226+
}
227+
228+
$original = $this->getOriginal($key);
229+
230+
if ($current === $original) {
231+
return true;
232+
}
233+
234+
if (null === $current) {
235+
return false;
236+
}
225237

226-
// Date comparison.
227-
if (in_array($key, $this->getDates())) {
238+
if ($this->isDateAttribute($key)) {
228239
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
229240
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
230241

231242
return $current == $original;
232243
}
233244

234-
return parent::originalIsNumericallyEquivalent($key);
245+
if ($this->hasCast($key)) {
246+
return $this->castAttribute($key, $current) ===
247+
$this->castAttribute($key, $original);
248+
}
249+
250+
return is_numeric($current) && is_numeric($original)
251+
&& strcmp((string) $current, (string) $original) === 0;
235252
}
236253

237254
/**
@@ -242,9 +259,7 @@ protected function originalIsNumericallyEquivalent($key)
242259
*/
243260
public function drop($columns)
244261
{
245-
if (!is_array($columns)) {
246-
$columns = [$columns];
247-
}
262+
$columns = Arr::wrap($columns);
248263

249264
// Unset attributes
250265
foreach ($columns as $column) {
@@ -263,16 +278,14 @@ public function push()
263278
if ($parameters = func_get_args()) {
264279
$unique = false;
265280

266-
if (count($parameters) == 3) {
281+
if (count($parameters) === 3) {
267282
list($column, $values, $unique) = $parameters;
268283
} else {
269284
list($column, $values) = $parameters;
270285
}
271286

272287
// Do batch push by default.
273-
if (!is_array($values)) {
274-
$values = [$values];
275-
}
288+
$values = Arr::wrap($values);
276289

277290
$query = $this->setKeysForSaveQuery($this->newQuery());
278291

@@ -294,9 +307,7 @@ public function push()
294307
public function pull($column, $values)
295308
{
296309
// Do batch pull by default.
297-
if (!is_array($values)) {
298-
$values = [$values];
299-
}
310+
$values = Arr::wrap($values);
300311

301312
$query = $this->setKeysForSaveQuery($this->newQuery());
302313

@@ -318,11 +329,11 @@ protected function pushAttributeValues($column, array $values, $unique = false)
318329

319330
foreach ($values as $value) {
320331
// Don't add duplicate values when we only want unique values.
321-
if ($unique and in_array($value, $current)) {
332+
if ($unique && (!is_array($current) || in_array($value, $current))) {
322333
continue;
323334
}
324335

325-
array_push($current, $value);
336+
$current[] = $value;
326337
}
327338

328339
$this->attributes[$column] = $current;
@@ -340,11 +351,13 @@ protected function pullAttributeValues($column, array $values)
340351
{
341352
$current = $this->getAttributeFromArray($column) ?: [];
342353

343-
foreach ($values as $value) {
344-
$keys = array_keys($current, $value);
354+
if (is_array($current)) {
355+
foreach ($values as $value) {
356+
$keys = array_keys($current, $value);
345357

346-
foreach ($keys as $key) {
347-
unset($current[$key]);
358+
foreach ($keys as $key) {
359+
unset($current[$key]);
360+
}
348361
}
349362
}
350363

0 commit comments

Comments
 (0)