<?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>Darin Boyd Dot Com &#187; PHP</title>
	<atom:link href="http://www.darinboyd.com/wordpress/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.darinboyd.com/wordpress</link>
	<description>This is what happens when you're interested in everything!</description>
	<lastBuildDate>Fri, 23 Jan 2009 05:41:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a String Range Rule with Monkey Wrench</title>
		<link>http://www.darinboyd.com/wordpress/2009/01/creating-a-string-range-rule-with-monkey-wrench/</link>
		<comments>http://www.darinboyd.com/wordpress/2009/01/creating-a-string-range-rule-with-monkey-wrench/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 09:35:48 +0000</pubDate>
		<dc:creator>Darin Boyd</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Monkeywrench]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.darinboyd.com/wordpress/?p=165</guid>
		<description><![CDATA[In this article, the third in a series of creating a business objects framework for the Wordpress blogging environment, we will discuss the construction of a custom rule class based on the Rule abstract base class from the last article.
The source file for the the entire framework may be downloaded here.  We will be [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, the third in a series of creating a business objects framework for the Wordpress blogging environment, we will discuss the construction of a custom rule class based on the <strong>Rule</strong> abstract base class from the last <a title="Building Monkey Wrench - The Rule Classes" href="http://www.darinboyd.com/wordpress/2009/01/building-monkey-wrench-the-rule-classes/" target="_blank">article</a>.<span id="more-165"></span></p>
<p>The source file for the the entire framework may be downloaded <a href="http://www.darinboyd.com/wordpress/wp-content/uploads/2009/01/monkeywrench.zip">here</a>.  We will be covering <strong>StringRangeRule.php</strong>.</p>
<h2>The StringRangeRule Class</h2>
<p>Using the abstract base class Rule which we created last time, we will build a class to solve a common validation problem.  For the sake of database integrity and business goals, it is common to enforce length restrictions on some of the text properties of an object.  Our string range validator can be configured to do the following.</p>
<ul>
<li>Set min and max length values so that the string length must fall within the range.</li>
<li>Set min to zero and provide a max so that the string may be any length not exceeding the max.</li>
<li>Set max to zero and provide a min so that the string may be any length as long as it is at least min characters in length.</li>
</ul>
<p>Our class will provide a neat package for handling the tasks on the above list.</p>
<p>Let&#8217;s write some code!</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">&nbsp;
We begin by using <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>require_once<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span> to reference the <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Rule<span style="color: #339933;">.</span>php<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span> <span style="color: #990000;">file</span> containing our base <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">.</span>  In my setup<span style="color: #339933;">,</span> the <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Rule<span style="color: #339933;">.</span>php<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span> <span style="color: #990000;">file</span> is in the same directory <span style="color: #b1b100;">as</span>  our <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">.</span>  We inherit from <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Rule<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span> using the <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>extends<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span> <span style="color: #990000;">key</span> word<span style="color: #339933;">.</span>  We declare the <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Min<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span> and <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>Max<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span> member variables<span style="color: #339933;">.</span>  These variables are declared <span style="color: #b1b100;">as</span> protected so that you could create other classes based on our <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">class</span> which would have access to the <span style="color: #990000;">Min</span> and <span style="color: #990000;">Max</span> member variables<span style="color: #339933;">.</span>
<span style="color: #339933;">&lt;</span>pre lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #666666; font-style: italic;">// Public State Methods</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Minimum length allowed for string.
	 *
	 * @return number
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GetMin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Sets minimum allowed length for string.
	 *
	 * @param number $value
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetMin<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Maximum length allowed for string.
	 *
	 * @return number
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GetMax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Sets maximum allowed length for string.
	 *
	 * @param number $value
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetMax<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span>;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The code above contains &#8220;getters and setters&#8221; for our member variables.  Notice that I am documenting these methods per our discussion about coding style and documentation in the first article.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">// Constructors</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Constructor
	 * @param string $_ruleName
	 * @param string $_propertyName
	 * @param string $_min
	 * @param string $_max
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_ruleName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_min</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_max</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_ruleName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_propertyName</span><span style="color: #339933;">,</span> <span style="">''</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">=</span> <span style="color: #000088;">$_min</span>;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">=</span> <span style="color: #000088;">$_max</span>;
&nbsp;
		<span style="color: #666666; font-style: italic;">// check the max and min values</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;CheckValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #666666; font-style: italic;">// set the display text</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;CalculateDisplayText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The constructor above takes in the rule name and property names, as does our base class, and adds two new parameters representing the min and max values for our new class.  We call the constructor on the base class in line 70 using the <strong>parent::_construct()</strong> method and passing along the rule name and property name values.  Values for Max and Min are then assigned based on the appropriate parameter values.  In lines 76 and 79, we call helper functions in our class to check for proper min and max values and to construct a good user description based on the values for min and max.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Overrides</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Validate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_objectToValidate</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Check the values again to make sure the rule is set up properly.</span>
&nbsp;
		<span style="color: #000088;">$_MethodName</span> <span style="color: #339933;">=</span> <span style="">'Get'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_PropertyName;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_PropertyValue <span style="color: #339933;">=</span> <span style="color: #000088;">$_objectToValidate</span><span style="color: #339933;">-&amp;</span>gt;<span style="color: #000088;">$_MethodName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;CheckValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000088;">$_MinOnly</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">==</span> <span style="color:#800080;">0</span>;
		<span style="color: #000088;">$_MaxOnly</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">==</span> <span style="color:#800080;">0</span>;
		<span style="color: #000088;">$_MaxAndMin</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>lt; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max;
		<span style="color: #000088;">$_Len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_PropertyValue<span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_MinOnly</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_IsValid <span style="color: #339933;">=</span> <span style="color: #000088;">$_Len</span> <span style="color: #339933;">&amp;</span>gt;= <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min;
			<span style="color: #b1b100;">return</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_MaxOnly</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_IsValid <span style="color: #339933;">=</span> <span style="color: #000088;">$_Len</span> <span style="color: #339933;">&amp;</span>lt;= <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max;
			<span style="color: #b1b100;">return</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_MaxAndMin</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_IsValid <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_Len</span> <span style="color: #339933;">&amp;</span>gt;= <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min<span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_Len</span> <span style="color: #339933;">&amp;</span>lt;= <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max<span style="color: #009900;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We arrive at our only override from the Rule base class in line 84.  We declared this method to be abstract in the Rule base class, so we are required to implement the method in our new class. In line 91, we set out to get the value of the correct property from the object which has been passed in.  Since we are using a convention of &#8220;getters and setters&#8221;, we can construct the proper &#8220;get&#8221; call to grab the value from the object.  For example, if our rule is set to validate the LastName of a Person object at runtime, the call to get that value would be GetLastName().  Another call is placed to CheckValues() to make sure that the values are still good.  We do this because the object could have been edited with the interface methods since the initial call to the constructor.  Lines 96 to 99 create member variables representing how the object has been configured based on the min and max values.  The length of the property value is recorded for comparison to the min and max values.  Lines 101 to 119 set the m_IsValid variable for the correct configuration scenario.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">// Helper Methods</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Figures out how to display the rule text to the user based on the
	 * values of Min and Max.
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> CalculateDisplayText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">==</span> <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>	<span style="color: #666666; font-style: italic;">// we have a rule where only the max has been set</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_DisplayText <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_PropertyName <span style="color: #339933;">.</span> <span style="">' may be no longer than '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">.</span> <span style="">' characters in length.'</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">==</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>	<span style="color: #666666; font-style: italic;">// we have a rule where only the min has been set</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_DisplayText <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_PropertyName <span style="color: #339933;">.</span> <span style="">' must be at least '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">.</span> <span style="">' characters in length.'</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>lt; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max<span style="color: #009900;">&#41;</span>	<span style="color: #666666; font-style: italic;">// we have a rule where min and max have been set</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_DisplayText <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_PropertyName <span style="color: #339933;">.</span> <span style="">' must be between '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">.</span> <span style="">' and '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">.</span> <span style="">' characters in length.'</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Checks values for min and max set by the programmer.
	 * Min must be less than max if max is greater than zero.
	 * Max must be greater than min if min is greater than zero.
	 * Neither may be a negative number.
	 * Both values may not be equal to zero at the same time.
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> CheckValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// check for negative values</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>lt; <span style="color:#800080;">0</span> || <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">&amp;</span>lt; <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="">'Min or Max values may not be less than zero.'</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// check for double zeros.</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">==</span> <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">==</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="">'Min or Max values may not both be set to zero.'</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// check for min less than max when max is set</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>gt;= <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="">'If Max is set to a non zero number, Min must be less than Max.'</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// check for max greater than min when min is set</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>gt; <span style="color:#800080;">0</span> <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">&amp;</span>lt;= <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Min <span style="color: #339933;">&amp;</span>amp;&amp;amp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt;m_Max <span style="color: #339933;">!=</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="">'If Min is set to a non zero number, Max must be greater than Min.'</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt;</pre></div></div>

<p>CalculateDisplayText() creates a message for the user based on how the min and max values are set.  You can see that CheckValues() is throwing an exception if there are crazy values set up for an object of our class.  These kinds of errors would be the result of improper coding while using the class.  Their audience is the developer.  I feel that it is important to provide this kind of feedback for others.</p>
<h2>Conclusion</h2>
<p>We now have a base class and a product of that class to use going forward with our framework.  Hopefully, you&#8217;re thinking of a few other classes you might create to handle some common validation situations that you run in to.  Feel free to send in your code.  I will add them to the framework and discuss the new classes on the blog.  In the next article, we are going to build another abstract base class which makes use of the validation classes we have created so far.  I know it&#8217;s a long road, but we&#8217;ll have some really cool development tools as we move forward.</p>
<h2>Also Available in This Series</h2>
<ul>
<li><a title="A Business Objects Framework for Wordpress" href="http://www.darinboyd.com/wordpress/2009/01/a-business-objects-framework-for-php-part-1/" target="_blank">A Business Objects Framework for Wordpress</a></li>
<li><a title="Building Monkey Wrench - The Rule Classes" rel="bookmark" href="http://www.darinboyd.com/wordpress/2009/01/building-monkey-wrench-the-rule-classes/" target="_blank">Building Monkey Wrench &#8211; The Rule Classes</a></li>
</ul>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.darinboyd.com/wordpress/2009/01/creating-a-string-range-rule-with-monkey-wrench/feed/</wfw:commentRss>
		<slash:comments>250</slash:comments>
		</item>
		<item>
		<title>A Business Objects Framework for Wordpress</title>
		<link>http://www.darinboyd.com/wordpress/2009/01/a-business-objects-framework-for-php-part-1/</link>
		<comments>http://www.darinboyd.com/wordpress/2009/01/a-business-objects-framework-for-php-part-1/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 04:12:04 +0000</pubDate>
		<dc:creator>Darin Boyd</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Monkeywrench]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.darinboyd.com/wordpress/?p=5</guid>
		<description><![CDATA[In this series of articles, I will cover the development of a business objects framework written in PHP for the Wordpress blogging platform.  I come from a Java/C++/C# background, so I am quite fond of the benefits of Object Oriented Programming when it comes to validating and persisting data to a database.  The framework  I  [...]]]></description>
			<content:encoded><![CDATA[<p>In this series of articles, I will cover the development of a business objects framework written in PHP for the <a title="Wordpress website link" href="http://www.wordpress.net">Wordpress</a> blogging platform.  I come from a Java/C++/C# background, so I am quite fond of the benefits of Object Oriented Programming when it comes to validating and persisting data to a database.  The framework  I  will construct draws inspiration from my work on other platforms.<span id="more-5"></span>  Some of you who are experienced PHP programmers may think that my approach is strange.  Those of you with any experience with the <a title="CSLA" href="http://www.lhotka.net/cslanet/" target="_blank">CSLA</a> framework may see a lot of similarities in the tutorial framework and older versions of <a title="CSLA" href="http://www.lhotka.net/cslanet/" target="_blank">CSLA</a>.</p>
<h2>Why Build a Framework?</h2>
<p>Frameworks are useful for many reasons. Below is a short list of some of the most common benefits of developing a business objects framework.  For an exhaustive presentation on the rationale for using frameworks as well as a complete discussion of the design of a complete framework, I recommend <a title="Exper C# Business Objects" href="http://www.amazon.com/gp/product/1430210192?ie=UTF8&amp;tag=darinboyd-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1430210192" target="_blank">Expert C# 2008 Business Objects</a>.</p>
<ul>
<li><strong>Separation of Concerns</strong> &#8211; Code for the business logic and persistence details may exist in one or more layers.  This keeps database concerns and business logic out of the UI code.  A layered architecture makes it easier to build a variety of user interfaces for the same code base.</li>
<li><strong>Specialization of Tasks</strong> -   The layered architecture allows people on a team to work on the programming tasks for which they are most suited.  I am a terrible web developer.  I like the fact that I can build a robust core and let the people with black turtlenecks put a slick user interface on top of it.  It is also a relief to designers that they can concentrate on the interface and use a simple, clean API which has been prepared for them by the core programmers.</li>
<li><strong>Coding Standards</strong> &#8211; Frameworks set a good example by helping to establish coding style and methods for all of the developers on a team.  Even when working alone, the establishment of styles and patterns is very helpful and increases productivity.</li>
<li><strong>Collaboration</strong> &#8211; All of the above items result in better collaboration in a team environment.  It is easy to get help from others when you have a clean and easy to follow code base.  New trainees also get up to speed a lot faster in the presence of a framework.  This builds confidence very early for new people and it also means that they are actually helping out on day one.</li>
<li><strong>Code Generation</strong> &#8211; Established style and methodology make it easy to generate a significant percentage of your boring, boiler plate, error prone work using a code generation tool.  It is fairly easy to write code generation tools and there are a few really nice free tools to do the job as well.  We&#8217;ll get in to this in other articles.</li>
</ul>
<h2>What Will Our Framework Do?</h2>
<p>The framework we are going to design will take care of the following tasks.</p>
<ul>
<li><strong>CRUD </strong>- <strong>C</strong>reation, <strong>R</strong>etrieval, <strong>U</strong>pdate and <strong>D</strong>eletion of records in the database.</li>
<li><strong>Validation</strong> &#8211; Our objects will be self validating.  All validation rules for fields and business logic for the entities will be represented as classes.</li>
</ul>
<p>Our framework will be implemented in one layer.  I am creating the framework for use with the Wordpress blogging system, so I&#8217;ll make use of some of the nice database features built in to Wordpress.  The framework may be used outside the Wordpress environment by including the proper files which allow use of <strong>$wpdb</strong>.  For more information of <strong>$wpdb</strong>, go <a title="Wordpress wpdb info" href="http://codex.wordpress.org/wpdb_Class" target="_blank">here</a>.  Of course, you could use another database.  It is also possible to separate the actual data access code into another suite of classes.  This takes a lot of extra work, but it does allow you to swap out your database technology without changing the rest of your code. This approach makes sense if you might switch database platforms.</p>
<p>Before we continue and actually build the framework, we should give it a name. I am tired of typing &#8220;the framework&#8221;.  For a working title, let&#8217;s call it <strong>Monkey Wrench.</strong> My wife, Jana, is rather fond of monkeys, so I&#8217;m sure she would approve.</p>
<h2>Coming Up. Building Monkey Wrench</h2>
<p>In the next article, we will begin building the <strong>Monkey Wrench</strong> framework.  See you next time!</p>
<h2>Also Available in This Series</h2>
<ul>
<li><a title="Building Monkey Wrench - The Rule Classes" rel="bookmark" href="http://www.darinboyd.com/wordpress/2009/01/building-monkey-wrench-the-rule-classes/" target="_blank">Building Monkey Wrench &#8211; The Rule Classes</a></li>
<li><a title="Creating a String Range Rule with Monkey Wrench" rel="bookmark" href="http://www.darinboyd.com/wordpress/2009/01/creating-a-string-range-rule-with-monkey-wrench/" target="_blank">Creating a String Range Rule with Monkey Wrench</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.darinboyd.com/wordpress/2009/01/a-business-objects-framework-for-php-part-1/feed/</wfw:commentRss>
		<slash:comments>553</slash:comments>
		</item>
	</channel>
</rss>
