PHPackages                             bigsinoos/laravel-repository-response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bigsinoos/laravel-repository-response

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bigsinoos/laravel-repository-response
=====================================

Solution to PHP repository class method responses, Helps to implement true Repository pattern.

492PHP

Since Jan 12Pushed 11y ago1 watchersCompare

[ Source](https://github.com/reshadman/laravel-repository-response)[ Packagist](https://packagist.org/packages/bigsinoos/laravel-repository-response)[ RSS](/packages/bigsinoos-laravel-repository-response/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Repository Response
===========================

[](#laravel-repository-response)

The trend of Repository pattern made all of us to implement it in our Laravel projects. But We all have been implementing it incorrect or at least incomplete. The problem is that in true Repository pattern in addition to the input contracts( Our repositoy interfaces ) We make a deal on our method response, which is not possible in dynamic languages like PHP (It has been added to PHP7). This package helps you to implement the true pattern approximately.

> We should know that there is no need to implement the full true pattern, patterns have been made to solve us problems, not to be the problem itself. The incomplete pattern that is being trended is good enough for easier testing.

Installation
------------

[](#installation)

```
composer require bigsinoos/laravel-repository-response

```

### How it works

[](#how-it-works)

The real pattern interacts with Entities instead of our Eloquent models. so with `Bigsinoos\RepositoryResponse\BaseEloquentEntity` this problem is solved. So instead of messaging the `Eloquent` models, We must pass our entities. when using Eloquent Repsitories, we can put the model inside our entity which only is accssible by our `EloquentRepository` implementation with the help of **Friend / Sibling** classes.

### Friend Classes and debug\_back\_trace function

[](#friend-classes-and-debug_back_trace-function)

Friend classes in the scope of object oriented design, can break each other's encapsulation layer, for example they can call `protected` method of each other. PHP does not support friend classes at all.

When we return Eloquent models from our repository methods, we allow external access to our database layer, which should be done through our repository contracts, this simply breaks the pattern, to solve this problem instead of returning Eloquent models from repository, we simply create a new Entity for the Model, and Wrap it arround the model, So everytime a method or propery is called from the entity the entity behvaes like below :

- Check that it is being called from a friend class or not (with debug\_backtract).
- If so, calls the method from it's model, else it will throw an access denied exception.

When trying to set a property on an Entity it is set on its models.

By this you can pass an Entity to multiple repositories and their eloquent models can be used as long as they are friend with each other, the friendship is defined in the eloquent implementation of the entity.

But this also breaks another rule. \*\* Just like as concrete implementations of reposutory classes we should define different entities for each implmenetation.\*\* (For example `MongoBlogeEntity`, `EloquentBlogEntity`, `DoctorineBlogEntity` but as long as you make a `BlogEntityInterface` contract for them. there isn't any problem, at least it allows to switch between different implmentaions, which was not possible in our previous methodology (returning eloquent models from methods)).

### Usage

[](#usage)

A user repository workflow will be as below :

- UserEntityContract.php
- EloquentUserEntity.php : this class should extends `Bigsinoos\RepositoryResponse\BaseEloquentEntity` class, to get the class friendship functionality.
- User.php
- UserRepositoryContract.php
- EloquentUserRepository.php

UserEntityContract.php

```
