Skip to content

Instantly share code, notes, and snippets.

Avatar

Ralph Schindler ralphschindler

View GitHub Profile
@ralphschindler
ralphschindler / colorize.php
Created Dec 26, 2014
CLI Colorization via HTML style tagging function. This solution is UNLICENSED, feel free to do whatever you want with it.
View colorize.php
<?php
/** @license http://unlicense.org/UNLICENSE */
function colorize($string, $useAnsi = null)
{
if (is_null($useAnsi)) {
$useAnsi = function_exists('posix_isatty');
}
@ralphschindler
ralphschindler / scalar-type-signatures.php
Created Feb 10, 2015
What happened when this was suggested many years ago?
View scalar-type-signatures.php
<?php
// coercive types, as per PersonFactory author's specification
class ElePHPant {
public $name, $age, $cuteness, $evil;
public function __construct(~string $name, ~int $age, ~float $cuteness, ~bool $evil) {
$this->name = $name;
$this->age = $age;
$this->cuteness = $cuteness;
$this->evil = $evil;
@ralphschindler
ralphschindler / conditional-ternary-eliminate-waste.php
Created Mar 4, 2011
Usage of the Conditional Ternary operator to reduce brace and newline waste when processing optional method parameters
View conditional-ternary-eliminate-waste.php
<?php
class Coordinate
{
protected $x;
protected $y;
public function __construct($x = null, $y = null)
{
(empty($x)) ?: $this->setX($x);
View create-service-repo.php
<?php
$service = 'Amazon';
chdir(__DIR__);
`git clone ./zf2 ZendService{$service}-library`;
`git clone ./zf2 ZendService{$service}-tests`;
chdir(__DIR__ . '/ZendService' . $service . '-library');
View GitHubController.php
<?php
namespace GitHub\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Http\ClientStatic;
use GitHubAPIv3\UserAPI;
View formatting_ascii_output.php
<?php
function getFormattedOutput($string)
{
$tags = array(
'bold' => 1, 'dark' => 2, 'italic' => 3, 'underline' => 4, 'blink' => 5, 'reverse' => 7, 'concealed' => 8,
'black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37,
'bg_black' => 40, 'bg_red' => 41, 'bg_green' => 42, 'bg_yellow' => 43, 'bg_blue' => 44, 'bg_magenta' => 45, 'bg_cyan' => 46, 'bg_white' => 47
);
View sig.php
<?php
class SomeClass {
/**
* @signature __construct(string $authentication) 40 byte auth code
* @signature __construct(string $username, string $password) Github Username, Github Password
* @signature __construct(array $authentication) array('username' => 'xx', 'password' => xxx)
*
* @param string|array $authentication
*/
View apache-ab.txt
$ ab -k -n1 http://localhost/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software: Apache/2.2.22
Server Hostname: localhost
View matchNamedAguments.php
<?php
function matchNamedArguments($target, $arguments = array())
{
if (!is_array($arguments) && !$arguments instanceof \ArrayAccess) {
throw new \InvalidArgumentException('$arguments for ' . __CLASS__ . ' must be array or ArrayAccess');
}
if (is_string($target) || $target instanceof \Closure) {
$r = new \ReflectionFunction($target);
@ralphschindler
ralphschindler / entity-mapper-via-dynamic-proxy-object.php
Last active Feb 15, 2016
The "No-framework ORM tool for generating proxies capable of strategically mapping data into clean POPO entities without bleeding concerns or doesn't require a tool to write generated code to disk in under 100 lines of code" prototype. </runon-and-exhale>
View entity-mapper-via-dynamic-proxy-object.php
<?php
namespace EntityMapperFramework {
class EntityMapper {
public function __construct() {
stream_wrapper_register('dynamicproxygenerator', __NAMESPACE__ . '\DynamicProxyGeneratorStream');
}
public function createProxy($entity) {
$class = get_class($entity);