Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dammy001 committed Dec 7, 2021
0 parents commit fe66334
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#### Laravel Helper Macros
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "damilare/laravel-helper-macros",
"description": "Laravel Helper Macros",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Damilare Anjorin",
"email": "damilareanjorin1@gmail.com"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4|^8.0",
"illuminate/contracts": "^8.0|^7.0",
"illuminate/support": "^8.0|^7.0"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
"psr-4": {
"Damilare\\LaravelHelperMaros\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Damilare\\LaravelHelperMacros\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Damilare\\LaravelHelperMacros\\HelperMacroServiceProvider"
]
}
}
}
35 changes: 35 additions & 0 deletions src/HelperMacroServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Providers;

use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;

class MacroServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
Collection::make(glob(__DIR__ . '/Macros/*.php'))
->mapWithKeys(function ($path) {
return [$path => pathinfo($path, PATHINFO_FILENAME)];
})
->each(function ($macro, $path) {
require_once $path;
});
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
6 changes: 6 additions & 0 deletions src/Macros/map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use Illuminate\Database\Eloquent\Builder;

Builder::macro('map', fn (callable $callback) => $this->get()->map($callback));
Builder::macro('filter', fn (callable $callback) => $this->get()->filter($callback));
35 changes: 35 additions & 0 deletions src/Macros/str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Str;

if (!Str::hasMacro('truncate')) {
Str::macro(/**
* @param int $length
* @param string $text
* @return array|string|string[]|null
*/ 'truncate',
function (int $length, string $text) {
if ($length >= strlen($text)) {
return $text;
}
return preg_replace("/^(.{1,{$length}})(\\s.*|$)/s", '\\1...', $text);
}
);
}

if (!Str::hasMacro('formatAmount')) {
Str::macro(/**
* @param int|float $amount
* @param int $decimal
* @return string
*/ 'formatAmount',
function ($amount, int $decimal) {
return number_format(
$amount,
$decimal,
'.',
''
);
}
);
}
35 changes: 35 additions & 0 deletions src/Macros/whereLike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

Builder::macro('whereLike', function ($columns, string $value, $concat = false) {
$this->where(function (Builder $query) use ($columns, $value, $concat) {
foreach (Arr::wrap($columns) as $column) {
$query->when(
Str::contains($column, '.'),
function (Builder $query) use ($column, $value) {
$parts = explode('.', $column);
$relationColumn = array_pop($parts);
$relationName = implode('.', $parts);

return $query->orWhereHas(
$relationName,
function (Builder $query) use ($relationColumn, $value) {
$query->where($relationColumn, 'LIKE', "%{$value}%");
}
);
},
function (Builder $query) use ($column, $value, $concat) {
return $concat ?
$query->orWhereRaw("CONCAT(users.name, ' ', users.last_name) like '%{$value}%'")
->orWhere($column, 'LIKE', "%{$value}%") :
$query->orWhere($column, 'LIKE', "%{$value}%");
}
);
}
});

return $this;
});

0 comments on commit fe66334

Please sign in to comment.