-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fe66334
Showing
7 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#### Laravel Helper Macros |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
'.', | ||
'' | ||
); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |