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

Align master with develop #126

Merged
merged 16 commits into from
May 31, 2018
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ php:
- 7.1

before_install:
- if [[ $TRAVIS_PHP_VERSION = 7.0 ]]; then pecl install mongodb; fi
- pecl install mongodb
- echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
],
"require": {
"php": ">=7.0",
"mongodb/mongodb": "^1.1",
"mongodb/mongodb": "^1.3",
"illuminate/container": "^5.4"
},
"require-dev": {
"mockery/mockery": "^0.9",
"mockery/mockery": "^1.0",
"satooshi/php-coveralls": "^1.0",
"phpunit/phpunit": "^5.7",
"phpunit/phpunit": "^6.4",
"squizlabs/php_codesniffer": "^2.0",
"sami/sami": "^4.0"
},
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
Expand Down
2 changes: 1 addition & 1 deletion src/Mongolid/Connection/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Pool
/**
* Opened connections.
*
* @var SplQueue
* @var \SplQueue
*/
protected $connections;

Expand Down
2 changes: 1 addition & 1 deletion src/Mongolid/Cursor/CacheableCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CacheableCursor extends Cursor
* Limit of the query. It is stored because when caching the documents
* the DOCUMENT_LIMIT const will be used.
*
* @var ìnteger
* @var int
*/
protected $originalLimit;

Expand Down
6 changes: 3 additions & 3 deletions src/Mongolid/DataMapper/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mongolid\DataMapper;

use MongoDB\BSON\ObjectID;
use MongoDB\BSON\ObjectId;
use MongoDB\Driver\BulkWrite as MongoBulkWrite;
use MongoDB\Driver\WriteConcern;
use Mongolid\Connection\Pool;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function setBulkWrite(MongoBulkWrite $bulkWrite)
*
* @see https://docs.mongodb.com/manual/reference/operator/update/set/#set-top-level-fields
*
* @param ObjectID|string $id
* @param ObjectId|string $id
* @param array $dataToSet
* @param array $options
* @param string $operator
Expand Down Expand Up @@ -117,7 +117,7 @@ public function execute($writeConcern = 1)
return $manager->executeBulkWrite(
$namespace,
$this->getBulkWrite(),
new WriteConcern($writeConcern)
['writeConcern' => new WriteConcern($writeConcern)]
);
}
}
18 changes: 9 additions & 9 deletions src/Mongolid/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Mongolid\DataMapper;

