Skip to content

Instantly share code, notes, and snippets.

View martinbean's full-sized avatar

Martin Bean martinbean

View GitHub Profile
@martinbean
martinbean / Condition.php
Created January 30, 2014 14:30
A rough, basic query object implementation
<?php
class Condition
{
protected $field;
protected $comparator;
protected $value;
const EQUALS = '=';
const GREATER_THAN = '>';
const LESS_THAN = '<';
@martinbean
martinbean / Adapter.php
Last active July 9, 2018 08:32
Basic ORM sketch that uses value objects, data mappers that persists data to a data source, and adapters for data sources.
<?php
abstract class Adapter
{
protected $config;
protected $connection;
public function __construct($config)
{
$this->config = $config;
@martinbean
martinbean / filtering.php
Last active December 28, 2015 05:29
Very crude way of building up raw SQL clauses for filtering and sorting.
<?php
// whitelist columns to filter by
$filterable = array(
'colour',
'size',
'price'
);
// build up string for WHERE clause
$where = array();
@martinbean
martinbean / NewsArticle.php
Last active December 10, 2015 07:28
Fetching records from one table based on existence of records in a HABTM relation.
<?php
class NewsArticle extends AppModel {
public $name = 'NewsArticle';
public $hasAndBelongsToMany = array(
'Image' => array(
'joinTable' => 'news_articles_images'
)
);
@martinbean
martinbean / google-feed.php
Created December 20, 2012 12:14
Building a Google RSS feed with DOMDocument.
<?php
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$rss = $xml->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:g', 'http://base.google.com/ns/1.0');
$xml->appendChild($rss);
@martinbean
martinbean / gist:4162031
Created November 28, 2012 15:36
Martin’s fantastic naming convention document

Martin’s fantastic naming convention document

Controllers: plural

  • For example: UsersControllers

Models: singular

  • A model representes one item, i.e. a record from a database. So: User or UserModel.
@martinbean
martinbean / composer.json
Created November 23, 2012 22:38
Code samples for “Uploading files with Amazon’s new PHP SDK” blog post.
{
"require": {
"aws/aws-php-sdk": "dev-master"
}
}
@martinbean
martinbean / xml.php
Created November 16, 2012 21:07
Building an XML file with DOMDocument
<?php
$products = array(
array(
'title' => 'Product #1',
'description' => 'Lorem ipsum'
),
array(
'title' => 'Product #2',
'description' => 'Lorem ipsum'
<?php
$file_to_upload = array(
'ticket_attachment[attachment]' => '@' . $_SERVER['DOCUMENT_ROOT'] . '/nmsworkbacklog/crown.jpg',
'ticket_attachment[description]' => 'test description'
);
$headers = array();
$headers[] = 'Authorization: Basic ' . base64_encode('nms/martinbean:' . $this->registry->get('authenticate')->getApiKey());
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api3.codebasehq.com/api-test-project/tickets/146/attachments');
curl_setopt($ch, CURLOPT_POST, true);
@martinbean
martinbean / gist:1037465
Created June 21, 2011 08:35
CSS pseudo selected styles
body.home #navigation li.home a,
body.events #navigation li.events a,
body.news #navigation li.news a,
body.wrestlers #navigation li.wrestlers a,
body.shop #navigation li.shop a {
background: #ff9900;
}