<?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; Wordpress</title>
	<atom:link href="http://www.darinboyd.com/wordpress/tag/wordpress/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>Building Monkey Wrench &#8211; The Rule Classes</title>
		<link>http://www.darinboyd.com/wordpress/2009/01/building-monkey-wrench-the-rule-classes/</link>
		<comments>http://www.darinboyd.com/wordpress/2009/01/building-monkey-wrench-the-rule-classes/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 03:37:54 +0000</pubDate>
		<dc:creator>Darin Boyd</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Monkeywrench]]></category>

		<guid isPermaLink="false">http://www.darinboyd.com/wordpress/?p=19</guid>
		<description><![CDATA[This is Part II in a series of articles dedicated to building a business objects framework for the Wordpress environment.  If you haven&#8217;t read Part I of the series, please check it out.  Part I introduces the concept of a framework.
Monkey Wrench Core Classes
The following UML diagram shows the basic class structure for the earliest [...]]]></description>
			<content:encoded><![CDATA[<p>This is <strong>Part II</strong> in a series of articles dedicated to building a business objects framework for the Wordpress environment.  If you haven&#8217;t read <a title="Link to A Business Objects Framework for Wordpress" href="http://www.darinboyd.com/wordpress/2009/01/a-business-objects-framework-for-php-part-1/" target="_blank">Part I</a> of the series, please check it out.  <strong>Part I</strong> introduces the concept of a framework.<span id="more-19"></span></p>
<h2>Monkey Wrench Core Classes</h2>
<p>The following UML diagram shows the basic class structure for the earliest version of our framework.  I&#8217;ll go over each class in detail over the next several articles.  This article will focus on dealing with validation rules.</p>
<div id="attachment_27" class="wp-caption alignnone" style="width: 518px"><img class="size-full wp-image-27" title="Monkey Wrench Class Diagram" src="http://www.darinboyd.com/wordpress/wp-content/uploads/2009/01/monkeywrenchclassdiagram2.png" alt="Monkey Wrench Class Diagram" width="508" height="506" /><p class="wp-caption-text">Monkey Wrench Class Diagram</p></div>
<p>The above diagram isn&#8217;t perfect UML.  I have simplified the classes to basic properties and operations for the sake of clarity.  The actual implementation will be more elaborate.  The green classes are abstract.  The blue classes are concrete classes built by inheriting from the base classes.</p>
<h2>The Abstract Rule Class</h2>
<p>As mentioned in <a title="Link to A Business Objects Framework for Wordpress" href="http://www.darinboyd.com/wordpress/2009/01/a-business-objects-framework-for-php-part-1/" target="_blank">Part I</a> of this series of articles, part of Monkey Wrench&#8217;s charter is to provide built in validation of our business objects.  We will need a way to represent a validation rule for our objects.  Validation may take on many forms, so it would be clunky to try and create a one-size-fits-all rule class.  Instead, let&#8217;s create an abstract base class from which to create rule classes that will do their own work.  Over time, you can build a nice library of validators and use them in your projects.  Custom one-off rule classes may be created for elaborate, specific validation tasks that are unique to a specific object.</p>
<p>Our Rule object should inform the user about the particulars of a validation or business rule.  It should also take in some parameters and evaluate the object.</p>
<h2>Properties of the Rule Class</h2>
<ul>
<li><strong>Name </strong>- A unique name to identify the Rule instance in a list.  Example: <em> &#8220;Last Name Length&#8221;</em></li>
<li><strong>DisplayText</strong> &#8211; A full description of the Rule to be displayed in the User Interface at run time.  Example: <em> &#8220;Last Name must be at least 5 characters in length.&#8221;</em></li>
<li><strong>PropertyName </strong>- Name of the property of our business object to which the validation rule instance is attached.  Example:  <em>&#8220;LastName&#8221;.</em></li>
</ul>
<h2>Operations of the Rule Class</h2>
<ul>
<li><strong>IsValid()</strong> &#8211; Returns the result of the validation.</li>
<li><strong>SetRuleValue()</strong> &#8211; Allows for the manual setting of the <strong>IsValid()</strong> value.  We&#8217;ll need this sometimes, but most often our Rule subclass will take care of setting this value.</li>
<li><strong>Validate()</strong> &#8211; An abstract method to be implemented by the subclasses.  This is where the object performs the actual validation.  The object to validate is passed in as a parameter to the Validate() method.</li>
</ul>
<h2>The Rule Class Implementation</h2>
<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&#8217;ll be looking at the <em><strong>Rule.php</strong></em> file.</p>
<p>This following section of code defines our abstract base class and a few protected member variables which may be accessed by our concrete subclasses.  All of the variables were explained earlier with the exception of the <strong>m_PropertyValue </strong>variable.  The <strong>m_PropertyValue</strong> variable is used to contain the actual data represented by the <strong>PropertyName</strong> on the object which is being validated.  So, if you have an object called Person with a LastName property, our subclass would fetch the value stored in Person-&gt;LastName and store that value in <strong>m_PropertyValue</strong>.</p>
<p>Notice that I am using JavaDoc style comments here.  The Doygen documentation tool can generate API documents from these comments.  This is a personal preference, but a useful artifact in your code if you want good documentation.  IDE&#8217;s such as PHPEclipse read these comments in real time and give you more feedback as to what your functions do with tooltips and prompts.</p>
<p>Also, I preface the class level variables with an &#8220;m_&#8221;.  I do this so that it is easy to tell the scope of the variable when you are down in a function.  Modern IDE&#8217;s like PHPEclipse make it easier to track down where a variable is declared.  Having a convention for scope is very handy when you don&#8217;t have a fancy IDE.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #0000ff; font-style: italic;">/**
 * Class representing a business or validation rule. *
 */</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Rule
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Member Variables</span>
	protected <span style="color: #000088;">$m_Name</span> <span style="color: #339933;">=</span> <span style="">''</span>;
	protected <span style="color: #000088;">$m_DisplayText</span> <span style="color: #339933;">=</span> <span style="">''</span>;
	protected <span style="color: #000088;">$m_PropertyName</span> <span style="color: #339933;">=</span> <span style="">''</span>;
	protected <span style="color: #000088;">$m_PropertyValue</span> <span style="color: #339933;">=</span> <span style="">''</span>;
	protected <span style="color: #000088;">$m_IsValid</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span>;</pre></td></tr></table></div>

<p>The next section has  the &#8220;getters&#8221; and &#8220;setters&#8221; one would expect in Java and other curly brace languages.  It is times like these when I really miss C#.  Maybe one day PHP will have properties.  For the Rule class, this is not required, but I&#8217;m trying to keep reasonable form here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">// Public State Methods</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Unique Name for the rule.
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GetName<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;">-&gt;</span><span style="color: #004000;">m_Name</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Sets unique Name for the rule.
	 *
	 * @param string $value
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetName<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;">-&gt;</span><span style="color: #004000;">m_Name</span> <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;">/**
	 * Name of the Property to be validated.
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GetPropertyName<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;">-&gt;</span><span style="color: #004000;">m_PropertyName</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Sets the name of the Property to be validated.
	 *
	 * @param string $value
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetPropertyName<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;">-&gt;</span><span style="color: #004000;">m_PropertyName</span> <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;">/**
	 * Display text for the rule.
	 * Used to notify the user of the exact purpose of the rule.
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GetDisplayText<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;">-&gt;</span><span style="color: #004000;">m_DisplayText</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Sets Display text for the rule.
	 *
	 * @param string $value
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetDisplayText<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;">-&gt;</span><span style="color: #004000;">m_DisplayText</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span>;
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the last bit of code, we have the <strong>IsValid() </strong>and <strong>SetRuleValue()</strong> methods described earlier in the article.  There is also a constructor and an abstract declaration for the <strong>Validate()</strong> method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;">	<span style="color: #0000ff; font-style: italic;">/**
	 * Indicates whether or not the rule has passed validation.
	 *
	 * @return boolean
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> IsValid<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;">-&gt;</span><span style="color: #004000;">m_IsValid</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Sets the value of the IsBroken member variable
	 * to the parameter value
	 *
	 * @param  boolean $value
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetRuleValue<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;">-&gt;</span><span style="color: #004000;">m_IsValid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Constructors</span>
&nbsp;
	<span style="color: #0000ff; font-style: italic;">/**
	 * Constructor: sets name and display text.
	 *
	 * @param  string $_name;
	 * @param  string $_displayText;
	 * @param  string $_propertyName;
	 */</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;">$_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_propertyName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_displayText</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_Name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_name</span>;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_PropertyName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_propertyName</span>;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_DisplayText</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_displayText</span>;		
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">// Abstract Methods</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> abstract <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>;
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Although our code doesn&#8217;t do very much, we have laid a reasonable foundation from which to build validators.  In the next article, we will use the Rule class to build a validator for string ranges.  Frameworks aren&#8217;t the most exciting things to read about, but I promise that you&#8217;ll be able to see the payoff if you hang in there with me.</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="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/building-monkey-wrench-the-rule-classes/feed/</wfw:commentRss>
		<slash:comments>725</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>
