Pragmatism in the real world

Setting OS X's Terminal Tab to the current directory

I use many tabs in a Terminal window quite frequently, and while the window title will show the current directory name, the tab title doesn't. You can manually change it using shift+cmd+i, but who can be bothered? Automating it so that the tab title always matches the current directory turns out to be really easy and just requires a few lines in ~/.profile. Firstly, we need a function that sets the tab's title to the… continue reading.

Exclude elements from Zend\Form's getData()

If you need to exclude some elements from a Zend\Form's getData(), the easiest way is to use a validation group. For example: class SomeForm extends \Zend\Form\Form implements \Zend\InputFilter\InputFilterProviderInterface { public function init() { $this->add([ 'name' => 'name', 'options' => ['label' => 'Name'], ]); $this->add([ 'name' => 'email', 'options' => ['label' => 'Email'], ]); $this->add([ 'name' => 'submit', 'type' => 'button', 'options' => [ 'label' => 'Filter', ], ]); $this->setValidationGroup(['name', 'email']); } public function getInputFilterSpecification() {… continue reading.

2014 in pictures

2014 is coming to an end, so as usual, let's look back at the year as highlighted by the photos that I've taken. This year, I took at least one photo every day, so there's been plenty for me to choose from! I've done this recap in 2008, 2009, 2010, 2011, 2012 and 2013. January As usual, there was flooding in Worcester, but clearly the biggest personal event of the month was breaking my elbow… continue reading.

Recursively deleting elements from an array

I had a need recently to delete items from a nested associative array and also any empty sub-arrays. My initial thought was to use array_walk_recursive, but this doesn't work as you can't unset nested elements and you only have access to the leaves. Clearly I needed a recursive function. I'm sure that this has been done many times before, but this is my solution: /** * Remove any elements where the callback returns true *… continue reading.

SSL certificate verification on PHP 5.6

I recently updated my local OS X Zend Server installation to PHP 5.6 and when I ran composer self-update, I got this error message: [Composer\Downloader\TransportException] The "https://getcomposer.org/version" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed Failed to enable crypto failed to open stream: operation failed Googling around, I finally worked out that there have been various SSL improvements in PHP 5.6 and that the problem… continue reading.

Overriding the built-in Twig date filter

In one project that I'm working on, I'm using Twig and needed to format a date received from an API. The date string received is of the style "YYYYMMDD", however date produced an unexpected output. Consider this: {{ "20141216"|date('jS F Y') }} creates the output: 20th May 1976 This surprised me. Then I thought about it some more and realised that the date filter is treating my date string as a unix timestamp. I investigated… continue reading.

Converting databases between MySQL and SQL Server

I regularly deal projects that target SQL Server, but mostly develop against MySQL to avoid having to run a full Windows stack locally all the time. One of the nice things about PHP with DBAL and Migrations is that the database is pretty well abstracted from my code. Of course, this means that I don't target any of the specialist features, but for these projects, this hasn't been an issue. To convert data from SQL… continue reading.

Validating JSON with ZF2's Zend\Validator

Let's say that you have an admin form where the user can enter JSON and you'd like to validate that the JSON parses before allowing the user to submit. To do this, you can use the rather excellent jsonlint project by Jordi Boggiano. Obviously, add it via Compser :) Usage is simple: use Seld\JsonLint\JsonParser; use Seld\JsonLint\ParsingException; $parser = new JsonParser(); $result = $parser->lint($json); if ($result instanceof ParsingException) { // $json is invalid JSON } We… continue reading.

Autocomplete Phing targets on the command line

Shortly after writing my last post, it crossed my mind that it would be nice if I could autocomplete the targets in my Phing build file on the command line. In order to do this, I read Buddy Lindsey's Quick and Dirty Write Your Own Bash Autocomplete and made it do what I needed! Start by creating a new bash completion file in the bash_completion.d directory. This file needs executable permission. This directory can usually… continue reading.

A few Phing tips

Following on from my last post, here's a few other Phing things that I've found helps me. Hiding targets from Phing -l I have a number of dependent targets in my build.xml that I don't want listed when I run phing -l. The easiest way to do this is to add the hidden="true" property to the targets I want to hide: <?xml version="1.0"?> <project name="buildfile" default="foo"> <target name="foo" depends="bar,baz" /> <target name="bar" hidden="true"> <!– tasks… continue reading.