https://github.com/eman-development-design/mongodb-helpers
Helper Objects to make writing MongoDB apps easier.
https://github.com/eman-development-design/mongodb-helpers
composer-package helpers library mongodb php72
Last synced: 4 months ago
JSON representation
Helper Objects to make writing MongoDB apps easier.
- Host: GitHub
- URL: https://github.com/eman-development-design/mongodb-helpers
- Owner: eman-development-design
- License: mit
- Created: 2019-06-03T00:01:18.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-11-25T22:38:27.000Z (over 1 year ago)
- Last Synced: 2024-11-25T23:29:07.515Z (over 1 year ago)
- Topics: composer-package, helpers, library, mongodb, php72
- Language: PHP
- Size: 104 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# mongodb-helpers
Helper Objects to make writing MongoDB apps easier.
## Helpers
### Sorting Helper
You can easily add sorting by extending ```DataSorting```
```php
class UserSorting extends DataSorting
{
public const Email = 'Email';
public const FirstName = 'FirstName';
public const LastName = 'LastName';
}
```
you can then use it like so:
```php
UserSorting::AddToSortList(UserSorting::FirstName, UserSorting::ASC);
```
you then compile the sort list by doing this:
```php
$sort = UserSorting::GetSortList();
```
this wil allow you you add as many sorting rules as you want to a query.
### Model Helper
```MongoModelInterface``` is an interface helper that'll provide some helper methods for handling model data.
```php
use MongoDB\BSON\Serializable;
use MongoDB\Model\BSONDocument;
use Edd\MongoDbHelpers\MongoModelInterface
class User implements Serializable, MongoModelInterface
{
public $userGuid;
public $email;
public $firstName;
public $lastName;
public function bsonSerialize() : array
{
return [
'UserUuid' => $this->userUuid,
'Email' => $this->email,
'FirstName' => $this->firstName,
'LastName' => $this->lastName
];
}
public function map(BSONDocument $document)
{
$this->userUuid = $document['UserUuid'];
$this->email = $document['Email'];
$this->firstName = $document['FirstName'];
$this->lastName = $document['LastName'];
}
public function toArray() : array
{
return get_object_vars($this);
}
}
```
```map``` helps you map a BSON Document result to your model properties.
```toArray``` is for presenting the data as an array.
### Repository Helper
```MongoRepository``` makes it easy to setup a basic repository style architecture, the constructor will create an instance of ```MongoDB\Client``` .
```SetCollection``` & ```GetCollection``` setup and provide an instance of ```MongoDB\Collection```.