This commit is contained in:
2018-07-08 17:34:50 +02:00
commit d2d098006c
112 changed files with 40655 additions and 0 deletions
@@ -0,0 +1,87 @@
<?php
namespace MightyPork\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class BladeExtensionsProvider extends ServiceProvider
{
/**
* Parse argument of a blade directive
*
* @param string $x argument received by a blade directive
* @return array the arguments (strings without quotes)
*/
private static function parseBladeArgs($x)
{
// strip wrapping parens
return array_map('trim', str_getcsv($x));
}
/**
* Add blade directives
*
* @return void
*/
public function boot()
{
// @faker(text, 100)
Blade::directive('faker', function ($x) {
$args = self::parseBladeArgs($x);
$method = $args[0];
$params = count($args) > 1 ? $args[1] : '';
return "<?= e(app()->make('\\Faker\\Generator')->$method($params)) ?>";
});
// csrf token for forms
Blade::directive('formCsrf', function () {
return '<?= csrf_field() ?>';
});
// json encode
Blade::directive('json', function ($x) {
if (config('app.pretty_json')) {
return "<?= json_encode(($x), JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES) ?>";
} else {
return "<?= json_encode(($x), JSON_UNESCAPED_SLASHES) ?>";
}
});
// json encode, escaped
Blade::directive('jsone', function ($x) {
if (config('app.pretty_json')) {
return "<?= e(json_encode(($x), JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)) ?>";
} else {
return "<?= e(json_encode(($x), JSON_UNESCAPED_SLASHES)) ?>";
}
});
// selected if cond true
Blade::directive('selected', function ($x) {
return "<?= ($x) ? 'selected' : '' ?>";
});
// checked if cond true
Blade::directive('checked', function ($x) {
return "<?= ($x) ? 'checked' : '' ?>";
});
Blade::if('admin', function () {
return \Auth::user()->isAdmin();
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
+152
View File
@@ -0,0 +1,152 @@
<?php
namespace MightyPork\Providers;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
/**
* Register macros
*/
class MacroServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
/** Natural ordering */
Collection::macro('natSortBy', function ($callback, $caseSensitive = false) {
$results = [];
$callback = $this->valueRetriever($callback);
// First we will loop through the items and get the comparator from a callback
// function which we were given. Then, we will sort the returned values and
// and grab the corresponding values for the sorted keys from this array.
try {
foreach ($this->items as $key => $value) {
$results[$key] = iconv('UTF-8', 'ASCII//TRANSLIT', $callback($value, $key));
}
} catch (\ErrorException $e) {
// some iconv versions do not support translit of UTF-8
foreach ($this->items as $key => $value) {
$results[$key] = simple_translit($callback($value, $key));
}
}
uasort($results, $caseSensitive ? 'strnatcmp' : 'strnatcasecmp');
// Once we have sorted all of the keys in the array, we will loop through them
// and grab the corresponding model so we can set the underlying items list
// to the sorted version. Then we'll just return the collection instance.
foreach (array_keys($results) as $key) {
$results[$key] = $this->items[$key];
}
return new static($results);
});
/** Natural ordering - for strings */
Collection::macro('natSort', function ($caseSensitive = false) {
$results = [];
// First we will loop through the items and get the comparator from a callback
// function which we were given. Then, we will sort the returned values and
// and grab the corresponding values for the sorted keys from this array.
try {
foreach ($this->items as $key => $value) {
$results[$key] = iconv('UTF-8', 'ASCII//TRANSLIT', $value);
}
} catch (\ErrorException $e) {
// some iconv versions do not support translit of UTF-8
foreach ($this->items as $key => $value) {
$results[$key] = simple_translit($value);
}
}
uasort($results, $caseSensitive ? 'strnatcmp' : 'strnatcasecmp');
// Once we have sorted all of the keys in the array, we will loop through them
// and grab the corresponding model so we can set the underlying items list
// to the sorted version. Then we'll just return the collection instance.
foreach (array_keys($results) as $key) {
$results[$key] = $this->items[$key];
}
return new static($results);
});
/** Use this array as keys, callback to produce values */
Collection::macro('asKeys', function ($callback) {
$results = [];
$callback = $this->valueRetriever($callback);
foreach ($this->items as $value) {
$results[$value] = $callback($value);
}
return new static($results);
});
/** Trim all strings */
Collection::macro('trim', function () {
return $this->map(function($x) {
return trim($x);
});
});
/** Trim all strings */
Collection::macro('without', function ($keys) {
if (!is_array($keys)) $keys = [$keys];
return $this->filter(function($v, $k) use($keys) {
return !in_array($k, $keys);
});
});
// --- kpluck ---
Arr::macro('kpluck', function ($array, $value, $key) {
$results = [];
list($value, $key) = static::explodePluckParameters($value, $key);
foreach ($array as $origKey => $item) {
$itemValue = data_get($item, $value);
// THIS was changed to use origKey
if (is_null($key)) {
$results[$origKey] = $itemValue;
} else {
$itemKey = data_get($item, $key);
$results[$itemKey] = $itemValue;
}
}
return $results;
});
/** Pluck, but preserving keys. Single level only! */
Collection::macro('kpluck', function ($value, $key = null) {
return new Collection(Arr::kpluck($this->items, $value, $key));
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}