<?xml version="1.0" encoding="utf-8"?><!-- generator="Kirby" -->

<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">

  <channel>
    <title>Durham Hale | Developer</title>
    <link>http://durhamhale.com/</link>
    <generator>Kirby</generator>
    <lastBuildDate>Tue, 31 Dec 2019 00:40:37 +0000</lastBuildDate>
    <atom:link href="http://durhamhale.com/blog/feed" rel="self" type="application/rss+xml" />

        <description>The latest from Durham's blog</description>
      
        <item>
      <title>Building the Laravel Campaign Monitor bundle</title>  
      <link>http://durhamhale.com/blog/building-the-laravel-campaign-monitor-bundle</link>
      <guid>http://durhamhale.com/blog/building-the-laravel-campaign-monitor-bundle</guid>
      <pubDate>Tue, 05 Jun 2012 23:52:04 +0000</pubDate>
        
                  <description><![CDATA[<p>One of the projects I'd been working on called for Campaign Monitor integration. I'd used the API in various other frameworks, normally only implementing the API requests I required as I go along as some frameworks aren't particularly good at being extended with 3rd party libraries. <em>(Not mentioning any names - <a href="http://heybigname.com/2012/05/06/why-codeigniter-is-dead/">CodeIgniter</a>!)</em> </p>

<p>Luckily for me I discovered <a href="http://www.laravel.com">Laravel</a> which is designed to be extendable. It also has an ever growing collection of bundles which allow users to easily share and install extensions from other users. As <a href="http://www.campaignmonitor.com">Campaign Monitor</a> is a large international company I guessed other Laravel developers may want to use their API, so I spent an extra bit of time creating a neatly formed, easy to update bundle.</p>

<h2>Getting to grips with the Campaign Monitor API PHP wrapper</h2>

<p>Campaign Monitor very kindly provide <a href="http://www.campaignmonitor.com/api/samples-and-wrappers/">various wrappers for their API</a> but I was only interested in the <a href="https://github.com/campaignmonitor/createsend-php">PHP one</a>. Normally I would consider starting from scratch and building a bundle/module from the ground up but as the wrapper from Campaign Monitor covers all the API requests possible and appears to be written in a decent enough fashion I decided to keep it in it's original state as it makes it easier to update if/when Campaign Monitor update it. Another reason for this is that I presume that it's well tested and the less work I do on it, the less chances I have of breaking it!</p>

<p>If you've looked at the PHP wrapper you'll see that Campaign Monitor have broken it down into 7 classes that deal with different sections of their system. The classes for <em>campaigns</em>, <em>clients</em>, <em>lists</em>, <em>segments</em>, <em>subscribers</em> and <em>templates</em> are all quite self-explanatory. The <em>general</em> just deals with everything else that doesn't fit into the previous 6 classes. The wrapper also contains a directory called <em>class</em> which is home to 'helper' utility classes that are used.</p>

<h2>Setting the structure of the bundle</h2>

<p>First things first - first things last would be stupid! lol! I created a directory in my application's bundle directory and gave it the incredibly imaginative name of <code>campaignmonitor</code>. Laravel is pretty flexible about where you stick any externally sourced libraries so I plonked them in a directory called <code>vendor</code> - I'm sure this is a habit I picked up from using <a href="http://kohanaframework.org">Kohana PHP</a> or <a href="http://www.codeigniter.com">CodeIgniter</a> a few versions ago but I really can't remember! The only things left to do then was create an empty CampaignMonitor class in <code>campaignmonitor.php</code> and add a mapping to it in the start.php file so Laravel knows where to look for it. Both of these are in the root of the <code>campaignmonitor</code> bundle directory.</p>

<h2>Creating the wrapper class</h2>

<p>The first obverse thing to do was to split they Campaign Monitor API key into a separate file as each install would need to use a different key. I created a directory called <code>config</code> with a sample config file called <code>campaignmonitor-sample.php</code> in it for the API key to go in. (The idea is that you copy the template file <code>campaignmonitor-sample.php</code> to your applications config directory and rename it <code>campaignmonitor.php</code>.)</p>

<p>I wanted to be able to access all of the Campaign Monitor methods, across all the 7 classes, via a single point of entry that would be simple and flexible enough so if/when Campaign Monitor ever extended or altered their API classes it wouldn't require me to update much of my code - it would just be a case of replacing their files.</p>

<h3>Revision #1</h3>

<p>My first concept was to load a Campaign Monitor class like this - where <code>CLASS_NAME</code> would match one of the classes such as <em>clients</em>, <em>lists</em> or <em>general</em>:</p>

<pre class="php"><code>CampaignMonitor::factory('CLASS_NAME');</code></pre>

<p>This would use a single method, <code>factory()</code>, in my CampaignMonitor class to load the API key, include the correct Campaign Monitor file and return the initiated classes so that all the methods from with the classes could then be accesses like so:</p>

<pre class="php"><code>// Example 1
$general = CampaignMonitor::factory('general');
$general-&gt;get_timezones();

// Example 2
CampaignMonitor::factory('clients', $client_id)-&gt;get_lists();</code></pre>

<p>The issue I had with the way of doing it was mainly down to the second example above, as to access some libraries you needs to provide the id/key for the item - such as a client id for a client or a list id for a list. </p>

<p>This method of retrieving the data worked perfectly correctly but it just didn't seem to be as elegant as I'd have liked it to be so I had another think!</p>

<h3>Revision #2</h3>

<p>I decided to drop the factory method and make use of the <code>__callStatic</code> magic method instead. It would it use much of the same logic as the <code>factory()</code> method but would allow for clearer, more semantic coding when using the bundle. As you can see from the examples below you now choose the Campaign Monitor class with the method name rather than the first argument. You now only need pass in 1 argument if the Campaign Monitor classes requires a id.</p>

<pre class="php"><code>// Example 3
$general = CampaignMonitor::general();
$general-&gt;get_timezones();

// Example 4
CampaignMonitor::clients($client_id)-&gt;get_lists();</code></pre>

<p>I did start to try and implement a way of using the singulars for the method names, rather than plurals, as these would have been more semantically correct but I ran into the age old problem of <code>list()</code> being a native PHP function, and it was getting rather late and I wanted to go to bed so this was short lived!</p>

<h2>And finally...</h2>

<p>Now the bundle was all complete and committed into <a href="http://github.com/durhamhale">my GitHub account</a>, all that was left to do was login to the <a href="http://bundles.laravel.com">Laravel Bundles</a> site via GitHub, fill in a quick form and submit the bundle. Now it has <a href="http://bundles.laravel.com/bundle/campaignmonitor">its own special little page</a> and can be installed by any Laravel user with one simple command...</p>

<pre class="shell"><code>$ php artisan bundle:install campaignmonitor</code></pre>

<p>The way bundles are handled through Laravel are probably the best and easier way I've seen by a PHP framework. What I found was from the whole process is that, yet again, Laravel is kick-ass and does what I've been longing for a framework to do for ages - easy bundle creation and sharing in a way that just makes sense.</p>

<p><em><strong>EXTRA NOTE:</strong> By the time I actually finished writing this blog post my bundle has been released for over a month and has had 86 installs at the time of publishing - I must of done something right!</em></p>
]]></description>      
            
    </item>
        <item>
      <title>Kirby, a file-based CMS that just works</title>  
      <link>http://durhamhale.com/blog/kirby-a-file-based-cms-that-just-works</link>
      <guid>http://durhamhale.com/blog/kirby-a-file-based-cms-that-just-works</guid>
      <pubDate>Thu, 29 Mar 2012 23:02:52 +0000</pubDate>
        
                  <description><![CDATA[<p>I've been "re-designing" this site for along time now - it's been a single holding page for way to long. I found, like many other people, that if I spend to long working on it I get pretty board of it and have to start it from fresh again and it never seems to get finished. I knew for certain that I needed to get it built and live in a short timespan and I wanted the new site to be a blog with the possibility to extend it in the future, so I started looking at my options...</p>

<h2>The almighty - Wordpress</h2>

<p>I wanted the core of my site to be a blog so where better to start looking then at everybody's friend - <a href="http://wordpress.org">Wordpress</a>. I was actually quite excited about using Wordpress. Even though I've been a web developer for over 5 years I never needed to use it so I was eager to see what all the fuss was about. I should of known better!</p>

<p>I spend my days ensuring all my code is keep neat and tidy. I keep the front-end stuff (HTML, CSS and javascript) as separate as possible from the functional PHP. I try to write re-usable code that is regularly (and sensibly) refactored so that classes and methods that sound like they should do the same thing - <em>do the same thing</em>! Anyway, enough ranting about what Wordpress doesn't do - as you might be able to tell I wasn't really impressed!</p>

<p><strong>NOTE:</strong> I feel like I should mention that Wordpress isn't all bad. It is a well tested, well supported and feature rich blogging platform. It's just to messy and bulky for what I was after.</p>

<h2>The familiar - PHP Frameworks</h2>

<p>I've used various PHP frameworks nearly everyday through-out my working life. They are amazing. I can't speak highly enough about them. With this in mind I thought I may as well quickly knock up a quick blog using one and <a href="http://twitter.github.com/bootstrap">Twitter Bootstrap</a> for the admin skin.</p>

<p>I thought I'd keep it interesting and start using one that I hadn't used at work. <a href="http://fuelphp.com">FuelPHP</a> and <a href="http://laravel.com">Laravel</a> seemed like a couple of good options as they are similar to my work staples of <a href="http://codeigniter.com">CodeIgniter</a> and <a href="http://kohanaframework.org">Kohana</a> and have a good buzz surrounding them.</p>

<p>I started this process with great enthusiasm until I realised that at 11pm every night (which is the only time I get to work on personal stuff) I couldn't really be bothered to try get my head around a new framework and the whole thing was starting to feel abit like work!</p>

<h2>The saviour - Kirby</h2>

<p>I can't actually remember how I can across <a href="http://getkirby.com">Kirby</a>. I presume it was because I was trying to find a markdown-based cms and I just stumbled over it. I flicked through a few of the example site and saw that the vast majority where simple and minimal blog-style sites - just what I was after. I downloaded the free trail from GitHub and had a look through the codebase. Everything looked neat and lovingly looked after. I created a couple a test pages and a test template and everything worked just as it said it would.</p>

<p>I browsed through the documentation and started thinking of how I could construct a blog from the file-based approach it used. I though I'd have a click around the Kirby site to see what sort of stuff was possible. When I clicked on the <a href="http://getkirby.com/tutorials">Tutorials</a> I was expecting, maybe, 3 or 4 <em>very</em> basic getting started guides, as is normally the case. I was pleasantly surprised to find that not only everything I was trying to do was possible, but it was all already accurately documented for me to follow. Perfect for late night coding!</p>

<p>A few tutorials and a couple of well thought out <a href="http://getkirby.com/downloads">snippets and plugins</a> later, I had a fully functioning blog with a nice clean HTML template to start styling. To complete all of the setup and development work for the blog it probably took less than an hour. Now I had plenty of time to style it and get it live before I would normally get fed up.</p>

<p>I raise my hat (if I had one) to the guys who made Kirby - it really does what it says on the tin (well, homepage) "Easy to setup, easy to use, flexible as hell".</p>
]]></description>      
            
    </item>
        <item>
      <title>JavaScript version of PHP's str_replace() function</title>  
      <link>http://durhamhale.com/blog/javascript-version-of-phps-str-replace-function</link>
      <guid>http://durhamhale.com/blog/javascript-version-of-phps-str-replace-function</guid>
      <pubDate>Wed, 07 Mar 2012 22:14:02 +0000</pubDate>
        
                  <description><![CDATA[<p>I spend a lot of my working day in PHP so when I come to coding in javascript I often get frustrated at the difference in functionality between similar sounding function names in the two languages. I use PHP's <code>str_replace()</code> function fairly frequently and often need to use it in JavaScript work. JavaScript has a function, <code>replace()</code>, which is a string replacement tool but it only works on the first instance it finds - this is functionally PHP lacks. I wanted a function that found and replaced all instances of the search term. </p>

<h2>The solution</h2>

<p>My solution works by splitting the string, on the <code>search</code> variable, into an array and then joining the array values back into a string on the <code>replace</code> variable. Essentially it looks like this:</p>

<pre><code>string.split(search).join(replace);
</code></pre>

<p>There are many arguments about extending the DOM prototype, which I am not going to go into here, so I've provided two varieties of the same solution - just use which ever one you agree with!</p>

<h2>Extending the DOM prototype</h2>

<p>This code should be put near the beginning of your javascript files as it needs to be loaded before the function can be called. The first argument is the string to be searched for and the second argument is the string to replace it with.</p>

<pre><code>String.prototype.str_replace = function(search, replace) {

    return this.split(search).join(replace);

}
</code></pre>

<h3>Example usage</h3>

<p>This example will only work if used after the prototyping code above.</p>

<pre><code>var string = 'this-is-a-test-string';

string = string.str_replace('-', '_');

console.log(string); // Should output 'this_is_a_test_string'
</code></pre>

<h2>Normal functions instead of extending</h2>

<p>The method above uses the prototype object to add extra functionality to the String object. If you prefer to just use basic functions then it could be written like the example below. <strong>Please note:</strong> Because the string variable needs to be passed as an argument to the function the <code>search</code> and <code>replace</code> arguments are now second and third - instead of first and second - respectively. </p>

<pre><code>function str_replace(string, search, replace) {

    return string.split(search).join(replace);

}
</code></pre>

<h3>Example usage</h3>

<p>This example should work with the <code>str_replace()</code> function placed anywhere in an accessible namespace.</p>

<pre><code>var string = 'this-is-a-test-string';

string = str_replace(string, '-', '_');

console.log(string); // Should output 'this_is_a_test_string'
</code></pre>

<h2>Case sensitivity</h2>

<p>The function <strong><em>IS</em> case sensitive</strong>, which is fine for the example above which replaces hyphens with underscores, but you may need it to be case insensitive if you're replacing actual human readable text. In order to make is case insensitive you can pass a regular expression to <code>search</code> argument, as shown in the example below.</p>

<pre><code>var string = 'this is a case sensitive string';

string = string.str_replace(/SENSITIVE/i, 'INsensitive');  // Extending
string = str_replace(string, /SENSITIVE/i, 'INsensitive');  // Non-extending

console.log(string); // Should output 'this is a case INsensitive string
</code></pre>
]]></description>      
            
    </item>
            
  </channel>
</rss>