Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
kmuenkel / helpers.php
Created February 6, 2023 17:24
Compile arguments in a Laravel fashion for closure bindings
<?php
if (!function_exists('array_is_assoc')) {
/**
* Determine if the given array is associative or numerically indexed
*
* @param array $array
* @return bool
*/
function array_is_assoc(array $array)
@kmuenkel
kmuenkel / helpers.php
Created March 15, 2023 12:59
Wildcard support for get path
<?php
if (!function_exists('get_path')) {
function get_path(array $data, $path)
{
$nodes = is_string($path) ? explode('.', $path) : $path;
$node = array_shift($nodes);
$getNext = function ($item) use ($nodes) {
return $nodes ? (is_array($item) ? get_path($item, $nodes) : null) : $item;
@kmuenkel
kmuenkel / NodePath.php
Last active November 21, 2024 22:07
Dot-delimited recursive array data extraction with wildcard and nested expression support
<?php
namespace Tests;
class NodePath
{
private const string ESCAPED = '\\\\';
private const string UNESCAPED = '(?<!\\\\)';
@kmuenkel
kmuenkel / sortRecursiveHelper.php
Created May 24, 2023 03:36
Laravel Collection Macro to Sort Results and All Nested Entities
<?php
function sortRecursive()
{
$sortRecursive = function (callable $sortBy = null, $options = SORT_REGULAR, bool $descending = false): Collection {
/** @var Collection|Request $this */
$sort = $sortBy ? fn (array $item): Collection => collect($item)->sortBy($sortBy, $options, $descending) :
fn (array $item): Collection => $descending ? collect($item)->sortDesc() : collect($item)->sort();
<?php
namespace Database\Seeders;
use Str;
use File;
use SplFileInfo;
use ReflectionClass;
use ReflectionException;
use Illuminate\Database\Seeder;
@kmuenkel
kmuenkel / GenerateTableSupportCommand.php
Last active September 25, 2023 19:39
Table support generator aggragate
<?php
namespace App\Console\Commands;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class GenerateTableSupportCommand extends Command
{
@kmuenkel
kmuenkel / DataTransformer.php
Last active February 10, 2025 22:47
Entity to array
<?php
namespace Tests;
use ArrayAccess;
use BackedEnum;
use DateTimeInterface;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
@kmuenkel
kmuenkel / exceptionParser.php
Last active January 16, 2024 05:45
Parse a stack trace from debug_backtrace or an Exception in a more readable format
<?php
function stack(\Throwable|array $context = null, string $prune = 'vendor', int $text = 64, int $arr = 4): void {
$currentTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$current = array_shift($currentTrace);
$caller = implode(':', [$current['file'] ?? '', $current['line'] ?? '']);
$exception = $context instanceof \Throwable ? get_class($context) : '';
$message = $context instanceof \Throwable ? $context->getMessage() : '';
$trace = $context instanceof \Throwable ? $context->getTrace() : ($context ?: $currentTrace);
<?php
namespace Tests\HighOrderReflection;
use BadMethodCallException;
use Closure;
use ReflectionClass;
use ReflectionException;
use Throwable;
use UnexpectedValueException;
<?php
function compileQuery(Chess\DoctrineBundle\Doctrine\ORM\HintedQueryDecorator|\Doctrine\ORM\AbstractQuery $query) {
$sql = $query->getSQL();
$params = array_column(array_map(function (\Doctrine\ORM\Query\Parameter $parameter) {
/** @var mixed|\MyCLabs\Enum\Enum $value */
$value = $parameter->getValue();
$type = $parameter->getType();
$name = $parameter->getName();