Skip to content

Instantly share code, notes, and snippets.

View noclat's full-sized avatar

Nicolas Torres noclat

View GitHub Profile
/*
Small timer script by Nicolas Torres
(WIP: needs settings & a some few more helpers)
Usage:
<span id="chrono"></span> // set an element
chrono.start() // starts the timer
chrono.toggle() // play or pause the timer
chrono.stop() // stop and reset the timer
*/
@noclat
noclat / chainable-objects.php
Last active January 30, 2016 09:39
KirbyCMS-inspired chainable object class.
<?php
# Chainable objects
# Ex: echo $films->filterBy('rating', 5)->sortBy('release')->flip()->first()->title();
class obj implements Iterator {
private $_ = array();
public function __construct($arr = array()) { $this->_ = is_array($arr[0]) ? array_map(function($a) { return is_object($a) ? $a : new obj($a); }, $arr) : $arr; }
public function __set($k, $v) { $this->_[$k] = $v; }
public function __get($k) { return $this->get($this->_, $k); }
public function __call($k, $args) { return $this->get($this->_, $k, $args[0]); }
private function get($arr, $k, $date = false) { return isset($arr[$k]) ? (($date AND !is_numeric($arr[$k]) AND strtotime($arr[$k]))?new DateTime($arr[$k]):$arr[$k]) : false; }