Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Feb 15, 2021
0 parents commit 4aa0993
Show file tree
Hide file tree
Showing 18 changed files with 914 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/tests export-ignore
/phpunit.xml export-ignore
/phpunit.xml.old export-ignore
/phpcs.xml export-ignore
/phpcs.xml export-ignore
/docker export-ignore
/.travis.yml export-ignore
/.styleci.yml export-ignore
/.php_cs export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/vendor export-ignore
/coverage export-ignore
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# IDE generated files #
######################
.idea

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
vendor
coverage
composer.lock
.php_cs.cache
.phpunit.result.cache
14 changes: 14 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return PhpCsFixer\Config::create()
->setRiskyAllowed(false)
->setRules([
'@PSR2' => true,
])
->setUsingCache(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
)
;
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
44 changes: 44 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
cache:
directories:
- $HOME/.composer/cache

language: php

matrix:
include:
# Laravel 6.*
- php: 7.2
env: LARAVEL='6.*' COMPOSER_FLAGS='--prefer-stable'
- php: 7.3
env: LARAVEL='6.*' COMPOSER_FLAGS='--prefer-stable'
# Laravel 7.*
- php: 7.2
env: LARAVEL='7.*' COMPOSER_FLAGS='--prefer-stable'
- php: 7.3
env: LARAVEL='7.*' COMPOSER_FLAGS='--prefer-stable'
- php: 7.4
env: LARAVEL='7.*' COMPOSER_FLAGS='--prefer-stable'
# Laravel 8.*
- php: 7.3
env: LARAVEL='8.*' COMPOSER_FLAGS='--prefer-stable'
- php: 7.4
env: LARAVEL='8.*' COMPOSER_FLAGS='--prefer-stable'
- php: 8.0
env: LARAVEL='8.*' COMPOSER_FLAGS='--prefer-stable'
fast_finish: true

before_install:
- travis_retry composer self-update
- travis_retry composer require "illuminate/database:${LARAVEL}" --no-interaction --no-update

install:
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction --no-suggest

before_script:
- composer config discard-changes true

script:
- XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "korridor/laravel-has-many-merged",
"description": "Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships",
"keywords": ["laravel", "eloquent", "relations", "has-many"],
"homepage": "https://github.com/korridor/laravel-has-many-merged",
"license": "MIT",
"authors": [
{
"name": "korridor",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require": {
"php": "^7.1|^8",
"illuminate/database": "^6|^7|^8"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^8.0|^9.0",
"friendsofphp/php-cs-fixer": "^2.16",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
"Korridor\\LaravelHasManyMerged\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Korridor\\LaravelHasManyMerged\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"fix": "./vendor/bin/php-cs-fixer fix",
"lint": "./vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php"
},
"config": {
"sort-packages": true
}
}
24 changes: 24 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM php:7.4-cli

RUN apt-get update && apt-get install -y \
zlib1g-dev \
libzip-dev

RUN docker-php-ext-install zip

RUN pecl install xdebug-2.8.1 \
&& docker-php-ext-enable xdebug

# Install composer and add its bin to the PATH.
RUN curl -s http://getcomposer.org/installer | php && \
echo "export PATH=${PATH}:/var/www/vendor/bin" >> ~/.bashrc && \
mv composer.phar /usr/local/bin/composer

# Add bash aliases
RUN echo "alias ll='ls --color=auto -al'" >> ~/.bashrc


# Source the bash
RUN . ~/.bashrc

WORKDIR /usr/src/app
9 changes: 9 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.7'
services:
workspace:
build:
context: .
volumes:
- ..:/usr/src/app
tty: true
stdin_open: true
25 changes: 25 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
The MIT License (MIT)
=====================

Copyright © `2021` `korridor`

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<ruleset name="ASH2">
<description>The ASH2 coding standard.</description>
<rule ref="PSR2">
</rule>

<file>src/</file>
<file>tests/</file>
</ruleset>
38 changes: 38 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Feature">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<server name="APP_ENV" value="testing"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="QUEUE_DRIVER" value="sync"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
97 changes: 97 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Laravel has many merged

[![Latest Version on Packagist](https://img.shields.io/packagist/v/korridor/laravel-has-many-merged?style=flat-square)](https://packagist.org/packages/korridor/laravel-has-many-merged)
[![License](https://img.shields.io/packagist/l/korridor/laravel-has-many-merged?style=flat-square)](license.md)
[![Codecov](https://img.shields.io/codecov/c/github/korridor/laravel-has-many-merged?style=flat-square)](https://codecov.io/gh/korridor/laravel-has-many-merged)
[![TravisCI](https://img.shields.io/travis/korridor/laravel-has-many-merged?style=flat-square)](https://travis-ci.org/korridor/laravel-has-many-merged)
[![StyleCI](https://styleci.io/repos/339041939/shield)](https://styleci.io/repos/339041939)

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships.
This relation fully supports lazy and eager loading.

## Installation

You can install the package via composer with following command:

```bash
composer require korridor/laravel-has-many-merged
```

### Requirements

This package is tested for the following Laravel versions:

- 8.* (PHP 7.3, 7.4, 8.0)
- 7.* (PHP 7.2, 7.3, 7.4)
- 6.* (PHP 7.2, 7.3)

## Usage examples

In the following example there are two models User and Message.
Each message has a sender and a receiver.
The User model has two hasMany relations - one for the sent messages and the other for the received ones.

With this plugin you can add a relation that contains sent and received messages of a user.

```php
use Korridor\LaravelHasManyMerged\HasManyMerged;
use Korridor\LaravelHasManyMerged\HasManyMergedRelation;

class User extends Model
{
use HasManyMergedRelation;

// ...

/**
* @return HasManyMerged|Message
*/
public function messages()
{
return $this->hasManyMerged(Message::class, ['sender_user_id', 'receiver_user_id']);
}

/**
* @return HasMany|Message
*/
public function sentMessages()
{
return $this->hasMany(Message::class, 'sender_user_id');
}

/**
* @return HasMany|Message
*/
public function receivedMessages()
{
return $this->hasMany(Message::class, 'receiver_user_id');
}
}
```

## Contributing

I am open for suggestions and contributions. Just create an issue or a pull request.

### Local docker environment

The `docker` folder contains a local docker environment for development.
The docker workspace has composer and xdebug installed.

```bash
docker-compose run workspace bash
```

### Testing

The `composer test` command runs all tests with [phpunit](https://phpunit.de/).
The `composer test-coverage` command runs all tests with phpunit and creates a coverage report into the `coverage` folder.

### Codeformatting/Linting

The `composer fix` command formats the code with [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).
The `composer lint` command checks the code with [phpcs](https://github.com/squizlabs/PHP_CodeSniffer).

## License

This package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.
Loading

0 comments on commit 4aa0993

Please sign in to comment.