Skip to content

Instantly share code, notes, and snippets.

View aklump's full-sized avatar

Aaron Klump aklump

View GitHub Profile
@aklump
aklump / QueryAddDateCondition.php
Last active March 28, 2024 22:36
Query conditions for dates with automatic timezone handling.
<?php
namespace Drupal\se_core\Helpers;
use DateTimeInterface;
use DateTimeZone;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use RuntimeException;
@aklump
aklump / drupal_version.php
Created March 28, 2024 00:38
Get any Drupal version.
$version = class_exists('\Drupal') ? Drupal::VERSION : NULL;
$version = $version ?? (defined('VERSION') ? constant('VERSION') : NULL);
@aklump
aklump / ThrowShellError.php
Last active November 21, 2023 01:38
Throws a PHP exception based a shell script error with accurate basename and best-guess line-number.
<?php
/**
* Throw an exception for a bash file, message and exit code.
*
* This class creates an exception where getFile returns a path with the correct
* basename, and in some cases the correct line number where the exit code
* occurred.
*/
class ThrowShellError {
@aklump
aklump / HumanList.php
Last active November 8, 2025 17:24
csv using "and"
<?php
/**
* A class that formats an array of items into a human-readable string.
*
* When the class is invoked as a callable, it formats the provided array
* by joining all elements with commas and adding "and" before the last item.
* This is typically used for creating natural language representations of lists.
*
* Methods:
class UpperCamel {
public function __invoke(string $value): string {
$value = preg_replace('/[\s\-_]/s', ' ', $value);
$value = trim(preg_replace('/[A-Z]/', ' \0', $value));
$value = preg_replace('/(\s)\s+/s', '\1', $value);
$value = ucwords($value);
$value = preg_replace('/\s+/s', '', $value);
return $value;
@aklump
aklump / EscapeDoubleQuotes.php
Last active September 22, 2023 02:52
Add backslash to double quotes in a string where not already present.
<?php
/**
* Add backslash to double quotes unless already present.
*/
class EscapeDoubleQuotes {
public function __invoke(string $query): string {
// This is four-backslashes because of preg--confusing--that's why this
// class exists to remove the confusion.
<?php
namespace AKlump\PHPUnit;
/**
* Given a mock object make it iterable using $data.
*
* @url https://stackoverflow.com/a/32422586
*
* @code
@aklump
aklump / _lifted-corners.scss
Last active January 20, 2018 21:04
Create that cool lifted corners 3-D effect on an image: `span>img`.
//
// For a given html structure:
//
// <span class="image"><img src="..."></span>
//
// Use this in your SCSS file:
//
// .image {
// @include lifted-corners()
// }

Keybase proof

I hereby claim:

  • I am aklump on github.
  • I am aklump (https://keybase.io/aklump) on keybase.
  • I have a public key ASB_LuPX2hRCGOYe-MYF_LW--oONccyBnVe-8WfoqQzSdgo

To claim this, I am signing this object:

@aklump
aklump / font-size-range.scss
Last active August 8, 2017 22:01
Mixin for variable font sizing between a minimum and maximum using media queries and the vw unit.
//
//
// Mixin for variable font sizing between two font sizes with min and max constraints.
//
// Ever want to declare a title to grow from 28px to 60px based on browser width, but always be at least 28px and never
// more than 60px? This SASS mixin does the heavy lifting in terms of math and media queries to figure the necessary CSS
// to do just that.
//
// @code
// @include loft-sass-font-size-range(28px to 46px at 960px);