With our Mach-II apps, I had a series of RegEx Search/Replaces in Dreamweaver that let me quickly turn a list of variables into the various basic components of a bean, gateway, function arguments, etc. For example, this one does all the properties in a bean.
So if I had a variable list like this:
bookid, title, isbn, dateacquired, numberofpages, formats_formatid, genres_genreid, series_seriesid, plotsummary, animania_url, amazon_asin, series_position, publishers_publisherid
I’d select the list (i.e. highlight it), then run my search to get this:
<cfproperty name="bookid" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="title" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="isbn" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="dateacquired" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="numberofpages" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="formats_formatid" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="genres_genreid" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="series_seriesid" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="plotsummary" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="animania_url" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="amazon_asin" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="series_position" getter="true" setter="true" type="string" required="false" default="" /> <cfproperty name="publishers_publisherid" getter="true" setter="true" type="string" required="false" default="" />
Then I just adjust the required, type, and default attributes as needed and moved on. Such scripts combined with a core model template enabled me to create a very basic model for any table quickly. However, with our Zend Framework 2 apps, these search replaces don’t work as well because of the mixing between case found on the same line. Dreamweaver’s search/replace, even with RegEx, can’t flip cases on the fly.
So I could either use them then have to go flip the case as needed (which was a pain since Dreamweaver won’t let you put a keyboard shortcut on their convert to lower case function), or find another solution because typing it all out is tedious, especially when you have tables four times the size of the one whose fields are listed above!
Since I also wanted something my partner could use, if he desired, and I needed to continue focusing on PHP coding, I decided to make a little web page for “cheaters and helpers”. For now, it just has the first function, to generate the three sets of things we need for entities. The nice thing, of course, is that it can also handle flipping the casing, as needed, to make our conventions.
For simplicity sake, the script is pretty simple (no framework here) and since it’s only on our internal dev box, I didn’t bother adding security levels.
<?php function upperCamelMe($stringToConvert) { $convertedString = str_replace("_", " ", $stringToConvert); $convertedString = preg_replace('/\b([a-z])/e', 'strtoupper(\'$1\')', $convertedString); $convertedString = str_replace(" ", "_", $convertedString); $convertedString = str_ireplace("_id", "_ID", $convertedString); return $convertedString; } ?>
<?php include('functions.php'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>SDG Cheats and Helpers</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="author" content="SDG"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css"> <!-- Scripts --> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <script language="javascript"> jQuery.fn.selectText = function(){ var doc = document; var element = this[0]; // console.log(this, element); if (doc.body.createTextRange) { var range = document.body.createTextRange(); range.moveToElementText(element); range.select(); } else if (window.getSelection) { var selection = window.getSelection(); var range = document.createRange(); range.selectNodeContents(element); selection.removeAllRanges(); selection.addRange(range); } }; $(document).ready(function(){ $(".tab-pane").on( "click", "pre", function() { $(this).selectText(); }); }); </script> </head> <body role="document"> <header> <div class="container"> <h1 class="text-center">SDG Cheaters and Helpers :-)</h1> </div> </header> <div class="container" role="main">
</div> </body> </html>
<?php include('header.php'); // give the form to get the variables ?> <h2 class="text-center">Generate Entity Stuff</h2> <p>This helper will generate things for a Zend-style entity. Specifically: </p> <ul> <li>the protected list that goes at the top of your entity </li> <li>the $this->set for your exchange array</li> <li>the getters/setters to finish off your entity</li> </ul> <form method="post" action="generateEntity.php"> <div class="form-group"> <label for="fieldList" class="text-danger">So give me a comma separated list of the fields you need that stuff for! You can copy/paste from PHP MyAdmin or MySQL Workbench with or without the tick marks (but no table names!).</label> <textarea name="fieldList" id="fieldList" class="form-control" rows="4"><?php if (isset($_POST['fieldList'])) echo $_POST['fieldList']; ?></textarea> </div> <p class="text-center text-muted">(option to just pick DB and table to come later...)</p> <div class="row"> <div class="col-sm-offset-5 col-sm-1"> <button type="submit" class="btn btn-primary"><strong>GENERATE ME!</strong></button> </div> </div> </form> <?php // run the generators if (isset($_POST['fieldList']) || !empty($_POST['fieldList'])): ?> <div class="container" style="border-top: solid 1px #336600; margin-top: 20px;"> <h3 class="text-center"><span class="label label-success">Click inside the tab to automatically select all the code. You still have to copy yourself - browser security stuff and all.</span></h3> <div role="tabpanel"> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#protecteds" aria-controls="protecteds" role="tab" data-toggle="tab">Protecteds</a></li> <li role="presentation"><a href="#thisSets" aria-controls="thisSets" role="tab" data-toggle="tab">thisSets</a></li> <li role="presentation"><a href="#getterSetters" aria-controls="getterSetters" role="tab" data-toggle="tab">Getter/Setters</a></li> </ul> <?php $cleanedFieldList = str_replace(" ", "", $_POST['fieldList']); $cleanedFieldList = str_replace("`", "", $cleanedFieldList); $aFieldList = explode(",", $cleanedFieldList); $aFieldsLower = array(); $aFieldsCamel = array(); // first, make arrays with our fields in the appropriate formats foreach($aFieldList as $key => $thisField): array_push($aFieldsLower, strtolower($thisField)); array_push($aFieldsCamel, upperCamelMe(strtolower($thisField))); endforeach; // now, do our generations, starting with the protecteds echo "<div class='tab-content'><div role='tabpanel' class='tab-pane active' id='protecteds'>\n" . '<pre class="language-php" data-lang="php">'; foreach($aFieldList as $index => $field): echo " protected $" . $aFieldsLower[$index] . ";";endforeach; echo "</pre></div>\n\n"; // then the this sets echo "<div role='tabpanel' class='tab-pane' id='thisSets'>\n" . '<pre class="language-php" data-lang="php">'; foreach($aFieldList as $index => $field): echo ' $this->set' . $aFieldsCamel[$index] . '((isset($data[\'' . $aFieldsLower[$index] . '\'])) ? $data[\'' . $aFieldsLower[$index] . "'] : null);";endforeach; echo "</pre></div>\n\n"; echo "<div role='tabpanel' class='tab-pane' id='getterSetters'>" . '<pre class="language-php" data-lang="php">'; foreach($aFieldList as $index => $field): echo " public function get" . $aFieldsCamel[$index] . "() { return \$this->" . $aFieldsLower[$index] . "; } public function set" . $aFieldsCamel[$index] . "($" . $aFieldsLower[$index] . ") { \$this->" . $aFieldsLower[$index] . " = $" . $aFieldsLower[$index] . "; } "; endforeach; echo "</pre></div>\n\n"; ?> <?php echo "</div></div></div>\n\n"; endif; include('footer.php'); ?>
You’ll probably notice the indenting in our generateEntity is slightly funky – this is because it’s echoing out in a pre-tag. When I indented normally, it was getting added it to the output LOL
So how does it work? Once we go to the page, we paste in our list of variables (with or without those stupid tick marks that PHP MyAdmin puts in field names).
Hit the button and done, all three available:
If you click inside a tab, it automatically selects the whole thing so you can just copy and go.
When I have more time to play with it, I’ll probably add the option to select a database and table to have it get the fields itself. I also still need to add some for building other key components, like the basic CRUD queries, but it’s a start and it’s already been tremendously helpful 🙂