<?xml version="1.0" encoding="UTF-8"?>
<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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>amnuts &#187; Zend Framework</title>
	<atom:link href="http://blog.amnuts.com/tag/zend-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.amnuts.com</link>
	<description>php projects, javascript, and... stuff.</description>
	<lastBuildDate>Fri, 07 May 2010 09:11:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Shorten urls automatically with a Zend Framework filter</title>
		<link>http://blog.amnuts.com/2009/02/15/shorten-urls-automatically-with-a-zend-framework-filter/</link>
		<comments>http://blog.amnuts.com/2009/02/15/shorten-urls-automatically-with-a-zend-framework-filter/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 01:24:10 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[filter]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=123</guid>
		<description><![CDATA[I think we can all agree that URL shortening services are great and are very handy to tidy up those long and obnoxious links.  However, a lot of the time people simply forget to use them, or often don&#8217;t know about them in the first place.  I&#8217;ve noticed this in a blog system [...]]]></description>
			<content:encoded><![CDATA[<p>I think we can all agree that URL shortening services are great and are very handy to tidy up those long and obnoxious links.  However, a lot of the time people simply forget to use them, or often don&#8217;t know about them in the first place.  I&#8217;ve noticed this in a blog system I wrote using Zend Framework.  On one hand I love that people post messages, but on the other it annoys me that they may supply a link that is so long it breaks the formatting of the page, or looks just plain ugly.</p>
<p>So what are my options?  I could train everyone who posts blogs on the system to use a url shortening service or I could manually tweak all the links myself.  As solutions they are not very practical at all; I don&#8217;t have the time to change any/all links myself, and I certainly don&#8217;t have enough patience to train everyone!  So an automatic way of doing things is needed, and the filtering in Zend Framework comes to the rescue!</p>
<p><span id="more-123"></span><br />
Zend Framework has a lot of filters that you can use right out of the box.  They do all sorts such as string tags, newlines, camel case strings, and so on.  The framework also provides an interface so you can easily create your own filters.  For example, <a href="/2008/06/05/stringtotitle-filter/">converting a string to title case</a>.  With that in mind you could see how easy it&#8217;d be to create a filter to scan for urls and shorten them.  And that&#8217;s exactly what this filter does!  It will scan the supplied text for any http or https urls, and then, if they are past a certain size, it will attempt to connect to a url shortening service and then replace the long urls with the short.</p>
<pre lang="php">
/**
 * Shorten urls found in text.
 *
 * This filter will scan the supplied text for any http or https urls, and
 * then, if they are past a certain size, it will attempt to connect to a
 * url shortening service and then replace the long urls with the short.
 *
 * Two services endpoints are in the class - tinyurl and is.gd - but there's
 * nothing to stop you supplying a different end point, so long as the format
 * of the end point has one string directive (%s) and the service simply
 * returns a short url.
 *
 * @copyright Copyright (c) 2009, Andrew Collington
<php @amnuts.com>
 * @license New BSD License
 */
class Amnuts_Filter_ShortenUrls implements Zend_Filter_Interface
{
    const REGEX = "~(?:https?://(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?)\.)*(?:[a-zA-Z](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?))|(?:(?:\d+)(?:\.(?:\d+)){3}))(?::(?:\d+))?)(?:/(?:(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[;:@&#038;=])*)(?:/(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[;:@&#038;=])*))*)(?:\?(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[;:@&#038;=])*))?)?)~";
    const SERVICE_ISGD    = 'http://is.gd/api.php?longurl=%s';
    const SERVICE_TINYURL = 'http://tinyurl.com/api-create.php?url=%s';

    /**
     * Options array containing:
     *
     *    - maxsize
     *      Maximum size the url can be before attempting to shorten it
     *    - service
     *      The url to the service API that returns a short url
     *    - timeout
     *      The maximum time allowed for a connection
     *
     * @var array
     */
    public $options = array(
        'maxsize' => 35,
        'service' => self::SERVICE_ISGD,
        'timeout' => 5
    );

    /**
     * Set options on construct
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
    }

    /**
     * Filter the text.
     *
     * The filtering may take some time, depending on how many links are in
     * the text to be shortened, how responsive the service is, etc.
     *
     * @param string $text
     * @return string
     * @see Zend_Http_Client, Zend_Http_Response
     */
    public function filter($text)
    {
        $matches = $replacements = array();
        $matched = preg_match_all(self::REGEX, $text, $matches, PREG_PATTERN_ORDER);
        if ($matched) {
            $http = new Zend_Http_Client();
            foreach ($matches[0] as $url) {
                if (strlen($url) > $this->options['maxsize']) {
                    $http->setConfig(array(
                        'timeout' => $this->options['timeout']
                    ));
                    $http->setUri(
                        sprintf($this->options['service'], urlencode($url))
                    );
                    $response = $http->request(Zend_Http_Client::GET);
                    if ($response->isSuccessful()) {
                        $replacements[$url] = $response->getBody();
                    }
                    $http->resetParameters();
                }
            }
        }
        if (empty($replacements)) {
            return $text;
        }
        return str_replace(
            array_keys($replacements),
            array_values($replacements),
            $text
        );
    }
}
</php></pre>
<p>It&#8217;s very easy to use, too:</p>
<pre lang="php">
$shorten = new Amnuts_Filter_ShortenUrls();
$updated = $shorten->filter($text);
</pre>
<p>Here&#8217;s an example of using tinyURL as the service and only converting urls in the text that are longer than 50 characters:</p>
<pre lang="php">
$shorten = new Amnuts_Filter_ShortenUrls(array(
    'maxsize' => 50,
    'service' => Amnuts_Filter_ShortenUrls::SERVICE_TINYURL
));
$updated = $shorten->filter($text);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2009/02/15/shorten-urls-automatically-with-a-zend-framework-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick and easy email encoding view helper</title>
		<link>http://blog.amnuts.com/2008/09/23/quick-and-easy-email-encoding-view-helper/</link>
		<comments>http://blog.amnuts.com/2008/09/23/quick-and-easy-email-encoding-view-helper/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 11:17:27 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[view helper]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=73</guid>
		<description><![CDATA[Here&#8217;s a quick and easy view helper for Zend Framework that will encode an email address.  It will encode just an email address or return a whole mailto link.  The encoding is basically the same as in the Smarty template engine.
Obviously there&#8217;s a lot of room for improvement; javascript encoding, representation as an [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick and easy view helper for Zend Framework that will encode an email address.  It will encode just an email address or return a whole mailto link.  The encoding is basically the same as in the Smarty template engine.</p>
<p>Obviously there&#8217;s a lot of room for improvement; javascript encoding, representation as an image, and so on&#8230; but then it wouldn&#8217;t be quick an easy &#8211; it&#8217;d be slightly longer and just a little more complex. <img src='http://blog.amnuts.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p><span id="more-73"></span></p>
<pre class="brush: php;">
class Amnuts_View_Helper_EncodeEmail extends Zend_View_Helper_Abstract
{
    /**
     * Return the class on direct calls.
     *
     * @return Amnuts_View_Helper_EncodeEmail
     */
    public function encodeEmail()
    {
        return $this;
    }

    /**
     * Return an encoded email address to help against spam.
     *
     * @param string $address The email address to encode
     * @return string
     */
    public function address($address)
    {
        $xhtml = '';
        $address = (string)$address;
        $len = strlen($address);
        for ($x = 0; $x &lt; $len; $x++) {
            if (preg_match('!\w!', $address[$x])) {
                $xhtml .= '%' . bin2hex($address[$x]);
            } else {
                $xhtml .= $address[$x];
            }
        }
        return $xhtml;
    }

    /**
     * Return an encoded mailto link to help against spam.
     *
     * If no link text is supplied then the email address will be used
     * (as is generally preferred).
     *
     * @param string $address The email address to encode
     * @param null|string $text Text to use for link
     * @param array $attrs Any extra attributes for link
     * @return string
     */
    public function link($address, $text = null, array $attrs = array())
    {
        $encodedtext    = '';
        $encodedaddress = $this-&gt;address($address);
        if (null === $text) {
            $text = (string)$address;
        } else {
            $text = (string)$text;
        }
        $len = strlen($text);
        for ($x = 0; $x &lt; $len; $x++) {
            $encodedtext .= '&amp;#x' . bin2hex($text[$x]).';';
        }
        $xhtml = '&lt;a href=&quot;&amp;#109;&amp;#97;&amp;#105;&amp;#108;&amp;#116;&amp;#111;&amp;#58;' . $encodedaddress . '&quot;';
        if (!empty($attrs)) {
            foreach ($attrs as $k =&gt; $v) {
                $xhtml .= ' ' . $this-&gt;view-&gt;escape($k) . '=&quot;' . $this-&gt;view-&gt;escape($v) . '&quot;';
            }
        }
        $xhtml .= '&gt;' . $encodedtext . '';
        return $xhtml;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/09/23/quick-and-easy-email-encoding-view-helper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework 1.6RC1</title>
		<link>http://blog.amnuts.com/2008/08/01/zend-framework-16rc1/</link>
		<comments>http://blog.amnuts.com/2008/08/01/zend-framework-16rc1/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 09:56:31 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[paginator]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[webinar]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=71</guid>
		<description><![CDATA[If you haven&#8217;t heard already, Zend Framework 1.6RC1 is out and has lots of interesting new features.  Finally there&#8217;s a SOAP component (seemed odd to me to have an enterprise-level framework without it!)  There&#8217;s also a paginator, XML configs can have attributes, Dojo integration and lots more.
It&#8217;s been out for about 10 days [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t heard already, <a href="http://devzone.zend.com/article/3712-Zend-Framework-1.6-Release-Candidate-1-now-available">Zend Framework 1.6RC1 is out</a> and has lots of interesting new features.  Finally there&#8217;s a SOAP component (seemed odd to me to have an enterprise-level framework without it!)  There&#8217;s also a paginator, XML configs can have attributes, Dojo integration and lots more.</p>
<p>It&#8217;s been out for about 10 days as of the time I write this, but the only new thing I&#8217;ve tried as of yet is the Zend_Paginator component.  And I must say that I am very happy with how easy it was to set up and integrate in to a site&#8230;  Essentially, I just had to pass my select object to the paginator and write a view partial to handle how it looked &#8211; it was that easy!  With the output of the pagination put in to a partial it makes the whole thing very easy to rebrand and configure to exactly how you want it to look.  This is definitely a component I&#8217;m going to be using a lot.</p>
<p>Looking forward to using the other new features, too.  There is a <a href="http://www.zend.com/en/company/news/event/webinar-what-s-new-in-zend-framework">Zend Webinar to show the new features of ZF1.6</a> coming up on the 13th August, 2008.  Also one in September to go over <a href="http://www.zend.com/en/company/news/event/webinar-zend-framework-and-dojo-integration">integrating the new Dojo features</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/08/01/zend-framework-16rc1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Application running really slow</title>
		<link>http://blog.amnuts.com/2008/07/15/application-running-really-slow/</link>
		<comments>http://blog.amnuts.com/2008/07/15/application-running-really-slow/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 10:50:22 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[slow]]></category>
		<category><![CDATA[Tiger]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=70</guid>
		<description><![CDATA[While working on an application built on Zend Framework, I experienced a really odd slow-down of the system while running on the web cluster at work as opposed to my machine at home.  I couldn&#8217;t see what the issue was myself, and it seemed to baffle people on #zftalk a bit as well as [...]]]></description>
			<content:encoded><![CDATA[<p>While working on an application built on Zend Framework, I experienced a really odd slow-down of the system while running on the web cluster at work as opposed to my machine at home.  I couldn&#8217;t see what the issue was myself, and it seemed to baffle people on #zftalk a bit as well as work colleagues.  The speed difference was quite dramatic &#8211; going from near instant on my home computer to around 30 seconds for a page display while running on the cluster.</p>
<p>Naturally, this required a fair amount of investigation&#8230;</p>
<p>It was quickly ruled out to be any fault of ZF.  After all, it is being used by companies such as IBM, Zend, Sourceforge, Fox, and more. If the framework were not suitable and produced slow results then they would obviously not use it, nor would any of you!</p>
<p>Next to be ruled out was custom code built on top of ZF.  With the exact same code-base producing faster results on one machine and not on another it was highly unlikely to be the code.</p>
<p>Profiling the code proved a little helpful.  I profiled the database connection for each query and ruled out any slowness with that as they were taking fractions of seconds.  Code profiling was a little bit more tricky, as everything seemed proportionally slower, not any one thing in particular.  However, the Zend_Loader component seemed to be taking quite some time to perform its tasks.</p>
<p>With a little command-line magic (using ktrace, kdump, grep, awk, etc. &#8211; not by me, but by talented colleague) it was determined that the OS itself, Mac OSX &#8216;Tiger&#8217;, was mainly to blame.  The cause of the problem was trying to determine relative paths and the slow speed at which Tiger was doing this&#8230;  As I understand it, to determine the current directory, &#8216;.&#8217;, the OS needs to back track all the way to the root, get the whole list of directories and work out which inode matches the one your current path is, and then work its way back down the directories until it finds a match. Once it&#8217;s done that you have your current path. If it sounds intensive, that&#8217;s because it is.</p>
<p>When comparing Tiger to Leopard we were seeing a 1000x improvement (4 microseconds as opposed to 4 milliseconds) to do various getdirentries() calls.</p>
<p>If you used the include path for a handful of files you&#8217;d never notice a significant drop in speed, but the application I&#8217;m working on, together with ZF will typically include 140+ files.</p>
<p>So how was the issue resolved?</p>
<p>For the short term there was a very simple fix; simply alter the include path so that the current path is last to be checked and the more significant paths (such as where the application or Zend library is located) are first. This simple tweak took a 30+ second load time to around two seconds &#8211; a vast improvement! Still, two seconds is not ideal so we will be having Leopard-based machine installed on the web cluster to see if that also helps to increase performance.</p>
<p>I&#8217;m curious; has anyone else had a similar problem?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/07/15/application-running-really-slow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tag cloud view helper</title>
		<link>http://blog.amnuts.com/2008/06/11/tag-cloud-view-helper/</link>
		<comments>http://blog.amnuts.com/2008/06/11/tag-cloud-view-helper/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 13:43:16 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[tag cloud]]></category>
		<category><![CDATA[view helper]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=66</guid>
		<description><![CDATA[Here&#8217;s a little view helper to display a tag cloud.  All you have to do is supply an array of tags, with the tag name being the index and how many times it&#8217;s used as the value, and the url you&#8217;d like the tags to go to.


&#60; ?php

/**
 * Display a tag cloud
 *
 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.amnuts.com/wp-content/uploads/2008/06/tagcloud.gif"><img class="alignright size-full wp-image-67" title="Tag cloud example" src="http://blog.amnuts.com/wp-content/uploads/2008/06/tagcloud.gif" alt="" width="222" height="180" /></a>Here&#8217;s a little view helper to display a tag cloud.  All you have to do is supply an array of tags, with the tag name being the index and how many times it&#8217;s used as the value, and the url you&#8217;d like the tags to go to.</p>
<p><span id="more-66"></span></p>
<div style="clear:both;"></div>
<pre class="brush: php;">&lt; ?php

/**
 * Display a tag cloud
 *
 * @category   Amnuts
 * @package    Amnuts_View
 * @subpackage Amnuts_View_Helper
 * @copyright  Copyright (c) 2008 Andrew Collington (http://www.amnuts.com/)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Amnuts_View_Helper_TagCloud
{
    public $view;

    /**
     * Output the tag cloud.
     *
     * The $tags parameter is expected to be an array with the tag being the
     * index and the number of times it's used as the value.  For example:
     *
     *     array(
     *         'foo' =&gt; 3,
     *         'bar' =&gt; 1,
     *         'dog' =&gt; 5,
     *         'cat' =&gt; 1
     *     )
     *
     * The url will have the tag text appended to it, so that if you supply
     * '/filter/by/tag/' as the url, then given the above array you will have:
     *
     *     &lt;a href=&quot;/filter/by/tag/foo&quot;&gt;foo&lt;/a&gt;
     *     &lt;a href=&quot;/filter/by/tag/bar&quot;&gt;bar&lt;/a&gt;
     *
     * and so on.
     *
     * @param array $tags The tag array
     * @param string $url The link for each tag (with the tag name appended)
     * @param int|string $minFont The minimum font value
     * @param int|string $maxFont The maximum font value
     * @param string $unit The unit of size type (%, em, px, etc.)
     * @return string
     */
    public function tagCloud(array $tags, $url, $minFont = 100, $maxFont = 150, $unit = '%')
    {
        $xhtml = '';
        $cloud = array();

        if (!empty($tags)) {
            $min  = min(array_values($tags));
            $max  = max(array_values($tags));
            $diff = $max - $min;
            if (!$diff) {
                ++$diff;
            }

            foreach ($tags as $tag =&gt; $count) {
                $size = $minFont + ($count - $min) * ($maxFont - $minFont) / $diff;
                $cloud[] = '&lt;a href=&quot;' . $url . urlencode($tag)
                         . '&quot; style=&quot;font-size:' . $size . $unit . ';&quot;&gt;'
                         . $this-&gt;view-&gt;escape($tag) . '&lt;/a&gt;';
            }
            $xhtml .= '&lt;div class=&quot;tagCloudContainer&quot;&gt;&lt;h4&gt;Tag cloud&lt;/h4&gt;' . join(', ', $cloud) . &quot;&lt;/div&gt;\n&quot;;
        }
        return $xhtml;
    }

    /**
     * Set the view object
     *
     * @param Zend_View_Interface $view
     */
    public function setView(Zend_View_Interface $view)
    {
        $this-&gt;view = $view;
    }
}

?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/06/11/tag-cloud-view-helper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>StringToTitle filter</title>
		<link>http://blog.amnuts.com/2008/06/05/stringtotitle-filter/</link>
		<comments>http://blog.amnuts.com/2008/06/05/stringtotitle-filter/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 12:38:14 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[filter]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/?p=65</guid>
		<description><![CDATA[I like the filtering capabilities of the Zend Framework, but for some reason there doesn&#8217;t seem to be a string to title case filter (though there is a string to upper and string to lower).  So here it is:

&#60; ?php

/**
 * @see Zend_Filter_Interface
 */
require_once 'Zend/Filter/Interface.php';

/**
 * Convert a string to titlecase.
 *
 * Seemingly [...]]]></description>
			<content:encoded><![CDATA[<p>I like the filtering capabilities of the Zend Framework, but for some reason there doesn&#8217;t seem to be a string to title case filter (though there is a string to upper and string to lower).  So here it is:</p>
<p><span id="more-65"></span></p>
<pre class="brush: php;">&lt; ?php

/**
 * @see Zend_Filter_Interface
 */
require_once 'Zend/Filter/Interface.php';

/**
 * Convert a string to titlecase.
 *
 * Seemingly missing from the core Zend distribution.
 */
class StringToTitle implements Zend_Filter_Interface
{
    /**
     * Encoding for the input string
     *
     * @var string
     */
    protected $_encoding = null;

    /**
     * Set the input encoding for the given string
     *
     * @param  string $encoding
     * @throws Zend_Filter_Exception
     */
    public function setEncoding($encoding = null)
    {
        if (!function_exists('mb_convert_case')) {
            require_once 'Zend/Filter/Exception.php';
            throw new Zend_Filter_Exception('mbstring is required for this feature');
        }
        $this-&gt;_encoding = $encoding;
    }

    /**
     * Defined by Zend_Filter_Interface
     *
     * Returns the string $value, converting characters to titlecase as necessary
     *
     * @param  string $value
     * @return string
     */
    public function filter($value)
    {
        if ($this-&gt;_encoding) {
            return mb_convert_case((string) $value, MB_CASE_TITLE, $this-&gt;_encoding);
        }
        return ucwords((string) $value);
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/06/05/stringtotitle-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto generating basic models for a Zend Framework app</title>
		<link>http://blog.amnuts.com/2008/04/11/auto-generating-basic-models-for-a-zend-framework-app/</link>
		<comments>http://blog.amnuts.com/2008/04/11/auto-generating-basic-models-for-a-zend-framework-app/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 13:18:39 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[models]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/2008/04/11/auto-generating-basic-models-for-a-zend-framework-app/</guid>
		<description><![CDATA[Do you have a database with foreign keys and just wish you could have something automatically create your ZF models from it? Well, today that was me. So as a little proof of concept, this is the code I came up with to do it for me&#8230;
But before we get to that, a few caveats:

It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Do you have a database with foreign keys and just wish you could have something automatically create your ZF models from it? Well, today that was me. So as a little proof of concept, this is the code I came up with to do it for me&#8230;</p>
<p>But before we get to that, a few caveats:</p>
<ul>
<li>It&#8217;s just a proof of concept</li>
<li>The output needs updating for proper reference names, etc.</li>
<li>Outputs everything to screen in one go and doesn&#8217;t save the files.</li>
</ul>
<p>However, it might be handy to someone, so I post it up for your comments.<br />
<span id="more-63"></span></p>
<pre class="brush: php;">&lt; ?php

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

$config = array(
	'host'	 =&gt; 'localhost',
	'username' =&gt; 'myusername',
	'password' =&gt; 'mypassword',
	'dbname'   =&gt; 'mydbname'
);
try {
	$db = Zend_Db::factory('pdo_mysql', $config);
} catch (Zend_Db_Exception $e) {
	echo $e-&gt;getMessage();
	die;
}

$model = &lt; &lt;&lt;EOT
&lt;?php

class %s extends Zend_Db_Table_Abstract
{
	protected \$_name = '%s';
%s
}
EOT;

$refmap_outer = &lt;&lt;&lt;EOT
	protected \$_referenceMap	= array(
%s
	);
EOT;

$refmap_inner = &lt;&lt;&lt;EOT
		'%s' =&gt; array(
			'columns'		   =&gt; array('%s'),
			'refTableClass'	 =&gt; '%s',
			'refColumns'		=&gt; array('%s')
		)
EOT;

$toTable = new Zend_Filter_Inflector(
	':tbl',
	array(':tbl' =&gt; array('Word_CamelCaseToUnderscore', 'StringToLower'))
);

$toClass = new Zend_Filter_Inflector(
	':tbl',
	array(':tbl' =&gt; array('StringToLower', 'Word_UnderscoreToCamelCase'))
);

foreach ($db-&gt;listTables() as $table) {
	$sql = &quot;
		select
			tc.constraint_name,
			kcu.table_name,
			kcu.column_name,
			kcu.referenced_table_name,
			kcu.referenced_column_name
		from
			information_schema.table_constraints tc,
			information_schema.key_column_usage kcu
		where
			tc.table_name = &quot; . $db-&gt;quote($table) . &quot;
			and tc.constraint_type = 'FOREIGN KEY'
			and kcu.constraint_name = tc.constraint_name
		&quot;;
	$keys = $db-&gt;fetchAll($sql);

	$refs = array();
	if (!empty($keys)) {
		$r = 0;
		foreach ($keys as $key) {
			$refs[] = sprintf($refmap_inner,
					'ref' . ++$r,
					$key['column_name'],
					$toClass-&gt;filter(array('tbl' =&gt; $key['referenced_table_name'])),
					$key['referenced_column_name']
				);
		}
	}
	printf($model,
		$toClass-&gt;filter(array('tbl' =&gt; $table)),
		$toTable-&gt;filter(array('tbl' =&gt; $table)),
		(!empty($refs) ? sprintf($refmap_outer, join(&quot;,\n&quot;, $refs)) : '')
	);
}
</pre>
<p>So assuming I had a database structure like:</p>
<pre class="brush: sql;">--
-- Table structure for table `users`
--
CREATE TABLE `users` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `added` datetime NOT NULL,
  `username` varchar(32) NOT NULL,
  `password` varchar(64) NOT NULL,
  `name_first` varchar(32) NOT NULL,
  `name_last` varchar(32) NOT NULL,
  `name_nick` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `user_tags`
--
CREATE TABLE `user_tags` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` int(10) UNSIGNED NOT NULL,
  `tag_user_id` int(10) UNSIGNED NOT NULL,
  `tag` varchar(24) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `tag` (`tag`),
  KEY `fk_user_tags_user_id` (`user_id`),
  KEY `fk_user_tags_tag_user_id` (`tag_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Constraints for table `user_tags`
--
ALTER TABLE `user_tags`
  ADD CONSTRAINT `fk_user_tags_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `fk_user_tags_tag_user_id` FOREIGN KEY (`tag_user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;</pre>
<p>it would output something like:</p>
<pre class="brush: php;">&lt; ?php

class UserTags extends Zend_Db_Table_Abstract
{
	protected $_name = 'user_tags';
	protected $_referenceMap	= array(
		'ref1' =&gt; array(
			'columns'		   =&gt; array('user_id'),
			'refTableClass'	 =&gt; 'Users',
			'refColumns'		=&gt; array('id')
		),
		'ref2' =&gt; array(
			'columns'		   =&gt; array('tag_user_id'),
			'refTableClass'	 =&gt; 'Users',
			'refColumns'		=&gt; array('id')
		)
	);
}

&lt;?php

class Users extends Zend_Db_Table_Abstract
{
	protected $_name = 'users';
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/04/11/auto-generating-basic-models-for-a-zend-framework-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework 1.5 preview release</title>
		<link>http://blog.amnuts.com/2008/01/29/zend-framework-15-preview-release/</link>
		<comments>http://blog.amnuts.com/2008/01/29/zend-framework-15-preview-release/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 09:55:46 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/2008/01/29/zend-framework-15-preview-release/</guid>
		<description><![CDATA[For those that don&#8217;t know by now, version 1.5 of the Zend Framework is now out in preview release.  Congratulations to everyone who has had apart in getting out this release &#8211; from programmers to documentation writers to project managers!
There are a lot of very interesting updates and new features.  Some notable ones [...]]]></description>
			<content:encoded><![CDATA[<p>For those that don&#8217;t know by now, <a href="http://devzone.zend.com/article/3020-Zend-Framework-1.5.0-Preview-Release-now-available">version 1.5 of the Zend Framework is now out in preview release</a>.  Congratulations to everyone who has had apart in getting out this release &#8211; from programmers to documentation writers to project managers!</p>
<p>There are a lot of very interesting updates and new features.  Some notable ones are the inclusion of Zend_Form, Zend_Layout, OpenID and LDAP adapters for authentication, Technorati web service, as well has handy tweaks Zend_Db_Table such as being able to directly access the select object.</p>
<p>As it&#8217;s a preview release the code isn&#8217;t intended for production systems just yet, though I hope the time frame for getting it to stable release is short enough so that I can use it soon, but long enough to work out any major kinks. <img src='http://blog.amnuts.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2008/01/29/zend-framework-15-preview-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple image view helper for Zend Framework</title>
		<link>http://blog.amnuts.com/2007/10/03/simple-image-view-helper-for-zend-framework/</link>
		<comments>http://blog.amnuts.com/2007/10/03/simple-image-view-helper-for-zend-framework/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 11:35:51 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[Code snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[view helper]]></category>

		<guid isPermaLink="false">http://blog.amnuts.com/2007/10/03/simple-image-view-helper-for-zend-framework/</guid>
		<description><![CDATA[Here&#8217;s a simply view helper for the Zend Framework that can be used to display image tags.  It checks to see if the image file exists and if not then it&#8217;ll use the data url scheme to output a very simple image that, ironically, says &#8216;NO IMG&#8217; on it.    Please note, [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simply view helper for the Zend Framework that can be used to display image tags.  It checks to see if the image file exists and if not then it&#8217;ll use the <a href="http://tools.ietf.org/html/rfc2397">data url scheme</a> to output a very simple image that, ironically, says &#8216;NO IMG&#8217; on it. <img src='http://blog.amnuts.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   Please note, though, that I&#8217;ve only seen Firefox support this scheme, as wonderful as it is!</p>
<p><span id="more-56"></span></p>
<pre class="brush: php;">&lt; ?php

class Zend_View_Helper_Img
{
    public $view;
    protected static $_baseurl = null;
    protected static $_cache = null;

    /**
     * Constructor
     */
    public function __construct()
    {
        if (null === self::$_baseurl) {
            $url = Zend_Controller_Front::getInstance()-&gt;getRequest()-&gt;getBaseUrl();
            $root = '/' . trim($url, '/');
            if ('/' == $root) {
                $root = '';
            }
            self::$_baseurl = $root . '/';
        }
    }

    /**
     * Output the &lt;img /&gt; tag
     *
     * @param string $path
     * @param array $params
     * @return string
     */
    public function img($path, $params = array())
    {
        $plist = array();
        $paramstr = null;
        $imagepath = self::$_baseurl . ltrim($path, '/');
        if (!isset(self::$_cache[$path])) {
            self::$_cache[$path] = file_exists(realpath($_SERVER['DOCUMENT_ROOT'] . '/' . $imagepath));
        }
        if (!isset($params['alt'])) {
            $params['alt'] = '';
        }
        foreach ($params as $param =&gt; $value) {
            $plist[] = $param . '=&quot;' . $this-&gt;view-&gt;escape($value) . '&quot;';
        }
        $paramstr = ' ' . join(' ', $plist);
        return '&lt;img src=&quot;' .
                ((self::$_cache[$path])
                    ? self::$_baseurl . ltrim($path, '/')
                    : 'data:image/gif;base64,R0lGODlhFAAUAIAAAAAAAP///yH5BAAAAAAALAAAAAAUABQAAAI5jI+pywv4DJiMyovTi1srHnTQd1BRSaKh6rHT2cTyHJqnVcPcDWZgJ0oBV7sb5jc6KldHUytHi0oLADs=') .
                '&quot;' . $paramstr . ' /&gt;';
    }

    /**
     * Set the view object
     *
     * @param Zend_View_Interface $view
     * @return void
     */
    public function setView(Zend_View_Interface $view)
    {
        $this-&gt;view = $view;
    }
}</pre>
<p>Usage is really easy:</p>
<pre class="brush: php;">&lt; ?php echo $this-&gt;img('/images/logo.png', array('id' =&gt; 'logo', 'title' =&gt; 'Cool Company')); ?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.amnuts.com/2007/10/03/simple-image-view-helper-for-zend-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
