Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
kmuenkel / artisan.php
Created June 21, 2021 20:58
Artisan command for Composer packages
#!/usr/bin/env php
<?php
use Illuminate\Config\Repository;use Illuminate\Encryption\Encrypter;define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
@kmuenkel
kmuenkel / TestCase.php
Last active January 3, 2022 01:29
Testbench bug workaround
<?php
namespace Tests;
use Throwable;
use PHPUnit\Util\Test;
use BadMethodCallException;
use Illuminate\Http\Request;
use Faker\Generator as Faker;
use Illuminate\Routing\Router;
@kmuenkel
kmuenkel / TokenHelpers.php
Created January 28, 2021 00:01
PhpUnit Token generators and parsers
<?php
namespace Tests;
use Firebase\JWT\JWT;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\File;
/**
* Trait TokenHelpers
@kmuenkel
kmuenkel / ResponseHelpers.php
Created January 28, 2021 00:01
PhpUnit Request and Response parsers
<?php
namespace Tests;
use Exception;
use DOMDocument;
use ErrorException;
use ReflectionClass;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
@kmuenkel
kmuenkel / CollectionMacroProvider.php
Last active November 4, 2021 16:48
Laravel Collection Macro for a dot-notation version of 'get' with wildcard support
<?php
Collection::macro('getDot', function ($index, $default = null) {
/** @var Collection $this */
$indexes = preg_split('~(?<!\\\)\.~', $index);
$indexes = array_map(function ($index) {
return preg_replace('~\\\.~', '.', $index);
}, $indexes);
@kmuenkel
kmuenkel / closureBinding.php
Last active November 4, 2021 16:49
Allow for consistent syntax when including an array of parameters in Laravel's app() helper function when the concrete binding is a Closure.
<?php
if (!function_exists('function_binding')) {
/**
* @param callable $callback
* @return callable
*/
function function_binding(callable $callback): callable
{
$params = app(ReflectionFunction::class, ['name' => $callback])->getParameters();
@kmuenkel
kmuenkel / JsonSchemaValidator.php
Last active November 4, 2021 16:49
Laravel validator for JSON-Schema
<?php
namespace App\Validators;
use stdClass;
use Illuminate\Support\Arr;
use Opis\JsonSchema\ISchema;
use Illuminate\Support\Fluent;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Facades\File;
@kmuenkel
kmuenkel / AppServiceProvider.php
Created November 24, 2020 17:27
Laravel Validator Fix
<?php
namespace App\Providers;
use ReflectionException;
use Illuminate\Validation\Validator;
use App\Validators\Rules\CustomRule;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Validation\Rule;
use Lti\Overrides\Validation\Validator as ValidatorOverride;
<?php
// Using 'gree/jose', 'phpseclib\Crypt', and 'guzzlehttp/guzzle'
if (!function_exists('is_rsa')) {
/**
* @param string $publicKey
* @return bool
*/
function is_rsa(string $publicKey)
@kmuenkel
kmuenkel / EnumOrFilter.php
Last active November 30, 2020 17:59
Custom Validator Rule that combines 'in' with filter_var() filters.
<?php
namespace App\Validators\Rules;
use Illuminate\Support\Arr;
use Illuminate\Validation\Validator;
use Illuminate\Contracts\Validation\Rule;
use function Lti\{filter_var_multi, get_flag_names, str_putcsv};
/**