Skip to content

Instantly share code, notes, and snippets.

View alganet's full-sized avatar

Alexandre Gomes Gaigalas alganet

  • São Paulo, SP, Brazil
View GitHub Profile
<?php
$teste = array(
array("foo" => "a", "bar" => "b", "timestamp" => 123456),
array("foo" => "a", "bar" => "b", "timestamp" => 987654),
array("foo" => "a", "bar" => "b", "timestamp" => 654789),
array("foo" => "a", "bar" => "b", "timestamp" => 456123)
);
function sortTimestamp($a, $b) {
@alganet
alganet / gist:819098
Created February 9, 2011 19:33
less javaish SplClassLoader
<?php
spl_autoload_register(
function($className) {
$fileParts = explode('\\', ltrim($className, '\\'));
if (false !== strpos(end($fileParts), '_'))
array_splice($fileParts, -1, 1, explode('_', current($fileParts)));
$file = implode(DIRECTORY_SEPARATOR, $fileParts) . '.php';
@alganet
alganet / stringf.php
Created February 11, 2011 14:15
Nice little string formatter
<?php
function stringf($template, array $vars=array())
{
return preg_replace_callback(
'/{{(\w+)}}/',
function($match) use(&$vars) { return $vars[$match[1]]; },
$template
);
}
@alganet
alganet / respect_zend.php
Created March 9, 2011 16:05
Exemplo de integração do Respect com Zend 1.x usando closures
<?php
use Respect\Validation\Validator as v;
//Enclosurer
function zv($name, $arguments) {
$name = ucfirst($name);
$validatorName = "Zend_Validate_$name";
$validator = new $validatorName($arguments);
return new v::callback(function($input) use ($validator) {
return $validator->isValid($input);
@alganet
alganet / UsersCollection.php
Created March 17, 2011 00:34
Exploring Respect\Rest concepts
<?php
namespace Users;
use Doctrine\ORM\EntityManager as Entities;
use Doctrine\ORM\EntityNotFoundException;
use Twig_Environment as Templates;
use Respect\Validation\Validatable as Validator;
use Respect\Validation\Exceptions\ValidationException;
@alganet
alganet / infered.php
Created March 28, 2011 01:45
Use cases for Respect\Relational
<?php
/**
* Infered database schema from naming conventions.
* Mapping works without configuration, reflection, annotation, reverse engineering, show tables
* or anything like that at all. All samples below generate a single SQL statement with apropriated joins.
*
* -All tables have a primary key named "id"
* -All foreign keys are named table_id
* -All association tables (N to N) are named table_table
@alganet
alganet / formatSql.php
Created March 31, 2011 15:12
Very, very very simple SQL formatter.
<?php
function formatSql($sql)
{
return preg_replace(
'/(select|from|(inner|left) join|where|limit|update|set|insert|values)/i',
"\n$1\n ",
str_replace("\n", '', $sql)
);
}
<?php
ob_start();
register_shutdown_function(function(){
$cacheFile = __DIR__.'/cache/'.date('Y-m-d');
$cacheTime = 18000;
if (file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile)) {
ob_end_flush();
readfile($cacheFile);
}
@alganet
alganet / microfun.php
Created April 11, 2011 04:10
Toying with microframework concepts
<?php
//users
Web::serve('/users', function() {
return 'Hello World';
});
//users/alganet
Web::serve('/users/*', function($screenName) {
return "Hello $screenName";
@alganet
alganet / basic_routing.php
Created April 12, 2011 18:48
Another microframework concept
<?php
$r = new Respect\Rest\Router;
$r->get('/', function() {
return 'Hello World';
});
$r->get('/users', function() use($users) {
return $users->list()->toHTML(); //sample model/view call