use InvalidArgumentException;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\ObjectId;
use MongoDB\Collection;
use Mongolid\Connection\Pool;
use Mongolid\Container\Ioc;
Expand Down Expand Up @@ -83,7 +83,7 @@ public function save($entity, array $options = [])
// If the "saving" event returns false we'll bail out of the save and return
// false, indicating that the save failed. This gives an opportunities to
// listeners to cancel save operations if validations fail or whatever.
if ($this->fireEvent('saving', $entity, true) === false) {
if (false === $this->fireEvent('saving', $entity, true)) {
return false;
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public function save($entity, array $options = [])
*/
public function insert($entity, array $options = [], bool $fireEvents = true): bool
{
if ($fireEvents && $this->fireEvent('inserting', $entity, true) === false) {
if ($fireEvents && false === $this->fireEvent('inserting', $entity, true)) {
return false;
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public function insert($entity, array $options = [], bool $fireEvents = true): b
*/
public function update($entity, array $options = []): bool
{
if ($this->fireEvent('updating', $entity, true) === false) {
if (false === $this->fireEvent('updating', $entity, true)) {
return false;
}

Expand Down Expand Up @@ -205,7 +205,7 @@ public function update($entity, array $options = []): bool
*/
public function delete($entity, array $options = []): bool
{
if ($this->fireEvent('deleting', $entity, true) === false) {
if (false === $this->fireEvent('deleting', $entity, true)) {
return false;
}

Expand Down Expand Up @@ -393,7 +393,7 @@ protected function prepareValueQuery($value): array
is_string($value['_id']) &&
ObjectIdUtils::isObjectId($value['_id'])
) {
$value['_id'] = new ObjectID($value['_id']);
$value['_id'] = new ObjectId($value['_id']);
}

if (isset($value['_id']) &&
Expand All @@ -406,7 +406,7 @@ protected function prepareValueQuery($value): array
}

/**
* Prepares an embedded array of an query. It will convert string ObjectIDs
* 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
Expand All @@ -421,7 +421,7 @@ protected function prepareArrayFieldOfQuery(array $value): array
) {
foreach ($value[$operator] as $index => $id) {
if (ObjectIdUtils::isObjectId($id)) {
$value[$operator][$index] = new ObjectID($id);
$value[$operator][$index] = new ObjectId($id);
}
}
}
Expand Down Expand Up @@ -504,7 +504,7 @@ protected function prepareProjection(array $fields)

if (is_int($key) && is_string($value)) {
$key = $value;
if (strpos($value, '-') === 0) {
if (0 === strpos($value, '-')) {
$key = substr($key, 1);
$value = false;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Mongolid/DataMapper/EntityAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function assemble($document, Schema $schema)
foreach ($document as $field => $value) {
$fieldType = $schema->fields[$field] ?? null;

if ($fieldType && substr($fieldType, 0, 7) == 'schema.') {
if ($fieldType && 'schema.' == substr($fieldType, 0, 7)) {
$value = $this->assembleDocumentsRecursively($value, substr($fieldType, 7));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mongolid/DataMapper/SchemaMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function parseField($value, string $fieldType)
}

// If the field type points to another schema.
if (substr($fieldType, 0, 7) == 'schema.') {
if ('schema.' == substr($fieldType, 0, 7)) {
return $this->mapToSchema($value, substr($fieldType, 7));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mongolid/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Manager
/**
* Container being used by Mongolid.
*
* @var \Illuminate\Contracts\Container
* @var \Illuminate\Contracts\Container\Container
*/
public $container;

Expand Down
2 changes: 1 addition & 1 deletion src/Mongolid/Model/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getAttribute(string $key)

if ($inAttributes) {
return $this->attributes[$key];
} elseif ($key == 'attributes') {
} elseif ('attributes' == $key) {
return $this->attributes;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Mongolid/Model/DocumentEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mongolid\Model;

use MongoDB\BSON\ObjectID;
use MongoDB\BSON\ObjectId;

/**
* Document embeder is a service that will embed documents within each other.
Expand Down Expand Up @@ -115,7 +115,7 @@ public function detach($parent, string $field, &$entity): bool
*
* @param mixed $object the object|array that the _id will be retrieved from
*
* @return ObjectID|mixed
* @return ObjectId|mixed
*/
protected function getId(&$object)
{
Expand All @@ -124,15 +124,15 @@ protected function getId(&$object)
return $object['_id'];
}

return $object['_id'] = new ObjectID();
return $object['_id'] = new ObjectId();
}

if (is_object($object) && !$object instanceof ObjectID) {
if (is_object($object) && !$object instanceof ObjectId) {
if (isset($object->_id) && $object->_id) {
return $object->_id;
}

return $object->_id = new ObjectID();
return $object->_id = new ObjectId();
}

return $object;
Expand Down
26 changes: 14 additions & 12 deletions src/Mongolid/Model/Relations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mongolid\Model;

use MongoDB\BSON\ObjectID;
use MongoDB\BSON\ObjectId;
use Mongolid\Container\Ioc;
use Mongolid\Cursor\CursorFactory;
use Mongolid\Cursor\EmbeddedCursor;
Expand All @@ -18,12 +18,13 @@ trait Relations
/**
* Returns the referenced documents as objects.
*
* @param string $entity class of the entity or of the schema of the entity
* @param string $field the field where the _id is stored
* @param string $entity class of the entity or of the schema of the entity
* @param string $field the field where the _id is stored
* @param bool $cacheable retrieves a CacheableCursor instead
*
* @return mixed
*/
protected function referencesOne(string $entity, string $field)
protected function referencesOne(string $entity, string $field, bool $cacheable = true)
{
$referenced_id = $this->$field;

Expand All @@ -37,27 +38,28 @@ protected function referencesOne(string $entity, string $field)
$dataMapper = Ioc::make(DataMapper::class);
$dataMapper->setSchema($entityInstance);

return $dataMapper->first(['_id' => $referenced_id], [], true);
return $dataMapper->first(['_id' => $referenced_id], [], $cacheable);
}

return $entityInstance::first(['_id' => $referenced_id], [], true);
return $entityInstance::first(['_id' => $referenced_id], [], $cacheable);
}

/**
* Returns the cursor for the referenced documents as objects.
*
* @param string $entity class of the entity or of the schema of the entity
* @param string $field the field where the _ids are stored
* @param string $entity class of the entity or of the schema of the entity
* @param string $field the field where the _ids are stored
* @param bool $cacheable retrieves a CacheableCursor instead
*
* @return array
*/
protected function referencesMany(string $entity, string $field)
protected function referencesMany(string $entity, string $field, bool $cacheable = true)
{
$referencedIds = (array) $this->$field;

if (ObjectIdUtils::isObjectId($referencedIds[0] ?? '')) {
foreach ($referencedIds as $key => $value) {
$referencedIds[$key] = new ObjectID($value);
$referencedIds[$key] = new ObjectId($value);
}
}

Expand All @@ -69,10 +71,10 @@ protected function referencesMany(string $entity, string $field)
$dataMapper = Ioc::make(DataMapper::class);
$dataMapper->setSchema($entityInstance);

return $dataMapper->where($query, [], true);
return $dataMapper->where($query, [], $cacheable);
}

return $entityInstance::where($query, [], true);
return $entityInstance::where($query, [], $cacheable);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Mongolid/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mongolid\Schema;

use MongoDB\BSON\ObjectID;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use Mongolid\Container\Ioc;
use Mongolid\Util\ObjectIdUtils;
Expand Down Expand Up @@ -58,20 +58,20 @@ abstract class Schema

/**
* Filters any field in the $fields that has it's value specified as a
* 'objectId'. It will wraps the $value, if any, into a ObjectID object.
* 'objectId'. It will wraps the $value, if any, into a ObjectId object.
*
* @param mixed $value value that may be converted to ObjectID
* @param mixed $value value that may be converted to ObjectId
*
* @return ObjectID|mixed
* @return ObjectId|mixed
*/
public function objectId($value = null)
{
if ($value === null) {
return new ObjectID();
if (null === $value) {
return new ObjectId();
}

if (is_string($value) && ObjectIdUtils::isObjectId($value)) {
$value = new ObjectID($value);
$value = new ObjectId($value);
}

return $value;
Expand Down Expand Up @@ -109,7 +109,7 @@ public function createdAtTimestamp($value)
return $value;
}

return $this->updatedAtTimestamp(null);
return $this->updatedAtTimestamp();
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Mongolid/Util/ObjectIdUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public static function isObjectId($value)
$value = (string) $value;
}

if (is_string($value) && strlen($value) == 24 && ctype_xdigit($value)) {
return true;
}

return false;
return is_string($value) && 24 == strlen($value) && ctype_xdigit($value);
}
}
2 changes: 1 addition & 1 deletion tests/Mongolid/DataMapper/BulkWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function testShouldExecuteBulkWrite()

$manager->shouldReceive('executeBulkWrite')
->once()
->with($namespace, $mongoBulkWrite, m::type(WriteConcern::class))
->with($namespace, $mongoBulkWrite, ['writeConcern' => new WriteConcern(1)])
->andReturn(true);

$bulkWrite = m::mock(BulkWrite::class.'[getBulkWrite]', [$entity]);
Expand Down
Loading