Skip to content

Instantly share code, notes, and snippets.

View primitive-type's full-sized avatar

Percy Hatcherson primitive-type

View GitHub Profile
@primitive-type
primitive-type / GenericSerializable.php
Last active December 15, 2015 06:19
Generic exchangeArray() method in a parent class
<?php
class GenericSerializable
{
public $id = null;
public $name = 'genericThing';
public $someProperties = array();
public function exchangeArray($data)
{
// Includes all public variables of children classes
@primitive-type
primitive-type / Cache.php
Created January 21, 2014 22:16
Implementation of a cache storage adapter implementing Zend\Authentication\Storage\StorageInterface for use with Zend\Authentication\AuthenticationService. This is modeled directly after Zend\Authentication\Storage\Session in order to mimic that class's behavior when used with the aforementioned AuthenticationService class.
<?php
namespace Zend\Authentication\Storage;
use Zend\Authentication\Storage\StorageInterface as AuthStorageInterface;
use Zend\Cache\Storage\StorageInterface as CacheStorageInterface;
class Cache implements AuthStorageInterface
{
/**
@primitive-type
primitive-type / cdn_mime_types.php
Created August 6, 2014 16:42
Get MIME types for CDN hosted files
<?php
$fileUrl = 'http://t2.gstatic.com/images?q=tbn:ANd9GcS8ntYWjylXybJ4WIr5WvFJiknlXdS8jkhwpaRXqhCY3w2vFyuR-Q';
// Get the file's MIME type
// This method is used because many file URLs hosted on CDNs do not contain file extensions
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mediaType = $finfo->buffer(file_get_contents($fileUrl));
// Is this an image, audio, text, video, etc.?
@primitive-type
primitive-type / rename-field.js
Created January 21, 2016 23:31
Rename Field (ReQL)
r.db('local').table('TableName')
.replace(function (doc) {
return doc.merge({
newFieldName: doc('oldFieldName')
}).without('oldFieldName');
});
@primitive-type
primitive-type / two-digit-school-year.ex
Created March 6, 2017 23:02
Two Digit School Year (e.g., "17-18")
defmodule SchoolYear do
# "17-18"
def get_two_digit_school_year do
[DateTime.utc_now.year, DateTime.utc_now.year + 1] # calendar years
|> Enum.map(
(fn (calendar_year) -> Integer.digits(calendar_year)
|> Enum.slice(2, 2)
|> Enum.join end)
)
|> Enum.join("-")
@primitive-type
primitive-type / snake_case_to_start_case.ex
Created March 24, 2017 19:22
Convert Snake Case to Start Case
def snake_case_to_start_case(snake_case) do
snake_case
|> String.replace("_", " ")
|> String.split
|> Enum.map(&String.capitalize(&1))
|> Enum.join(" ")
end