Skip to content

Instantly share code, notes, and snippets.

View dsantuc's full-sized avatar

David Santucci dsantuc

  • Bindable, Inc
  • Boston, MA
View GitHub Profile
@dsantuc
dsantuc / ds_array_merge_recursive.php
Created March 6, 2020 22:07
A more intuitive implementation of PHP's array_merge_recursive()
<?php
function ds_array_merge_recursive () {
$args = func_get_args();
$numArgs = count($args);
if ($numArgs < 1) {
trigger_error(__FUNCTION__ . "() expects at least one parameter, $numArgs given", E_USER_WARNING);
return null;
}
<?php
require_once("vendor/autoload.php");
use Aws\Sqs\SqsClient,
GuzzleHttp\Promise;
class AsyncQueueMonitor {
protected $sqsClient;
@dsantuc
dsantuc / word-number.php
Created November 10, 2018 04:55
Computing the "number" of a word.
<?php
/*
Given a word between 1 and 20 characters in length, write a command-line script that outputs its "number".
A word's "number" is its alphabetical ordinal position in the set of permutations of its letters.
Examples:
ABAB = 2
@dsantuc
dsantuc / ipconv.js
Last active June 14, 2018 15:46
JavaScript to convert IPv4 addresses from dotted-decimal notation to integers and back.
function ip2int (ip) {
var ipVal = 0;
ip.split(".").forEach(function (octet, i, arr) {
var exp = 8 * (arr.length - i - 1);
ipVal += octet * Math.pow(2, exp);
});
return ipVal;
}
// Behat code to wait for ajax calls to complete
public function waitForAjax ($seconds=5) {
if(!$this->getSession()->wait((1000 * $seconds), "jQuery.active == 0")) {
throw new Exception("Ajax calls still pending after $seconds seconds.");
}
}
@dsantuc
dsantuc / invert-tree.php
Last active March 14, 2018 21:48
Inverting a binary tree
// invert a binary tree with root node $rootNode
function isLeafNode ($node) {
return is_null($node->left) && is_null($node->right);
}
function invert (&$node) {
if (isLeafNode($node)) {
return;
@dsantuc
dsantuc / behat-auth.php
Last active March 14, 2018 21:53
Pattern for reusing cookie-based authentication tokens in BeHat
// in FeatureContext.php
// Store session tokens for reauthentication
private static $credentialStore = array();
// ... other code ...
/**
* Authenticates a user with password from configuration.
*
@dsantuc
dsantuc / behat-cookies.php
Last active March 31, 2020 02:37
Getting all cookies from various BeHat/Mink browser drivers
public function getAllCookies ($driver) {
$cookies = array();
if ($driver instanceof Behat\Mink\Driver\BrowserKitDriver) {
$cookies = $this->getBrowserKitCookies($driver);
}
else if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) {
$cookies = $this->getSeleniumCookies($driver);
}
else if ($driver instanceof Behat\Mink\Driver\ZombieDriver) {
@dsantuc
dsantuc / ses-smtp-credentials.php
Last active February 14, 2018 20:00
Deriving Amazon SES SMTP credentials from IAM credentials in PHP.
function getSMTPPassword ($key) {
$message = "SendRawEmail";
$hexVersion = "02";
$hexSignature = hash_hmac("sha256", $message, $key);
return base64_encode(hex2bin($hexVersion . $hexSignature));
}