Skip to content

Instantly share code, notes, and snippets.

View lsloan's full-sized avatar

Mr. Lance E Sloan «UMich» lsloan

  • Teaching and Learning (@tl-its-umich-edu) at University of Michigan: Information and Technology Services
  • Ann Arbor, Michigan, USA
  • 21:45 (UTC -04:00)
  • X @lsloan_umich
View GitHub Profile
@lsloan
lsloan / ksortRecursive
Last active August 29, 2015 14:20 — forked from cdzombak/README.md
Resursive version of PHP's ksort.
See README.md for details.
@lsloan
lsloan / enum.py
Last active August 29, 2015 14:20
Pure Python implementation of an enumerated type
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['reverse_mapping'] = reverse
return type('Enum', (), enums)
def example():
Numbers = enum(ONE=1, TWO=2, THREE='three')
print Numbers.ONE
print Numbers.THREE
@lsloan
lsloan / JsonSortTest.php
Last active August 29, 2015 14:20
My attempt to recursively sort data in nested arrays and objects. This is helpful for sorting data in JSON by key.
<?php
class JsonSortTest extends PHPUnit_Framework_TestCase {
public $fixtureFilename = 'fixture.json';
public $outputFilename = 'output.json';
public $fixtureJson;
function ksortObjectsRecursive(&$data, $sortFlags = SORT_REGULAR) {
if (!function_exists('ksortObjectsRecursiveCallback')) {
function ksortObjectsRecursiveCallback(&$data, $unusedKey, $sortFlags) {
@lsloan
lsloan / getNewGUID.php
Created May 12, 2015 17:39
A method for creating GUIDs for PHP, even if com_create_guid() isn't available.
private function getNewGUID($wrapWithBraces = false) {
if (function_exists('com_create_guid')) {
return com_create_guid();
} else {
mt_srand((double)microtime() * 10000); //optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$uuid = ($wrapWithBraces ? '{' : '')
. substr($charid, 0, 8) . '-'
. substr($charid, 8, 4) . '-'
. substr($charid, 12, 4) . '-'
@lsloan
lsloan / phpunit_fluent_mock.php
Last active July 12, 2023 17:09
Mocking a Fluent Interface with PHPUnit
/*
* With PHPUnit one can easily mock a class that implements a fluent interface.
*
* This will make any method call to your mock object return a reference to itself.
*/
$mock = $this->getMock('MyClass');
$mock->expects($this->any())
->method(new PHPUnit_Framework_Constraint_IsAnything())
->will($this->returnSelf());

This is a list of tips for the Vim editor

@lsloan
lsloan / GitHub workflow.gv
Last active February 27, 2018 22:27
Diagram of a common GitHub workflow (short URL: https://goo.gl/79JCB9)
digraph G {
label = "GitHub Workflow\nSource: https://goo.gl/79JCB9"
labelloc = "t"
subgraph cluster_upstream {
label = upstream
style = filled
color = lightgrey
node [style = filled color = white]
<?php
/**
* Class ClassUtil
*
* Provide useful methods to overcome OOP shortcomings in some versions of PHP.
*/
class ClassUtil {
/**
* The "::class" notation isn't available until PHP 5.5. This method is a workaround for
* older versions of PHP.
@lsloan
lsloan / SplEnumPlus.php
Created June 12, 2015 14:26
My subclass of PHP's enum class, SplEnum, from the spl_types extension on PECL. Adds a method to indicate whether an enum has a desired key.
<?php
class SplEnumPlus extends \SplEnum {
static function hasKey($key) {
$foundKey = false;
try {
$enumClassName = get_called_class();
new $enumClassName($key);
$foundKey = true;
} finally {
@lsloan
lsloan / BetterEnum.php
Last active April 12, 2024 01:02
BetterEnum.php: A pure-PHP alternative to SplEnum, although only a 99% compatible replacement for it.
<?php
/**
* Class BetterEnum
*
* A pure-PHP alternative to SplEnum, although only a 99% compatible replacement for it.
*
* See: http://php.net/manual/en/class.splenum.php
*
* To declare an enum class, subclass BetterEnum and define the names and values as constants.