2017 © Pedro Peláez
 

library repository

Detached model and controller warehouse data provider

image

crcms/repository

Detached model and controller warehouse data provider

  • Sunday, July 29, 2018
  • by hiword
  • Repository
  • 0 Watchers
  • 18 Stars
  • 705 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 17 Versions
  • 21 % Grown

The README.md

CRCMS Repository

Latest Stable Version License StyleCI, (*1)

A specialized data provider layer, based on ORM, only as a data provider, the main role is to separate the coupling before the Controller and Model, (*2)

Install

You can install the package via composer:, (*3)

composer require crcms/repository

Laravel

If your version is less than 5.5 please modify config / app.php, (*4)

'providers' => [
    CrCms\Repository\RepositoryServiceProvider::class,
]

If you'd like to make configuration changes in the configuration file you can pubish it with the following Aritsan command:, (*5)

php artisan vendor:publish --provider="CrCms\Repository\RepositoryServiceProvider"

Commands

php artisan make:repository TestRepository --model TestModel
php artisan make:magic TestMagic



## Example ### QueryMagic ```php use CrCms\Repository\AbstractMagic; use CrCms\Repository\Contracts\QueryRelate; class TestMagic extends AbstractMagic { /** * @param QueryRelate $queryRelate * @param int $id * @return QueryRelate */ protected function byName(QueryRelate $queryRelate, string $name) { return $queryRelate->where('name', $name); } /** * @param QueryRelate $queryRelate * @param string $title * @return QueryRelate */ protected function byTitle(QueryRelate $queryRelate, string $title) { return $queryRelate->where('title', 'like', "%{$title}%"); } /** * @param QueryRelate $queryRelate * @param int $id * @return QueryRelate */ protected function byId(QueryRelate $queryRelate, int $id) { return $queryRelate->where('id', $id); } /** * @param QueryRelate $queryRelate * @param array $sort * @return QueryRelate */ protected function bySort(QueryRelate $queryRelate, array $sort) { return $queryRelate->orderByArray($sort); } }
### Repository
class TestRepository extends AbstractRepository
{
    /**
     * @var array
     */
    protected $guard = [
        'id', 'title','other'
    ];

    /**
     * @return TestModel
     */
    public function newModel(): TestModel
    {
        return app(TestModel::class);
    }

    /**
     * @param int $perPage
     * @return LengthAwarePaginator
     */
    public function paginate(AbstractMagic $magic = null, int $perPage = 15): LengthAwarePaginator
    {
        return $this->whenMagic($magic)->where('built_in', 1)->orderBy($this->getModel()->getKeyName(), 'desc')->paginate($perPage);
    }

    /**
     * @param int $name
     * @param int $title
     */
    public function updateName(string $name, string $title)
    {
        $this->where('name', $name)->update(['title' => $title]);
    }

}
### Guard Or Scenes Usually we need to filter the incoming parameter values when adding or modifying and querying the data, and retain the required parameter values. Guard and scenes are born for this
class TestRepository extends AbstractRepository
{
    /**
     * @var array
     */
    protected $scenes = [
        'create' => ['sort', 'added_at'],
        'modify' => ['sort', 'published_at']
    ];

    /**
     * @var array
     */
    protected $guard = [
        'id', 'title', 'other'
    ];
}

$testRepository->create($data, 'create'); //OR
$testRepository->setCurrentScene('create')->create($data); //OR
$testRepository->setGuard(['sort', 'added_at'])->create($data); 

class TestMagic extends AbstractMagic
{
    /**
     * @var array
     */
    protected $scenes = [
        'frontend' => ['name'],
        'backend' => ['title']
    ];

    /**
     * @var array
     */
    protected $guard = [
        'title',
    ];

    /**
     * @param QueryRelate $queryRelate
     * @param int $id
     * @return QueryRelate
     */
    protected function byName(QueryRelate $queryRelate, string $name)
    {
        return $queryRelate->where('name', $name);
    }

