Skip to content

Instantly share code, notes, and snippets.

@geekish
Last active May 22, 2018 20:03
Show Gist options
  • Save geekish/ef1407ece6f88fddeb3ccdad7a4db0f0 to your computer and use it in GitHub Desktop.
Save geekish/ef1407ece6f88fddeb3ccdad7a4db0f0 to your computer and use it in GitHub Desktop.
Laravel macros

Changelog

Why not?

2018-05-22

  • Added typehints; this assumes PHP 7 is being used (as it should!).
  • Removed Str::uuid() as this is now built in.

The MIT License (MIT)

Copyright (c) 2018 Hannah Warmbier hannahwarmbier@gmail.com

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.

<?php
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
/**
* Checks if value at 'key' is null
*/
Collection::macro('hasNotNull', function ($key) : bool {
if (!$this->has($key)) {
return false;
}
return ($this->get($key) !== null);
});
/**
* Pulls $key as a collection
*/
Collection::macro('pullect', function (string $key) : Collection {
return collect($this->pull($key));
});
/**
* Since you can't just do $collection->put('extra.foo', 'bar')
*/
Collection::macro('putExtra', function (string $key, $value) : Collection {
$that = $this;
if (!$that->has('extra')) {
$that['extra'] = collect();
}
$extra = $that['extra']->put($key, $value);
return $that->put('extra', $extra);
});
/**
* Because $collection->has('foo.bar') doesn't work but Arr::has('foo.bar') does
*/
Collection::macro('reallyHas', function (string $key) : bool {
return Arr::has($this, $key);
});
/**
* Sometimes you just want to rename a key (consuming API data, etc.)
* This uses the reallyHas() macro from above
*/
Collection::macro('rekey', function (string $key, string $new) : Collection {
$that = $this;
if ($that->reallyHas($key)) {
$value = $that->pull($key);
return $that->put($new, $value);
}
return $that;
});
/**
* Iterative version of the above
*/
Collection::macro('rekeyMap', function (array $map) : Collection {
$that = $this;
foreach ($map as $key => $new) {
$that = $that->rekey($key, $new);
}
return $that;
});
/**
* Checks if current route starts with string/prefix
*/
Route::macro('currentRouteStartsWith', function (string $start) : bool {
if ($this->currentRouteNamed($start)) {
return true;
}
return Str::startsWith($start, $this->currentRouteName());
});
/**
* When you just want to remove something from a string
*/
Str::macro('remove', function (string $remove, string $target) : string {
return str_replace($remove, '', $target);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment