Skip to content

Instantly share code, notes, and snippets.

View mattparker's full-sized avatar

Matt Parker mattparker

View GitHub Profile
@mattparker
mattparker / CustomerSpecificationSet.php
Last active August 29, 2015 13:56
Building up specifications
<?php
// This relates to Matthias Verraes talk on unbreakable domain models
// (which is very good). Slides are http://verraes.net/2013/06/unbreakable-domain-models/
// and this is about the end of the talk (about three-quarters through the slide deck).
//
// I like the whole specification thing, but want to be able to combine them
// dynamically (in the language of the talk, the tenant can create their own
// specification of what a premium custom is), using building blocks we provide.
class CustomerSpecificationSet
@mattparker
mattparker / SqlSpecification.php
Last active August 29, 2015 13:56
Including SQL in Specifications
<?php
// This is another response to Mattias Verraes talk on unbreakable domain
// models (slides at http://verraes.net/2013/06/unbreakable-domain-models/)
// Near the end there's a bit where he suggests you implement business logic
// rules initially as code (i.e. an array filter) and secondly as sql.
// You can then test that these are consistent (though the tests in the talk
// don't show correctness, there needs to be a test against a known good result
// too). But with that, it seems like a nice way to have testable SQL.
// A problem for me with the example (which is obviously shortened for the slide example)
<?php
// Domain expert says:
// Tenant can construct their own 'premium customer specification' from a list of ingredients
// Tenant chooses 3 orders, one in the last month:
$spec1 = new CustomerWith3OrdersIsPremium();
$spec2 = new CustomerWithRecentOrderIsPremium();
$allSpecs = new CompositeCustomerSpecification();
$allSpecs->add($spec1)->add($spec2);
@mattparker
mattparker / pre-post-conditions.js
Created March 18, 2014 20:36
pre and post conditions in js
// this: https://twitter.com/raganwald/status/445938910808391680
// Wrap a function f (which accepts two arguments, and b) to enforce
// preconditions (if I understand them correctly) are guards on
// parameter values
// You can also optionally add a postcondition afterwards
// though it'd be nicer to add this in the main preCondition function really
@mattparker
mattparker / super.php
Created April 22, 2014 13:13
why php is super
<?php
class Marvellous implements Community {
public function smart () {
return true;
}
public function helpful () {
return true;
}
public function join (Anyone $person) {
return true;
@mattparker
mattparker / mongo-test.php
Last active August 29, 2015 14:06
Using breakpoints to trigger deprecated errors
<?php
error_reporting(E_ALL);
$host = 'mongodb://127.0.0.1/test';
echo "\n Testing deprecated MongoClient::connected property \n";
$client1 = new \MongoClient($host);
echo "done"; // breakpoint on this line triggers deprecation notice
@mattparker
mattparker / pic-of-a-page
Last active August 29, 2015 14:14
Makes an image out of the content of a webpage
javascript:(function () {
var d = document.createElement('canvas'),
ctx = d.getContext('2d'),
w = window.innerWidth,
h = window.innerHeight,
x = 0,
code_to_0256 = function (code) {
if (code < 32 || code > 127) {
code = 90;
@mattparker
mattparker / tweettopic
Last active August 29, 2015 14:14
picture your tweet
javascript:(function () {
var d = document.createElement('canvas'),
ctx = d.getContext('2d'),
tweet = document.querySelector('.expanded-conversation .tweet .tweet-text'),
box,
w,
h = 80,
x = 0,
code_to_0256 = function (code) {
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.8.0r4/build/reset-fonts-grids/reset-fonts-grids.css&2.8.0r4/build/assets/skins/sam/skin.css">
<style>
.yui-skin-sam .yui-toolbar-container .yui-toolbar-personal {
width: 120px;
}
</style>
(function() {
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event,
Lang = YAHOO.lang,
Toolbar = YAHOO.widget.Toolbar;
/**
* The Rich Text Editor is a UI control that replaces a standard HTML textarea; it allows for the rich formatting of text content, including common structural treatments like lists, formatting treatments like bold and italic text, and drag-and-drop inclusion and sizing of images. The Rich Text Editor's toolbar is extensible via a plugin architecture so that advanced implementations can achieve a high degree of customization.
* @constructor
* @class SimpleEditor