    /**
     * @param QueryRelate $queryRelate
     * @param string $title
     * @return QueryRelate
     */
    protected function byTitle(QueryRelate $queryRelate, string $title)
    {
        return $queryRelate->where('title', 'like', "%{$title}%");
    }
}

$testRepository->magic(new TestMagic($data, 'frontend'))->paginate(); //OR
$testRepository->magic((new TestMagic($data))->setCurrentScene('frontend'))->paginate(); //OR
$testRepository->magic((new TestMagic($data))->setGuard(['title']))->paginate(); //OR->create($data);

**Note: when guard and scenes are both present, scenes has a higher priority. If scenes is empty, it will use guard.** ### Listener
TestRepository::observer(TestListener::class);

TestListener {

    public function creating(TestRepository $repository, array $data)
    {
        //append the value to be written
        $repository->addData('append_data','value');

        //rewrite all values written
        $repository->setData(['key'=>'value']);
    }    

    public function created(TestRepository $repository, TestModel $model)
    {
    }    

    public function updating(TestRepository $repository, array $data)
    {
    }    

    public function updated(TestRepository $repository, TestModel $model)
    {
    }

    public function deleting(TestRepository $repository, array $ids)
    {
    }

    public function deleted(TestRepository $repository, Collection $models)
    {
    }
}
### Cache
class TestRepository {

    public function do(User $user)
    {
        return $this->byIntId($user->id);
    }
}

$repository = new TestRepository;

