Skip to content

Commit

Permalink
Merge pull request #2 from onursimsek/feature/readme
Browse files Browse the repository at this point in the history
Readme
  • Loading branch information
onursimsek committed Sep 8, 2024
2 parents 7c45d1f + b3d2b7d commit 756c90c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 13 deletions.
72 changes: 67 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# This is my package laravel-extended
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://banners.beyondco.de/Laravel%20Extended.png?theme=dark&packageManager=composer+require&packageName=onursimsek%2Flaravel-extended&pattern=topography&style=style_1&description=Extend+your+Laravel+project+with+mixins+and+mores&md=1&showWatermark=0&fontSize=100px&images=arrows-expand">
<source media="(prefers-color-scheme: light)" srcset="https://banners.beyondco.de/Laravel%20Extended.png?theme=light&packageManager=composer+require&packageName=onursimsek%2Flaravel-extended&pattern=topography&style=style_1&description=Extend+your+Laravel+project+with+mixins+and+mores&md=1&showWatermark=0&fontSize=100px&images=arrows-expand">
<img alt="Package Image" src="https://banners.beyondco.de/Precondition.png?theme=dark&packageManager=composer+require&packageName=onursimsek%2Fprecondition&pattern=topography&style=style_2&description=HTTP+Precondition+for+Laravel&md=1&showWatermark=1&fontSize=125px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg">
</picture>

# Extend your Laravel project with mixins and mores

[![Latest Version on Packagist](https://img.shields.io/packagist/v/onursimsek/laravel-extended.svg?style=flat-square)](https://packagist.org/packages/onursimsek/laravel-extended)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/onursimsek/laravel-extended/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/onursimsek/laravel-extended/actions?query=workflow%3Arun-tests+branch%3Amain)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/onursimsek/laravel-extended/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/onursimsek/laravel-extended/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/onursimsek/laravel-extended.svg?style=flat-square)](https://packagist.org/packages/onursimsek/laravel-extended)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
[![Tests](https://github.com/onursimsek/laravel-extended/actions/workflows/run-tests.yml/badge.svg)](https://github.com/onursimsek/laravel-extended/actions)
[![Total Downloads](https://img.shields.io/packagist/dt/onursimsek/laravel-extended.svg?style=flat-square)](https://packagist.org/packages/onursimsek/)

## Installation

Expand All @@ -15,6 +20,63 @@ You can install the package via composer:
composer require onursimsek/laravel-extended
```

## Usage

### Extended Illuminate\Database\Query\Builder

```php
Product::whereGreaterThan('price', 500)->get();
// select * from products where price > 500
Product::whereGreaterThanOrEqual('price', 500)->get();
// select * from products where price >= 500

Product::whereLessThan('price', 500)->get();
// select * from products where price < 500
Product::whereLessThanOrEqual('price', 500)->get();
// select * from products where price <= 500

Product::whereColumnGreaterThan('price', 'amount')->get();
// select * from products where price > amount
Product::whereColumnGreaterThanOrEqual('price', 'amount')->get();
// select * from products where price >= amount

Product::whereColumnLessThan('price', 'amount')->get();
// select * from products where price < amount
Product::whereColumnLessThanOrEqual('price', 'amount')->get();
// select * from products where price <= amount

Product::whenWhere(false, 'is_active')->get();
// select * from products
Product::whenWhere(true, 'is_active')->get();
// select * from products where is_active = 1
```

### Extended Illuminate\Support\Str

```php
use Illuminate\Support\Str;

Str::squishBetween("I\twill kiss\t\nyou!");
// I will kiss you!
Str::replaceBetween('I will kiss you!', 'will', 'you', 'miss');
// I will miss you!
Str::replaceBetweenMatch('I will kiss you!', 'will', 'you', '/k(.*)s/', 'hug');
// I will hug you!
```

### Extended Illuminate\Support\Stringable

```php
use Illuminate\Support\Stringable;

Str::of("I\twill kiss\t\nyou!")->squishBetween();
// I will kiss you!
Str::of('I will kiss you!')->replaceBetween('will', 'you', 'miss');
// I will miss you!
Str::of('I will kiss you!')->replaceBetweenMatch('will', 'you', '/k(.*)s/', 'hug');
// I will hug you!
```

## Testing

```bash
Expand Down
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.2",
"php": "^8.2|^8.3",
"illuminate/contracts": "^10.0||^11.0"
},
"require-dev": {
Expand Down Expand Up @@ -71,10 +71,7 @@
"laravel": {
"providers": [
"OnurSimsek\\LaravelExtended\\LaravelExtendedServiceProvider"
],
"aliases": {
"LaravelExtended": "OnurSimsek\\LaravelExtended\\Facades\\LaravelExtended"
}
]
}
},
"minimum-stability": "dev",
Expand Down
3 changes: 2 additions & 1 deletion config/extended.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

return [

\Illuminate\Database\Query\Builder::class => true,
\Illuminate\Support\Str::class => true,
];
12 changes: 10 additions & 2 deletions src/LaravelExtendedServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Support\AggregateServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use OnurSimsek\LaravelExtended\Mixins\BuilderMixin;
use OnurSimsek\LaravelExtended\Mixins\StringableMixin;
use OnurSimsek\LaravelExtended\Mixins\StrMixin;

final class LaravelExtendedServiceProvider extends AggregateServiceProvider
Expand All @@ -21,8 +23,14 @@ public function boot(): void
{
AboutCommand::add('Laravel Extended', fn () => ['Version' => '1.0.0']);

Builder::mixin(new BuilderMixin());
Str::mixin(new StrMixin());
if (config(sprintf('extended.%s', Builder::class), false)) {
Builder::mixin(new BuilderMixin());
}

if (config(sprintf('extended.%s', Str::class), false)) {
Str::mixin(new StrMixin());
Stringable::mixin(new StringableMixin());
}

if ($this->app->runningInConsole()) {
$this->publishing();
Expand Down
25 changes: 25 additions & 0 deletions tests/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Workbench\App\Models\Product;

it('boundary constraints', function ($method, $column, $value, $sql) {
expect(Product::query()->{$method}($column, $value)->toRawSql())->toEqual($sql);
})->with([
// Greater
['whereGreaterThan', 'price', 500, 'select * from "products" where "price" > 500'],
['whereGreaterThanOrEqual', 'price', 500, 'select * from "products" where "price" >= 500'],
['whereColumnGreaterThan', 'price', 'amount', 'select * from "products" where "price" > "amount"'],
['whereColumnGreaterThanOrEqual', 'price', "amount", 'select * from "products" where "price" >= "amount"'],
// Less
['whereLessThan', 'price', 500, 'select * from "products" where "price" < 500'],
['whereLessThanOrEqual', 'price', 500, 'select * from "products" where "price" <= 500'],
['whereColumnLessThan', 'price', 'amount', 'select * from "products" where "price" < "amount"'],
['whereColumnLessThanOrEqual', 'price', "amount", 'select * from "products" where "price" <= "amount"'],
]);

it('conditional constraints', function ($value, $column, $sql) {
expect(Product::query()->whenWhere($value, $column)->toRawSql())->toEqual($sql);
})->with([
[true, 'is_active', 'select * from "products" where "is_active" = 1'],
[false, 'is_active', 'select * from "products"'],
]);
9 changes: 9 additions & 0 deletions workbench/app/Models/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
}

0 comments on commit 756c90c

Please sign in to comment.