Skip to content

Instantly share code, notes, and snippets.

View jesseschalken's full-sized avatar

Jesse Schalken jesseschalken

View GitHub Profile
@jesseschalken
jesseschalken / FileMode.php
Created November 4, 2015 06:09
Parser/Unparser of Unix file mode
<?php
/**
* @param int $int
* @param int $offset
* @return bool
*/
function get_bit($int, $offset) {
return (bool)((1 << $offset) & $int);
}
@jesseschalken
jesseschalken / ts-modules.md
Last active November 24, 2015 08:35
TypeScript modules

The export of a module is effectively a single JavaScript value, usually an object with individual functions, classes, constants etc as properties. The exported value can be set with export = ... or individual things can be exported as properties of an object by putting export before its definition. For example, the following modules are equivelant:

export function foo() {}
export class Blah {}
export = {
 foo: function () {}
@jesseschalken
jesseschalken / js-classes.md
Last active April 3, 2017 02:02
JavaScript classes

JavaScript Objects, Classes, Functions and Prototypes

Objects

A JavaScript object is a map from keys (or "properties") to values, supporting the usual methods of a map/dictionary (set, get, delete, exists, iterate):

// set property
object.proprty = value;
object['property'] = value;
@jesseschalken
jesseschalken / php-cross-compiler.php
Last active May 26, 2016 02:32
Compile PHP 7.0 to PHP 5.3
<?php
const PHP50 = 50000;
const PHP51 = 50100;
const PHP52 = 50200;
const PHP53 = 50300;
const PHP54 = 50400;
const PHP55 = 50500;
const PHP56 = 50600;
const PHP70 = 70000;
@jesseschalken
jesseschalken / gist:de2e35d81d317235d289
Last active May 5, 2016 06:18
Trait inlining process
  1. Start with all methods from all used traits.
  2. Resolve conflicting method names using "insteadof" rules.
    • Each trait offering a method may be excluded as the implementation of that method by being mentioned on the right hand side of the "insteadof" rules for the method.
    • A trait cannot be mentioned on the right hand side of the "insteadof" rules for a method more than once in total.
    • Of all the used traits which offer the method, either one or none must remain as the implemenation of the method. If none remain, the method does not exist after this step.
    • The trait name used on the left hand side of an "insteadof" rule has no meaning, but must be one of the used traits which offer the method and must not be mentioned on the right hand side of the same "insteadof" rule.
  3. Add aliases.
    • Qualified aliases use the method implementation from the specified trait.
    • Unqualified aliases use the method implementation from the first used trait that offers it.
  • The method visibility
<?php
// See http://www.unicode.org/versions/corrigendum1.html Table 3.1B. Legal UTF-8 Byte Sequences
$utf8 = "^([\\\x00-\\\x7F]|[\\\xC2-\\\xDF][\\\x80-\\\xBF]|\\\xE0[\\\xA0-\\\xBF][\\\x80-\\\xBF]|[\\\xE1-\\\xEF][\\\x80-\\\xBF][\\\x80-\\\xBF]|\\\xF0[\\\x90-\\\xBF][\\\x80-\\\xBF][\\\x80-\\\xBF]|[\\\xF1-\\\xF3][\\\x80-\\\xBF][\\\x80-\\\xBF][\\\x80-\\\xBF]|\\\xF4[\\\x80-\\\x8F][\\\x80-\\\xBF][\\\x80-\\\xBF])*$";
<?php
namespace JesseSchalken;
/**
* @param mixed $x
* @return string
*/
function assert_string($x) {
if (is_string($x)) {
@jesseschalken
jesseschalken / README.md
Last active April 6, 2016 04:20
Transaction API for nested transactions in SQL databases. PHP 5.3+

Usage

This library implements nesting of transactions so code that uses transactions can be safely and arbitrarily composed.

There is a stack of in-progress transactions. Calling $txn = new Transaction(...) adds a transaction to the top of the stack. Calling $txn->commit() removes a transaction from the stack (and releases the savepoint). Calling $txn->rollback() rolls back a transaction and all transactions above it on the stack.

The transaction at the bottom of the stack represents a real START TRANSACTION/COMMIT/ROLLBACK. Transactions above the root transaction represent a SAVEPOINT .../ROLLBACK TO .../RELEASE SAVEPOINT .... Committing the root transaction does not COMMIT until all transactions on the stack have been committed or rolled back.

Nothing happens when a Transaction object falls out of scope. A transaction that is never committed or rolled back remains stuck on the stack and prevents the root transaction from committing. A Transaction object which commits on `__d

interface Interface_ {
public function getName();
public function getMethods();
public function getExtends();
}
interface HasTraits {
public function getTraits():array;
@jesseschalken
jesseschalken / Uri.php
Last active January 28, 2022 08:14
Simple implementation of \Psr\Http\Message\UriInterface
<?php
class Uri implements \Psr\Http\Message\UriInterface {
const PASS = 'pass';
const USER = 'user';
const PORT = 'port';
const SCHEME = 'scheme';
const QUERY = 'query';
const FRAGMENT = 'fragment';
const HOST = 'host';