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:315880
Created February 26, 2010 16:34
Get weather w/o API keys
<?php
/**
* Small, free, weather web-service using Yahoo APIs that don't require an API key.
*/
// Fetch zip code
$location = isset($_GET['location']) ? $_GET['location'] : 'Tempe,AZ';
// Use YQL to get the WOEID by the location
$query = 'select * from geo.places where text="'.$location.'"';
@jeremeamia
jeremeamia / gist:321200
Created March 3, 2010 23:42
Route with subdirectory - Ko3
<?php
// Thanks for the help Zeelot
// Admin Route
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'account',
'action' => 'login',
@jeremeamia
jeremeamia / gist:345227
Created March 26, 2010 18:39
Find approximate time passed - Ko3
<?php // Uses Kohana 3 Framework
function time_ago($timestamp)
{
$time_ago = Date::span($timestamp);
foreach ($time_ago as $unit => $value)
{
if ($value > 0)
{
return 'about '.$value.' '.Inflector::singular($unit, $value).' ago';
}
@jeremeamia
jeremeamia / gist:369111
Created April 17, 2010 00:08
Check event bindings
// Adds a hasEvent() function for jQuery objects.
// Example: var hasClickEvent = $('a.add').hasEvent('click');
(function($){
$.fn.hasEvent = function(eventType){
var events = this.data("events");
return (events && events[eventType]);
};
})(jQuery);
@jeremeamia
jeremeamia / gist:397927
Created May 11, 2010 21:36
Effects of __isset() on empty()
<?php
// Demonstrating how the presence of __isset() method affects accessing object members
class Person implements ArrayAccess {
protected $_data = array();
public function __construct($first_name, $last_name)
{
@jeremeamia
jeremeamia / gist:409121
Created May 21, 2010 17:27
User Impersonation - Ko3
<?php defined('SYSPATH') or die('No direct script access.');
// Extending Auth to support user impersonation functions
class Auth extends Kohana_Auth {
public function impersonate(ORM $user)
{
if ( ! $user->loaded())
throw new Exception;
@jeremeamia
jeremeamia / gist:438122
Created June 14, 2010 19:05
Code Golf: Ordinal suffix
<?php // Code Golf: Return a number with its ordinal suffix (eg. 31 -> 31st)
error_reporting(E_ALL ^ E_NOTICE);
function ordinal($n)
{
return($n%=100)>10&&$n<14?th:date(S,mktime(0,0,0,0,$n%10,0));
}
@jeremeamia
jeremeamia / gist:442647
Created June 17, 2010 19:27
Find last child class
<?php // This script is an example of how you can find the last item in a non-branching inheritance hierarchy
// Declaring classes
class food {}
class cheese extends food {}
class cheddar extends cheese {}
class white_cheddar extends cheddar {}
// Finds the last child in a non-branching inheritance hierarchy
function get_last_child($parent)
@jeremeamia
jeremeamia / gist:445875
Created June 20, 2010 14:22
Extendible singleton class for PHP 5.3
<?php
class Singleton
{
public static function instance()
{
static $instance;
$class = get_called_class();
return $instance ?: ($instance = new $class);
}
@jeremeamia
jeremeamia / gist:450658
Created June 23, 2010 22:34
ObjectAccess interface
<?php // PHP has a ArrayAccess interface, why not an ObjectAccess interface?
interface ObjectAccess
{
public function __isset($key);
public function __get($key);
public function __set($key, $value);
public function __unset($key);
}