Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nilportugues/eloquent-mongodb-repository

Eloquent MongoDB Repository Implementation
https://github.com/nilportugues/eloquent-mongodb-repository

composer crud database eloquent filter illuminate laravel laravel5 mongodb orm pageable paginate php php7 storage

Last synced: 2 months ago
JSON representation

Eloquent MongoDB Repository Implementation

Awesome Lists containing this project

README

        

# Eloquent MongoDB Repository
![PHP7 Tested](http://php-eye.com/badge/nilportugues/eloquent-mongodb-repository/php70.svg)
[![Build Status](https://travis-ci.org/PHPRepository/php-eloquent-mongodb-repository.svg)](https://travis-ci.org/PHPRepository/php-eloquent-mongodb-repository) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/php-eloquent-mongodb-repository/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nilportugues/php-eloquent-mongodb-repository/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/d993addb-38ad-4847-bd42-fa35fcfa0390/mini.png?gold)](https://insight.sensiolabs.com/projects/d993addb-38ad-4847-bd42-fa35fcfa0390) [![Latest Stable Version](https://poser.pugx.org/nilportugues/eloquent-mongodb-repository/v/stable?)](https://packagist.org/packages/nilportugues/eloquent-mongodb-repository) [![Total Downloads](https://poser.pugx.org/nilportugues/eloquent-mongodb-repository/downloads?)](https://packagist.org/packages/nilportugues/eloquent-mongodb-repository) [![License](https://poser.pugx.org/nilportugues/eloquent-mongodb-repository/license?)](https://packagist.org/packages/nilportugues/eloquent-mongodb-repository)
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues)

Eloquent MongoDB Repository using *[nilportugues/repository](https://github.com/nilportugues/php-repository)* as foundation, using *[jenssegers/mongodb](https://github.com/jenssegers/laravel-mongodb)*.

## Installation

Use [Composer](https://getcomposer.org) to install the package:

```json
$ composer require nilportugues/eloquent-mongodb-repository
```

## Why? Drivers and Multiple Implementations!

Using this implementation you can switch it out to test your code without setting up databases.

Doesn't sound handy? Let's think of yet another use case you'll love using this. `Functional tests` and `Unitary tests`.

No database connection will be needed, nor fakes. Using an `InMemoryRepository` or `FileSystemRepository` implementation will make those a breeze to code. And once the tests finish, all data may be destroyed with no worries at all.

**Available drivers:**

Also, if you feel like changing the repository implementation, no logic changes would be needed, as there are a set of drivers for you to use out of the box:

- `composer require nilportugues/repository-cache` for [caching](https://github.com/nilportugues/php-repository-cache).
- `composer require nilportugues/repository` for an [InMemoryRepository implementation](https://github.com/nilportugues/php-repository).
- `composer require nilportugues/filesystem-repository` for a [FileSystemRepository implementation](https://github.com/nilportugues/php-filesystem-repository).
- `composer require nilportugues/eloquent-repository` for a [SQL Eloquent implementation](https://github.com/nilportugues/php-eloquent-repository).
- `composer require nilportugues/doctrine-repository` for a [SQL Doctrine implementation](https://github.com/nilportugues/php-doctrine-repository).

## Usage

To set up Eloquent you don't need Laravel or Lumen frameworks at all. This is how you use Eloquent in any project.

```php
getDatabaseManager()->extend('mongodb', function($config) {
return new \Jenssegers\Mongodb\Connection($config);
});

$capsule->addConnection([
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'database' => 'default',
'username' => '',
'password' => '',
'options' => [
'db' => 'admin'
]
],
'default'
);
$capsule->bootEloquent();
$capsule->setAsGlobal();
```

Now that Eloquent is running, we can use the Repository.

### A note about MongoDB Models

The best way to ensure maximum compatibility with other Repository implementations is to **ignore** the MongoDB's ObjectId field: `_id` and declare one such as `id`. This means, not using MongoDB's ObjectId to fetch elements.

This also means that you'll need an Adapter build from MongoDB object the expected Business objects. More on that further down, or check the [/example](https://github.com/nilportugues/php-eloquent-repository/tree/master/example) directory.

### One Repository for One Eloquent Model

A well defined repository returns one kind of objects that belong to one Business model.

```php
userAdapter = $userAdapter;
}

/**
* {@inheritdoc}
*/
protected function modelClassName()
{
return User::class;
}

/**
* {@inheritdoc}
*/
public function find(Identity $id, Fields $fields = null)
{
$eloquentModel = parent::find($id, $fields);

return $this->userAdapter->fromEloquent($eloquentModel);
}

/**
* {@inheritdoc}
*/
public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
{
$eloquentModelArray = parent::findBy($filter, $sort, $fields);

return $this->fromEloquentArray($eloquentModelArray);
}

/**
* {@inheritdoc}
*/
public function findAll(Pageable $pageable = null)
{
$page = parent::findAll($pageable);

return new Page(
$this->fromEloquentArray($page->content()),
$page->totalElements(),
$page->pageNumber(),
$page->totalPages(),
$page->sortings(),
$page->filters(),
$page->fields()
);
}

/**
* {@inheritdoc}
*/
public function add(Identity $value)
{
$value = $this->userAdapter->toEloquent($value);

return parent::add($value);
}

/**
* {@inheritdoc}
*/
public function addAll(array $values)
{
$eloquent = [];
foreach ($values as $value) {
$eloquent[] = $this->userAdapter->toEloquent($value);
}

parent::addAll($eloquent);
}

/**
* @param array $eloquentModelArray
* @return array
*/
protected function fromEloquentArray(array $eloquentModelArray)
{
$results = [];
foreach ($eloquentModelArray as $eloquentModel) {
//This is required to handle findAll returning array, not objects.
$eloquentModel = (object) $eloquentModel;

$results[] = $this->userAdapter->fromEloquent($eloquentModel);
}

return $results;
}
}
```

A sample implementation can be found in the [/example](https://github.com/nilportugues/php-eloquent-repository/tree/master/example) directory.

### One EloquentRepository for All Eloquent Models

While **this is not the recommended way**, as a repository should only return one kind of Business objects, this works well with Laravel projects.

While the amount of code is less than the previous example, bare in mind that your code will be coupled with Eloquent.

```php
modelClass = (string) $modelClass;
}

/**
* {@inheritdoc}
*/
protected function modelClassName()
{
return $this->modelClass;
}
}
```

## Filtering data

Filtering is as simple as using the `Filter` object. For instance, lets retrieve how many users are named `Ken`.

```php
must()->contain('name', 'Ken');

echo $repository->count($filter);
```

Notice how the key `name` matches the database column `name` in the `users` table.

**Available options**

Filter allow you to use `must()`, `mustNot()` and `should()` methods to set up a fine-grained search. These provide a fluent interface with the following methods available:

- `public function notEmpty($filterName)`
- `public function hasEmpty($filterName)`
- `public function startsWith($filterName, $value)`
- `public function endsWith($filterName, $value)`
- `public function equal($filterName, $value)`
- `public function notEqual($filterName, $value)`
- `public function includeGroup($filterName, array $value)`
- `public function notIncludeGroup($filterName, array $value)`
- `public function range($filterName, $firstValue, $secondValue)`
- `public function notRange($filterName, $firstValue, $secondValue)`
- `public function notContain($filterName, $value)`
- `public function contain($filterName, $value)`
- `public function beGreaterThanOrEqual($filterName, $value)`
- `public function beGreaterThan($filterName, $value)`
- `public function beLessThanOrEqual($filterName, $value)`
- `public function beLessThan($filterName, $value)`

## Sorting data

Sorting is straight forward. Create an instance of Sort and pass in the column names and ordering.

```php
findBy($filter, $sort, $fields);
```

## Fields data

Create a Fields object to fetch only selected columns. If no Fields object is passed, all columns are selected by default.

```php
findBy($filter, $sort, $fields);
```

## Fetching data

Repository allows you to fetch data from the database by using the following methods:

- `public function findAll(Pageable $pageable = null)`
- `public function find(Identity $id, Fields $fields = null)`
- `public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)`

## Quality

To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.

This library attempts to comply with [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/), [PSR-4](http://www.php-fig.org/psr/psr-4/).

If you notice compliance oversights, please send a patch via [Pull Request](https://github.com/nilportugues/php-eloquent-repository/pulls).

## Contribute

Contributions to the package are always welcome!

* Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/php-eloquent-repository/issues/new).
* You can grab the source code at the package's [Git Repository](https://github.com/nilportugues/php-eloquent-repository).

## Support

Get in touch with me using one of the following means:

- Emailing me at
- Opening an [Issue](https://github.com/nilportugues/php-eloquent-repository/issues/new)

## Authors

* [Nil Portugués Calderó](http://nilportugues.com)
* [The Community Contributors](https://github.com/nilportugues/php-eloquent-repository/graphs/contributors)

## License
The code base is licensed under the [MIT license](LICENSE).