Skip to content

Add L5.5 support #1300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.6
- 7
- 7.1

Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
],
"license" : "MIT",
"require": {
"illuminate/support": "^5.1",
"illuminate/container": "^5.1",
"illuminate/database": "^5.1",
"illuminate/events": "^5.1",
"illuminate/support": "^5.5",
"illuminate/container": "^5.5",
"illuminate/database": "^5.5",
"illuminate/events": "^5.5",
"mongodb/mongodb": "^1.0.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0",
"phpunit/phpunit": "^6.0",
"orchestra/testbench": "^3.1",
"mockery/mockery": "^0.9",
"satooshi/php-coveralls": "^1.0"
"satooshi/php-coveralls": "^1.0",
"doctrine/dbal": "^2.5"
},
"autoload": {
"psr-0": {
Expand Down
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function tokenExpired($token)
$date = $token['created_at']->toDateTime();
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$token['created_at'] = $date->format('Y-m-d H:i:s');
} elseif (is_array($token['created_at']) and isset($token['created_at']['date'])) {
} elseif (is_array($token['created_at']) && isset($token['created_at']['date'])) {
$date = new DateTime($token['created_at']['date'], new DateTimeZone(isset($token['created_at']['timezone']) ? $token['created_at']['timezone'] : 'UTC'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$token['created_at'] = $date->format('Y-m-d H:i:s');
Expand Down
3 changes: 2 additions & 1 deletion src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jenssegers\Mongodb;

use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use MongoDB\Client;

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

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

// Create the connection
$this->connection = $this->createConnection($dsn, $config, $options);
Expand Down
10 changes: 9 additions & 1 deletion src/Jenssegers/Mongodb/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,18 @@ public function raw($expression = null)

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

return $results;
}

/**
* @return \Illuminate\Database\ConnectionInterface
*/
public function getConnection()
{
return $this->query->getConnection();
}
}
5 changes: 3 additions & 2 deletions src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jenssegers\Mongodb\Eloquent;

use Illuminate\Support\Str;
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;

Expand Down Expand Up @@ -32,7 +33,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
}

if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
$foreignKey = Str::snake(class_basename($this));
}

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

if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
$foreignKey = Str::snake(class_basename($this));
}

$query = $this->newQuery();
Expand Down
34 changes: 30 additions & 4 deletions src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,20 @@ public function morphTo($name = null, $type = null, $id = null)
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
{
public function belongsToMany(
$related,
$collection = null,
$foreignKey = null,
$otherKey = null,
$parentKey = null,
$relatedKey = null,
$relation = null
) {
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply.
Expand All @@ -228,7 +237,15 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,

// Check if it is a relation with an original model.
if (!is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
return parent::belongsToMany(
$related,
$collection,
$foreignKey,
$otherKey,
$parentKey,
$relatedKey,
$relation
);
}

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

return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
return new BelongsToMany(
$query,
$this,
$collection,
$foreignKey,
$otherKey,
$parentKey ?: $this->getKeyName(),
$relatedKey ?: $instance->getKeyName(),
$relation
);
}

/**
Expand Down
77 changes: 45 additions & 32 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTime;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use MongoDB\BSON\ObjectID;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function getIdAttribute($value = null)
{
// If we don't have a value for 'id', we will use the Mongo '_id' value.
// This allows us to work with models in a more sql-like way.
if (!$value and array_key_exists('_id', $this->attributes)) {
if (!$value && array_key_exists('_id', $this->attributes)) {
$value = $this->attributes['_id'];
}

Expand Down Expand Up @@ -131,12 +132,12 @@ public function getAttribute($key)
}

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

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

Expand All @@ -149,8 +150,8 @@ public function getAttribute($key)
protected function getAttributeFromArray($key)
{
// Support keys in dot notation.
if (str_contains($key, '.')) {
return array_get($this->attributes, $key);
if (Str::contains($key, '.')) {
return Arr::get($this->attributes, $key);
}

return parent::getAttributeFromArray($key);
Expand All @@ -162,17 +163,17 @@ protected function getAttributeFromArray($key)
public function setAttribute($key, $value)
{
// Convert _id to ObjectID.
if ($key == '_id' and is_string($value)) {
if ($key == '_id' && is_string($value)) {
$builder = $this->newBaseQueryBuilder();

$value = $builder->convertKey($value);
} // Support keys in dot notation.
elseif (str_contains($key, '.')) {
elseif (Str::contains($key, '.')) {
if (in_array($key, $this->getDates()) && $value) {
$value = $this->fromDateTime($value);
}

array_set($this->attributes, $key, $value);
Arr::set($this->attributes, $key, $value);

return;
}
Expand All @@ -199,8 +200,8 @@ public function attributesToArray()

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

Expand All @@ -218,20 +219,36 @@ public function getCasts()
/**
* @inheritdoc
*/
protected function originalIsNumericallyEquivalent($key)
protected function originalIsEquivalent($key, $current)
{
$current = $this->attributes[$key];
$original = $this->original[$key];
if (!array_key_exists($key, $this->original)) {
return false;
}

$original = $this->getOriginal($key);

if ($current === $original) {
return true;
}

if (null === $current) {
return false;
}

// Date comparison.
if (in_array($key, $this->getDates())) {
if ($this->isDateAttribute($key)) {
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;

return $current == $original;
}

return parent::originalIsNumericallyEquivalent($key);
if ($this->hasCast($key)) {
return $this->castAttribute($key, $current) ===
$this->castAttribute($key, $original);
}

return is_numeric($current) && is_numeric($original)
&& strcmp((string) $current, (string) $original) === 0;
}

/**
Expand All @@ -242,9 +259,7 @@ protected function originalIsNumericallyEquivalent($key)
*/
public function drop($columns)
{
if (!is_array($columns)) {
$columns = [$columns];
}
$columns = Arr::wrap($columns);

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

if (count($parameters) == 3) {
if (count($parameters) === 3) {
list($column, $values, $unique) = $parameters;
} else {
list($column, $values) = $parameters;
}

// Do batch push by default.
if (!is_array($values)) {
$values = [$values];
}
$values = Arr::wrap($values);

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

Expand All @@ -294,9 +307,7 @@ public function push()
public function pull($column, $values)
{
// Do batch pull by default.
if (!is_array($values)) {
$values = [$values];
}
$values = Arr::wrap($values);

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

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

foreach ($values as $value) {
// Don't add duplicate values when we only want unique values.
if ($unique and in_array($value, $current)) {
if ($unique && (!is_array($current) || in_array($value, $current))) {
continue;
}

array_push($current, $value);
$current[] = $value;
}

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

foreach ($values as $value) {
$keys = array_keys($current, $value);
if (is_array($current)) {
foreach ($values as $value) {
$keys = array_keys($current, $value);

foreach ($keys as $key) {
unset($current[$key]);
foreach ($keys as $key) {
unset($current[$key]);
}
}
}

Expand Down
Loading