Skip to content

Instantly share code, notes, and snippets.

View jeremeamia's full-sized avatar
🌵
WFH in AZ

Jeremy Lindblom jeremeamia

🌵
WFH in AZ
View GitHub Profile
@jeremeamia
jeremeamia / gist:2344113
Created April 9, 2012 15:05
An interesting functional try...catch...finally workflow
<?php
/**
* Uses closures to execute a try...catch...finally group of statements.
*
* @param Closure $try Closure to execute as a try statement.
* @param array $catch Hash exception names as keys and closures (catch statements) as values.
* @param Closure $finally Closure to execute as a finally statement.
* @throws Exception If an excpetion is not caught.
*/
@jeremeamia
jeremeamia / aws-csv-streamwrapper.php
Created June 6, 2014 18:23
Shows how to read a CSV stored in S3 using the AWS SDK for PHP's S3 Stream Wrapper.
<?php
require __DIR__ . '/vendor/autoload.php';
$s3 = Aws\S3\S3Client::factory($config);
$s3->registerStreamWrapper();
$url = 's3://{$bucket}/{$key}';
// Read CSV with fopen
@jeremeamia
jeremeamia / fn.php
Last active August 19, 2022 18:45
Functional magic with PHP 5.6
<?php namespace fn;
/**
* A constant representing an argument to leave a placeholder for when creating
* a partial function application. This value needs to be weird (note the use
* of null bytes) in order to be disambiguated from a normal argument.
*/
const ANY = "\0*\0";
/**
<?php
/**
* A simple class for calculating holidays
*
* Able to calculate dates for holidays based on the stored derivations.
* Holidays other than Easter based on lunar cycles will not be able to be
* calculated. Work should be done to improve this with configuration abilities
*/
class Holiday {
@jeremeamia
jeremeamia / build-zip-from-s3.php
Created August 7, 2014 17:40
Create a zip from objects from S3.
<?php
// These are just the basics for how to do this. Notes:
// - Not fully tested.
// - Not suitable for production.
// - May not work well if large ammounts of data.
// - Requires AWS SDK for PHP and http://php.net/manual/en/book.zip.php
require '/path/to/sdk-or-autoloader';
@jeremeamia
jeremeamia / keybase.md
Created June 10, 2021 23:39
Keybase proof

Keybase proof

I hereby claim:

  • I am jeremeamia on github.
  • I am jeremeamia (https://keybase.io/jeremeamia) on keybase.
  • I have a public key ASCBfb8yAYogwzk5U3UqoPp1oPzXnIUTnCk-9QG7IeIJTQo

To claim this, I am signing this object:

@jeremeamia
jeremeamia / parse-slack-request.php
Created June 8, 2021 17:48
Quick and dirty Slack request parsing in PHP
<?php
function parse_slack_request(string $body): array
{
if (empty($body)) {
return [];
}
if ($body[0] === '{') {
$data = json_decode($body, true);
@jeremeamia
jeremeamia / gist:1002974
Created June 1, 2011 18:42
Creating an ORM architecture compatible with dependency injection and unit testing
<?php
// Domain Classes
abstract class Domain { // Provides means to persist models and understand relationships
public function __construct(Database_Interface $database, Domain_Builder_Base $builder) {/* ... */}
public function find($unique_key) {/* ... */}
public function find_all($unique_key) {/* ... */}
public function save(Domain_Model $model) {/* ... */}
public function find_all_related_to(Domain_Model $model) {/* ... */}
/* ... */
@jeremeamia
jeremeamia / gist:4316182
Last active November 4, 2020 23:41
PHP 5.4 Fun: Adding fully-privileged methods to a class at runtime.
<?php
class MutableMethodsObject
{
protected $methods = array();
public function addMethod($name, Closure $method)
{
if (!is_string($name)) {
throw new InvalidArgumentException("Method name must be a string.");
@jeremeamia
jeremeamia / Fn.php
Last active November 4, 2020 23:41
Fun with closures, callables, and partial function application.
<?php
class Fn
{
const SKIP = '…';
protected $callable;
protected $fixedArgs = [];
public function __construct(callable $callable, array $fixedArgs = [])