Git Product home page Git Product logo

repository's Introduction

Repository

PHP7 Tested Build Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate

Generic implementation and definition of a Repository and its in-memory implementation.

Installation

Use Composer to install the package:

$ composer require nilportugues/repository

InMemory Implementation

A custom repository can be easily created by extending the InMemoryRepository class provided.

use NilPortugues\Foundation\Infrastructure\Model\Repository\InMemory\InMemoryRepository

class MyInMemoryRepository extends InMemoryRepository
{
    //... your custom implementation.
}

Implementation can be seen here.

The base InMemoryRepository implements the following interfaces:

  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\Repository
  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository
  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository
  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\WriteRepository

InMemoryRepository Example

An example with a complete implementation can be found in the /example directory.

In the example:

  • Colors are defined as a class implementing the Identity interface.
  • A ColorRepository is implemented. Will throw exception if Color is not found.
  • Examples on how to filter are provided in the example.php file.

Foundation Classes

Interaction with the repository requires the usage of the following classes or classes implementing interfaces.

  • NilPortugues\Foundation\Domain\Model\Repository\Fields

    • public function __construct(array $fields = [])
    • public function add($field)
    • public function get()
  • NilPortugues\Foundation\Domain\Model\Repository\Filter

    • public function filters()
    • public function must()
    • public function mustNot()
    • public function should()
    • public function clear()
  • NilPortugues\Foundation\Domain\Model\Repository\BaseFilter

    • public function notStartsWith($filterName, $value)
    • public function notEndsWith($filterName, $value)
    • public function notEmpty($filterName)
    • public function empty($filterName)
    • public function notNull($filterName)
    • public function empty($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)
    • public function clear()
    • public function get()
    • public function hasEmpty($filterName) //alias of empty() for BC reasons.
  • NilPortugues\Foundation\Domain\Model\Repository\Order

    • public function __construct($direction)
    • public function isDescending()
    • public function isAscending()
    • public function __toString()
    • public function equals($object)
    • public function direction()
  • NilPortugues\Foundation\Domain\Model\Repository\Pageable

    • public function __construct($pageNumber, $pageSize, SortInterface $sort = null, FilterInterface $filter = null, FieldsInterface $fields = null)
    • public function offset()
    • public function pageNumber()
    • public function sortings()
    • public function next()
    • public function pageSize()
    • public function previousOrFirst()
    • public function hasPrevious()
    • public function first()
    • public function filters()
    • public function fields()
  • NilPortugues\Foundation\Domain\Model\Repository\Page

    • public function __construct(array $elements, $totalElements, $pageNumber, $totalPages, SortInterface $sort = null, FilterInterface $filter = null, FieldsInterface $fields = null)
    • public function content()
    • public function hasPrevious()
    • public function isFirst()
    • public function isLast()
    • public function hasNext()
    • public function pageSize()
    • public function pageNumber()
    • public function totalPages()
    • public function nextPageable()
    • public function sortings()
    • public function filters()
    • public function fields()
    • public function previousPageable()
    • public function totalElements()
    • public function map(callable $converter)
  • NilPortugues\Foundation\Domain\Model\Repository\Sort

    • public function __construct(array $properties = [], OrderInterface $order = null)
    • public function andSort(SortInterface $sort)
    • public function orders()
    • public function equals(SortInterface $sort)
    • public function orderFor($propertyName)
    • public function setOrderFor($propertyName, OrderInterface $order)
    • public function property($propertyName)

Interfaces

  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity

    • public function id()
    • public function __toString()
  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\Repository

    • public function count(Filter $filter = null)
    • public function exists(Identity $id)
  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository

    • public function findAll(Pageable $pageable = null)
  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository

    • public function find(Identity $id, Fields $fields = null)
    • public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
    • public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null, Fields $fields = null)
  • NilPortugues\Foundation\Domain\Model\Repository\Contracts\WriteRepository

    • public function add($value)
    • public function addAll(array $values)
    • public function remove(Identity $id)
    • public function removeAll(Filter $filter = null)
    • public function transactional(callable $transaction)

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, PSR-2, PSR-4.

If you notice compliance oversights, please send a patch via Pull Request.

Contribute

Contributions to the package are always welcome!

Support

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

Authors

License

The code base is licensed under the MIT license.

repository's People

Contributors

flavioheleno avatar nilportugues avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

repository's Issues

Implement Null Objects

All "property" objects need to implement its NullObject counterpart as they might be Null.

All Objects being initialitzed in a method as null are candidates to implement this.

Fix InMemory Filtering

  • InMemoryFilter::must should act upon the filtered data on every iteration (real AND behaviour)
  • InMemoryFilter::mustNot should act upon the filtered data on every iteration (real AND NOT behaviour)
  • InMemoryFilter::should should act upon the result set on every iteration (real OR behaviour )

[Insight] Unused method, property, variable or parameter - in src/…/Filters/RangeFilter.php, line 53

in src/Infrastructure/Model/Repository/InMemory/Filters/RangeFilter.php, line 53

This types local variable is declared but never used. You should remove it.

     *
     * @throws Exception
     */
    private static function sameTypeGuard($v, $value1, $value2)
    {
        if (1 !== count(array_unique($types = [is_scalar($value1), is_scalar($value2), is_scalar($v)]))) {
            throw new Exception('Range values and property values should be of the same type, either scalars or objects.');
        }
    }
}

Posted from SensioLabsInsight

Take mnapoli ideas into account

Ideas from @mnapoli over reddit, http://mnapoli.fr/repository-interface/

Eric Evans:
A repository represents all objects of a certain type as a conceptual set (usually emulated). It acts like a collection, except with more elaborate querying capability. […] For each type of object that needs global access, create an object that can provide the illusion of an in-memory collection of all objects of that type.

We could benefit of being more 'Domain Driven' and avoid all references to "Infrastructure" ideas.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.