Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/tomkyle/repository-persistence

Scaffold for Repository-and-Persistence design pattern
https://github.com/tomkyle/repository-persistence

Last synced: about 12 hours ago
JSON representation

Scaffold for Repository-and-Persistence design pattern

Awesome Lists containing this project

README

        

Repository · Persistence

[![Packagist](https://img.shields.io/packagist/v/tomkyle/repository-persistence.svg?style=flat)](https://packagist.org/packages/tomkyle/repository-persistence)
[![PHP version](https://img.shields.io/packagist/php-v/tomkyle/repository-persistence.svg)](https://packagist.org/packages/tomkyle/repository-persistence)
[![PHP Composer](https://github.com/tomkyle/repository-persistence/actions/workflows/php.yml/badge.svg)](https://github.com/tomkyle/repository-persistence/actions/workflows/php.yml)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)

**Scaffold for Repository-and-Persistence design pattern.**

---

## Installation

```bash
$ composer require tomkyle/repository-persistence
```

## Setup

**The repository needs a persistence.**

```php
get('john-doe');
print_r($person);
}
catch (\OutOfBoundsException) {
// Not found
}
```

Output will be like:

```text
Array (
[age] => 30
[city] => New York
[name] => John Doe
)
```

**Find one item by criteria.** This method may return `null`.

```php
$repo->findOneBy([
'name' => 'John'
]);
```

**Get all items:**

```php
$repo->findAll();
```

**Find items by criteria**

```php
$repo->findBy([
'color' => 'blue'
]);
```

**Update item**

```php
$saved = $repo->save(['id' => 43, 'name' => 'John']));
```

**Delete item**

```php
$repo->delete(43);
```

**Create new item**

```php
$saved = $repo->save(['name' => 'Angie']));
```

If you need the new ID onbeforehand in your App controller, e.g. for redirecting the client to the new resource, you can obtain a new ID from the repo. It then looks exactly like updating, but the *Repository* implementation will figure out if the item has to be created or updated.

```php
$new_id = $repo->getNextId();
$repo->save(['id' => $new_id, 'name' => 'Angie']));
```

---

## Persistence

Inside a repository, the *Persistence* actually manages the data storage.

### Instantiation

```php
`?int` limit
`?int` offset | `iterable` | Some records |
| save | `array¦object` entity | | `bool` | |
| delete | `array¦object` entity | | `bool` | |

---

## Development

### Install requirements

```bash
$ composer install
$ npm install
```

### Watch source and run various tests

This will watch changes inside the **src/** and **tests/** directories and run a series of tests:

1. Find and run the according unit test with *PHPUnit*.
2. Find possible bugs and documentation isses using *phpstan*.
3. Analyse code style and give hints on newer syntax using *Rector*.

```bash
$ npm run watch
```

### Run all tests

Choose to your taste:

```bash
$ npm run phpunit
$ composer test
```