#### store
$repository->cache()->do(new User);
#### forget
$repository->cache()->forget('do')
#### flush
$repository->cache()->flush()
### Repository Methods
public function all(): Collection;
```php public function get(): Collection;
```php
public function pluck(string $column, string $key = ''): Collection;
```php public function max(string $column): int;
```php
public function count(string $column = '*'): int;
```php public function avg($column): int;
```php
public function sum(string $column): int;
```php public function chunk(int $limit, callable $callback): bool;
```php
public function valueOfString(string $key, string $default = ''): string;
```php public function valueOfInt(string $key, int $default = 0): int;
```php
public function increment(string $column, int $amount = 1, array $extra = []): int;
```php public function decrement(string $column, int $amount = 1, array $extra = []): int;
```php
public function delete(): int;
```php public function deleteByStringId(string $id): int;
```php
public function deleteByIntId(int $id): int;
```php public function deleteByArray(array $ids): int;
```php
public function paginate(int $perPage = 15) : LengthAwarePaginator;
```php public function create(array $data) : Model;
```php
public function update(array $data): int;
```php public function updateByIntId(array $data, int $id) : Model;
```php
public function updateByStringId(array $data, string $id) : Model;
```php public function byIntId(int $id);
```php
public function byStringId(string $id);
```php public function byIntIdOrFail(int $id) : Model;
```php
public function byStringIdOrFail(string $id) : Model;
```php public function oneByString(string $field, string $value): Model;
```php
public function oneByInt(string $field, int $value): Model;
```php public function oneByStringOrFail(string $field, string $value) : Model;
```php
public function oneByIntOrFail(string $field, int $value) : Model;
```php public function first();
```php
public function firstOrFail() : Model;
### QueryRelate Methods
public function select(array $column = ['*']): QueryRelate;
```php public function selectRaw(string $expression, array $bindings = []): QueryRelate;
```php
public function skip(int $limit): QueryRelate;
```php public function take(int $limit): QueryRelate;
```php
public function groupBy(string $column): QueryRelate;
```php public function groupByArray(array $columns): QueryRelate;
```php
public function orderBy(string $column, string $sort = 'desc'): QueryRelate;
```php public function orderByArray(array $columns): QueryRelate;
```php
public function distinct(): QueryRelate;
```php public function where(string $column, string $operator = '=', string $value = ''): QueryRelate;
```php
public function whereClosure(\Closure $callback): QueryRelate;
```php public function orWhereClosure(\Closure $callback): QueryRelate;
```php
public function orWhere(string $column, string $operator = '=', string $value = ''): QueryRelate;
```php public function whereBetween(string $column, array $between): QueryRelate;
```php
public function orWhereBetween(string $column, array $between): QueryRelate;
```php public function whereRaw(string $sql, array $bindings = []): QueryRelate;
```php
public function orWhereRaw(string $sql, array $bindings = []): QueryRelate;
public function orWhereNotBetween($column, array $between): QueryRelate;
public function whereExists(\Closure $callback): QueryRelate;
public function orWhereExists(\Closure $callback): QueryRelate;
public function whereNotExists(\Closure $callback): QueryRelate;
public function orWhereNotExists(\Closure $callback): QueryRelate;
```php public function whereIn(string $column, array $values): QueryRelate;
```php
public function orWhereIn(string $column, array $values): QueryRelate;
```php public function whereNotIn(string $column, array $values): QueryRelate;

```php public function orWhereNotIn(string $column, array $values): QueryRelate;
```php public function whereNull(string $column): QueryRelate;
```php
public function orWhereNull(string $column): QueryRelate;
```php public function whereNotNull(string $column): QueryRelate;
```php
public function orWhereNotNull(string $column): QueryRelate;
```php public function raw(string $sql): QueryRelate;
```php
public function from(string $table): QueryRelate;
```php public function join(string $table, string $one, string $operator = '=', string $two = ''): QueryRelate;
```php
public function joinClosure(string $table, \Closure $callback): QueryRelate;
```php public function leftJoin(string $table, string $first, string $operator = '=', string $two = ''): QueryRelate;
```php
public function leftJoinClosure(string $table, \Closure $callback): QueryRelate;
```php public function rightJoin(string $table, string $first, string $operator = '=', string $two = ''): QueryRelate;
```php
public function rightJoinClosure(string $table, \Closure $callback): QueryRelate;
```php public function callable(callable $callable): QueryRelate;
```php
public function wheres(array $wheres): QueryRelate;
```php public function union(QueryRelate $queryRelate): QueryRelate;
```php
public function magic(QueryMagic $queryMagic): QueryRelate;
```php public function whenMagic(?QueryMagic $queryMagic = null): QueryRelate;
```php
public function with(string $relation): QueryRelate;
```php public function withArray(array $relations): QueryRelate;
```php
public function without(string $relation): QueryRelate;
```php public function withoutArray(array $relations): QueryRelate;
```php
public function having(string $column, $operator = null, $value = null): QueryRelate;
```php public function orHaving(string $column, $operator = null, $value = null): QueryRelate;
```php
public function havingRaw(string $sql, array $bindings = []): QueryRelate;
```php public function orHavingRaw(string $sql, array $bindings = []): QueryRelate;
```php
public function lockForUpdate(): QueryRelate;
```php public function sharedLock(): QueryRelate;

License

MIT license, (*6)

The Versions

29/07 2018

dev-master

9999999-dev https://github.com/crcms/repository

Detached model and controller warehouse data provider

  Sources   Download

MIT

The Requires

 

orm laravel repository php model crcms

19/07 2018

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/crcms/repository

Detached model and controller warehouse data provider

  Sources   Download

MIT

The Requires

 

orm laravel repository php model crcms

05/04 2018

2.0.10

2.0.10.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

04/04 2018

2.0.9

2.0.9.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

11/01 2018

2.0.8

2.0.8.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

02/01 2018

2.0.7

2.0.7.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

22/12 2017

2.0.6

2.0.6.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

21/12 2017

2.0.5

2.0.5.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

15/12 2017

2.0.4

2.0.4.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

27/11 2017

2.0.2

2.0.2.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

25/10 2017

2.0.1

2.0.1.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

09/10 2017

2.0.0

2.0.0.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

12/06 2017

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

21/03 2017

1.2.x-dev

1.2.9999999.9999999-dev https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

25/02 2017

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

18/01 2017

0.1.0

0.1.0.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms

17/01 2017

0.0.1

0.0.1.0 https://github.com/crcms/repository

crcms repository extends laravel

  Sources   Download

MIT

The Requires

 

laravel repository php crcms