Skip to content

Instantly share code, notes, and snippets.

View chadicus's full-sized avatar

Chad Gray chadicus

View GitHub Profile
@chadicus
chadicus / setNested.php
Created May 10, 2023 23:49
Simple method to set a value in an array by a delimited key
public static function setNested(array &$array, string $delimitedKey, $value, $delimiter = '.')
{
$parts = explode($delimiter, $delimitedKey);
$first = array_shift($parts);
if (empty($parts)) {
$array[$first] = $value;
return;
}
$array = $array + [$first => []];
@chadicus
chadicus / jsonf.php
Created October 9, 2020 12:53
Simple php script to format the contents of a json file, optionally writing to a separate output file.
#!/usr/bin/php
<?php
function stop($message)
{
fputs(STDOUT, "{$message}\nUsage: jsonf file [newfile]\n");
exit(0);
}
if (!isset($argv[1])) {
@chadicus
chadicus / git-ignore.php
Created October 8, 2020 23:46
Simple PHP Script for maintaining a .gitignore file
#!/usr/bin/php
<?php
function stop($message)
{
fputs(STDOUT, "{$message}\nUsage: git-ignore pattern\n");
exit(0);
}
function format(array $input) : array
@chadicus
chadicus / Arrays.php
Created March 19, 2019 13:16
Class to assist with array operations
/**
* Static utility class for working with the PHP array primitive type.
*/
abstract class Arrays
{
/**
* Unsets and returns the value at index $key in the given array.
*
* Example:
* <br />
@chadicus
chadicus / Loop.php
Created March 19, 2019 13:13
Class to perform operations at specific iterations of a loop
final class Loop
{
/**
* @var object
*/
private $counter;
/**
* @var bool
*/
<?php
abstract class Minutes
{
const ONE_MINUTE = 1;
const HOUR_IN_MINUTES = self::ONE_MINUTE * 60;
const DAY_IN_MINUTES = self::HOUR_IN_MINUTES * 24;
const WEEK_IN_MINUTES = self::DAY_IN_MINUTES * 7;
const MONTH_IN_MINUTES = self::DAY_IN_MINUTES * 30;
<?php
abstract class Seconds
{
const ONE_SECOND = 1;
const MINUTE_IN_SECONDS = self::ONE_SECOND * 60;
const HOUR_IN_SECONDS = self::MINUTE_IN_SECONDS * 60;
const DAY_IN_SECONDS = self::HOUR_IN_SECONDS * 24;
const WEEK_IN_SECONDS = self::DAY_IN_SECONDS * 7;
@chadicus
chadicus / LinqCollection.php
Created November 20, 2018 19:56
a collection object with linq methods
<?php
namespace SubjectivePHP\Linq;
use ArrayIterator;
use CallbackFilterIterator;
use Countable;
use Exception;
use Traversable;
use IteratorAggregate;
<?php
use SplFileObject;
final class HeaderStrategy
{
/**
* @var callable
*/
private $getHeadersCallable;
$input = [
[
'id' => 1,
'code' => 'foo',
'extra' => 'abc',
],
[
'id' => 2,
'code' => 'bar',
],