Skip to content
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

Cleanup and tests #2567

Merged
merged 19 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ea2da7a
PHPORM-39: Add namespace for tests directory (#2)
alcaeus Jun 29, 2023
b6e8a44
Add classes to cast ObjectId and UUID instances (#1)
alcaeus Jun 29, 2023
bc364b5
PHPORM-44: Throw an exception when Query\Builder::push() is used inco…
GromNaN Jul 11, 2023
60a22f4
PHPORM-45 Add Query\Builder::toMql() to simplify comprehensive query …
GromNaN Jul 12, 2023
843904b
Create UTCDateTime from DateTimeInterface objects (#8)
GromNaN Jul 12, 2023
8562a4b
PHPORM-46 Throw an exception when Query\Builder::orderBy() is used wi…
GromNaN Jul 12, 2023
4658d54
PHPORM-51 Throw an exception when unsupported query builder method is…
GromNaN Jul 12, 2023
78319d4
Optimize calls to debug_backtrace (#11)
GromNaN Jul 13, 2023
bd86f85
Add header documentation for classes & traits that can be used in app…
GromNaN Jul 13, 2023
cf103ba
PHPORM-47 Improve Builder::whereBetween to support CarbonPeriod and r…
GromNaN Jul 19, 2023
9d9c7c8
PHPORM-49 Implement `Query\Builder::whereNot` by encapsulating into `…
GromNaN Jul 19, 2023
52c0ea3
PHPORM-49 Implement `Query\Builder::whereNot` by encapsulating into `…
GromNaN Jul 20, 2023
9cbadea
PHPORM-50 PHPORM-65 Remove call to deprecated Collection::count for c…
GromNaN Jul 26, 2023
03c58ea
PHPORM-67 Accept operators prefixed by $ in Query\Builder::orWhere (#20)
GromNaN Jul 26, 2023
b0b796c
PHPORM-33 Add tests on Query\Builder methods (#14)
GromNaN Jul 26, 2023
2824dc4
PHPORM-64 Remove Query\Builder::whereAll (#16)
GromNaN Jul 26, 2023
3a46876
PHPORM-68 Fix unique validator when the validated value is part of an…
GromNaN Jul 26, 2023
2319d53
PHPORM-53 Fix and test `like` and `regex` operators (#17)
GromNaN Jul 27, 2023
9f1f8f3
PHPORM-35 Add various tests on Model `_id` types (#22)
GromNaN Aug 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- Add classes to cast `ObjectId` and `UUID` instances [#1](https://github.com/GromNaN/laravel-mongodb-private/pull/1) by [@alcaeus](https://github.com/alcaeus).
- Add `Query\Builder::toMql()` to simplify comprehensive query tests [#6](https://github.com/GromNaN/laravel-mongodb-private/pull/6) by [@GromNaN](https://github.com/GromNaN).
- Fix `Query\Builder::whereNot` to use MongoDB [`$not`](https://www.mongodb.com/docs/manual/reference/operator/query/not/) operator [#13](https://github.com/GromNaN/laravel-mongodb-private/pull/13) by [@GromNaN](https://github.com/GromNaN).
- Fix `Query\Builder::whereBetween` to accept `Carbon\Period` object [#10](https://github.com/GromNaN/laravel-mongodb-private/pull/10) by [@GromNaN](https://github.com/GromNaN).
- Throw an exception for unsupported `Query\Builder` methods [#9](https://github.com/GromNaN/laravel-mongodb-private/pull/9) by [@GromNaN](https://github.com/GromNaN).
- Throw an exception when `Query\Builder::orderBy()` is used with invalid direction [#7](https://github.com/GromNaN/laravel-mongodb-private/pull/7) by [@GromNaN](https://github.com/GromNaN).
- Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN).
- Remove public property `Query\Builder::$paginating` [#15](https://github.com/GromNaN/laravel-mongodb-private/pull/15) by [@GromNaN](https://github.com/GromNaN).
- Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN).
- Accept operators prefixed by `$` in `Query\Builder::orWhere` [#20](https://github.com/GromNaN/laravel-mongodb-private/pull/20) by [@GromNaN](https://github.com/GromNaN).
- Remove `Query\Builder::whereAll($column, $values)`. Use `Query\Builder::where($column, 'all', $values)` instead. [#16](https://github.com/GromNaN/laravel-mongodb-private/pull/16) by [@GromNaN](https://github.com/GromNaN).
- Fix validation of unique values when the validated value is found as part of an existing value. [#21](https://github.com/GromNaN/laravel-mongodb-private/pull/21) by [@GromNaN](https://github.com/GromNaN).
- Support `%` and `_` in `like` expression [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).

## [3.9.2] - 2022-09-01

### Addded
### Added
- Add single word name mutators [#2438](https://github.com/jenssegers/laravel-mongodb/pull/2438) by [@RosemaryOrchard](https://github.com/RosemaryOrchard) & [@mrneatly](https://github.com/mrneatly).

### Fixed
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ $users =
->get();
```

**NOT statements**

```php
$users = User::whereNot('age', '>', 18)->get();
```

**whereIn**

```php
Expand Down Expand Up @@ -477,6 +483,17 @@ Car::where('weight', 300)

### MongoDB-specific operators

In addition to the Laravel Eloquent operators, all available MongoDB query operators can be used with `where`:

```php
User::where($fieldName, $operator, $value)->get();
```

It generates the following MongoDB filter:
```ts
{ $fieldName: { $operator: $value } }
```

**Exists**

Matches documents that have the specified field.
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
],
"license": "MIT",
"require": {
"ext-mongodb": "^1.15",
"illuminate/support": "^10.0",
"illuminate/container": "^10.0",
"illuminate/database": "^10.0",
Expand All @@ -36,11 +37,9 @@
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/models",
"tests/seeds"
]
"psr-4": {
"Jenssegers\\Mongodb\\Tests\\": "tests/"
}
},
"suggest": {
"jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB",
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function getPayload($email, $token)
return [
'email' => $email,
'token' => $this->hasher->make($token),
'created_at' => new UTCDateTime(Date::now()->format('Uv')),
'created_at' => new UTCDateTime(Date::now()),
];
}

Expand Down
3 changes: 3 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use MongoDB\BSON\ObjectID;
use MongoDB\Collection as MongoCollection;

/**
* @mixin MongoCollection
*/
class Collection
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use MongoDB\Database;
use Throwable;

/**
* @mixin Database
*/
class Connection extends BaseConnection
{
use ManagesTransactions;
Expand Down
63 changes: 63 additions & 0 deletions src/Eloquent/Casts/BinaryUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Jenssegers\Mongodb\Eloquent\Casts;

use function bin2hex;
use function hex2bin;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Jenssegers\Mongodb\Eloquent\Model;
use MongoDB\BSON\Binary;
use function str_replace;
use function substr;

class BinaryUuid implements CastsAttributes
{
/**
* Cast the given value.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if (! $value instanceof Binary || $value->getType() !== Binary::TYPE_UUID) {
return $value;
}

$base16Uuid = bin2hex($value->getData());

return sprintf(
'%s-%s-%s-%s-%s',
substr($base16Uuid, 0, 8),
substr($base16Uuid, 8, 4),
substr($base16Uuid, 12, 4),
substr($base16Uuid, 16, 4),
substr($base16Uuid, 20, 12),
);
}

/**
* Prepare the given value for storage.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return Binary
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value instanceof Binary) {
return $value;
}

if (is_string($value) && strlen($value) === 16) {
return new Binary($value, Binary::TYPE_UUID);
}

return new Binary(hex2bin(str_replace('-', '', $value)), Binary::TYPE_UUID);
}
}
46 changes: 46 additions & 0 deletions src/Eloquent/Casts/ObjectId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Jenssegers\Mongodb\Eloquent\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Jenssegers\Mongodb\Eloquent\Model;
use MongoDB\BSON\ObjectId as BSONObjectId;

class ObjectId implements CastsAttributes
{
/**
* Cast the given value.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if (! $value instanceof BSONObjectId) {
return $value;
}

return (string) $value;
}

/**
* Prepare the given value for storage.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value instanceof BSONObjectId) {
return $value;
}

return new BSONObjectId($value);
}
}
11 changes: 5 additions & 6 deletions src/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;

/**
* Embeds relations for MongoDB models.
*/
trait EmbedsRelations
{
/**
Expand All @@ -23,9 +26,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[, $caller] = debug_backtrace(false);

$relation = $caller['function'];
$relation = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

if ($localKey === null) {
Expand Down Expand Up @@ -58,9 +59,7 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[, $caller] = debug_backtrace(false);

$relation = $caller['function'];
$relation = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

if ($localKey === null) {
Expand Down
12 changes: 6 additions & 6 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use Jenssegers\Mongodb\Relations\MorphMany;
use Jenssegers\Mongodb\Relations\MorphTo;

/**
* Cross-database relationships between SQL and MongoDB.
* Use this trait in SQL models to define relationships with MongoDB models.
*/
trait HybridRelations
{
/**
Expand Down Expand Up @@ -134,9 +138,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[$current, $caller] = debug_backtrace(false, 2);

$relation = $caller['function'];
$relation = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

// Check if it is a relation with an original model.
Expand Down Expand Up @@ -178,9 +180,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if ($name === null) {
[$current, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$name = $caller['function'];
$name = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

[$type, $id] = $this->getMorphs(Str::snake($name), $type, $id);
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getDateFormat()
*/
public function freshTimestamp()
{
return new UTCDateTime(Date::now()->format('Uv'));
return new UTCDateTime(Date::now());
}

/**
Expand Down