-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e3d3cd
commit ca3e27f
Showing
7 changed files
with
234 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Mongolid\Util; | ||
|
||
use MongoDB\BSON\ObjectId; | ||
use Mongolid\Model\ModelInterface; | ||
|
||
class QueryBuilder | ||
{ | ||
public static function resolveQuery($query, ModelInterface $model): array | ||
{ | ||
$query = self::prepareValueQuery($query); | ||
|
||
return self::addSoftDeleteFilterIfRequired($query, $model); | ||
} | ||
|
||
public static function getDeletedAtColumn(ModelInterface $model): string | ||
{ | ||
return defined( | ||
$model::class . '::DELETED_AT' | ||
) | ||
? $model::DELETED_AT | ||
: 'deleted_at'; | ||
} | ||
|
||
/** | ||
* Transforms a value that is not an array into an MongoDB query (array). | ||
* This method will take care of converting a single value into a query for | ||
* an _id, including when a objectId is passed as a string. | ||
* | ||
* @param mixed $value the _id of the document | ||
* | ||
* @return array Query for the given _id | ||
*/ | ||
public static function prepareValueQuery($value): array | ||
{ | ||
if (!is_array($value)) { | ||
$value = ['_id' => $value]; | ||
} | ||
|
||
if ( | ||
isset($value['_id']) && | ||
is_string($value['_id']) && | ||
ObjectIdUtils::isObjectId($value['_id']) | ||
) { | ||
$value['_id'] = new ObjectId($value['_id']); | ||
} | ||
|
||
if ( | ||
isset($value['_id']) && | ||
is_array($value['_id']) | ||
) { | ||
$value['_id'] = self::prepareArrayFieldOfQuery($value['_id']); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
private static function addSoftDeleteFilterIfRequired(array $query, ModelInterface $model): array | ||
{ | ||
$field = self::getDeletedAtColumn($model); | ||
|
||
if (isset($query['withTrashed'])) { | ||
unset($query['withTrashed']); | ||
|
||
return $query; | ||
} | ||
|
||
return array_merge( | ||
$query, | ||
[ | ||
'$or' => [ | ||
[ | ||
$field => null, | ||
], | ||
[ | ||
$field => ['$exists' => false], | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Prepares an embedded array of an query. It will convert string ObjectIds | ||
* in operators into actual objects. | ||
* | ||
* @param array $value array that will be treated | ||
* | ||
* @return array prepared array | ||
*/ | ||
private static function prepareArrayFieldOfQuery(array $value): array | ||
{ | ||
foreach (['$in', '$nin'] as $operator) { | ||
if ( | ||
isset($value[$operator]) && | ||
is_array($value[$operator]) | ||
) { | ||
foreach ($value[$operator] as $index => $id) { | ||
if (ObjectIdUtils::isObjectId($id)) { | ||
$value[$operator][$index] = new ObjectId($id); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.