Article 5PRE CodeSOD: Are You Down With PHP?

CodeSOD: Are You Down With PHP?

by
Dan J.
from The Daily WTF on (#5PRE)
Who's Down With PHP?

PHP often gets a bad rap. A lot of the time, that's because it's used by developers that don't know what they're doing, just like there's nothing inherently wrong with spandex, but there are times, places and people where it is inappropriate. And don't get me wrong, the language has made big strides in recent years (good luck finding a web server hosting one of those versions, though). But there are just uses of PHP that reinforce that reputation. Robert Osswald provides this example from the contact-form editing code of a domain registrar database.

Let's say you have some JSON data from an AJAX request, and it looks like this:

array(13) {["controller"]=>string(7) "account"["action"]=>string(10) "changedata"["module"]=>string(7) "default"["data"]=>string(314) "[{"name":"adAddress","value":"Address 123"},{"name":"adCity","value":"MyCity"},{"name":"adZip","value":"12345"},{"name":"adState","value":"RS"},{"name":"adTelephone","value":"0112323555"},{"name":"adJMBG","value":"1706987782831"},{"name":"check[]","value":"domain_adminperson"},{"name":"cp_osobaid","value":"156"}]"["ajax"]=>string(15) "json_serialized"["adAddress"]=>string(11) "Address 123"["adCity"]=>string(6) "MyCity"["adZip"]=>string(5) "12345"["adState"]=>string(2) "RS"["adTelephone"]=>string(10) "0112323555"["adJMBG"]=>string(13) "1706987782831"["check"]=>array(1) {[0]=>string(18) "domain_adminperson"}["cp_osobaid"]=>string(3) "156"}

You want to turn the fields in that check[] array into an object, like this:

object(stdClass)#208 (1) {["AdministrativniKontakt"]=> object(stdClass)#207 (6) { ["Adresa"]=> string(11) "Address 123" ["Grad"]=> string(6) "MyCity" ["PostanskiBroj"]=> string(5) "12345" ["Drzava"]=> string(2) "RS" ["Tel"]=> string(10) "0112323555" ["MaticniBroj"]=> string(13) "1706987782831" }}

To map one to the other, you also have an .ini file like this:

form.domain_adminperson.prefix = adform.domain_adminperson.object = AdministrativniKontaktform.domain_adminperson.field.adName = Imeform.domain_adminperson.field.adLastname = Prezimeform.domain_adminperson.field.adEmail = Emailform.domain_adminperson.field.adAddress = Adresaform.domain_adminperson.field.adCity = Gradform.domain_adminperson.field.adState = Drzavaform.domain_adminperson.field.adTelephone = Telform.domain_adminperson.field.adJMBG = MaticniBrojform.domain_adminperson.field.adZip = PostanskiBroj

Once you've mapped the request array to the defined object, you want to persist it and return the object in the JSON response. Would you do it" like this?

class RequestToRegObjectMapper{public static function parseRequestToObj($request, $saveInSession = false){$iniFile = new Zend_Config_Ini(APP_FS_ROOT.'/lib/spec/formtoservice.ini','default', true);if($saveInSession == true){$sessionHandler = new Zend_Session_Namespace('domainreg'); }$response = new stdClass();foreach($request['check'] as $elem){$elemIniData = $iniFile->form->{$elem};$elemObjectName = $elemIniData->object;$response->{$elemObjectName} = new stdClass();$response->{$elemObjectName} = $sessionHandler->{$elemObjectName};foreach($request as $k=>$v){$key = isset($elemIniData->field->{$k}) ? $elemIniData->field->{$k} : null;if(!is_null($key) && !empty($v)){$response->{$elemObjectName}->{$key} = $v;}}if($saveInSession == true){if(!isset($sessionHandler->{$elemObjectName}))$sessionHandler->{$elemObjectName} = $response->{$elemObjectName};else{foreach (get_object_vars($response->{$elemObjectName}) as $key => $value) {$sessionHandler->{$elemObjectName}->{$key} = $value;}}} }return $response;}}public function changedataAction(){$this->_helper->layout->disableLayout();$this->_helper->viewRenderer->setNoRender(true); $request = RequestToRegObjectMapper::parseRequestToObj($this->REQPARAMS, false);if(in_array("domain_dns", $this->REQPARAMS["check"])){$requestParams = new stdClass();$requestParams->IdDomena = $this->REQPARAMS["cp_domenid"];if (!empty($request->DNS->Nazivservera1)) {$requestParams->NazivServera1 = $request->DNS->Nazivservera1;}if (!empty($request->DNS->Ipadresa1)) {$requestParams->IPAdresa1 = $request->DNS->Ipadresa1;}if (!empty($request->DNS->Nazivservera2)) {$requestParams->NazivServera2 = $request->DNS->Nazivservera2;}if (!empty($request->DNS->Ipadresa2)) {$requestParams->IPAdresa2 = $request->DNS->Ipadresa2;}$response = $this->serviceClient->call('izmeniDNSServereZaDomen', $requestParams);echo json_encode($response);die();}if(in_array("domain_regperson", $this->REQPARAMS["check"])){$requestParams = new stdClass();$requestParams = $request->Registrant;$requestParams->Id = $this->REQPARAMS["Id"];$response = $this->serviceClient->call('izmeniRegistranta', $requestParams);if($response->status == 0){$this->redirectToActionController('index', 'account');}}if(in_array("domain_adminperson", $this->REQPARAMS["check"]) || in_array("domain_payperson", $this->REQPARAMS["check"])){$requestParams = new stdClass();if(in_array("domain_adminperson", $this->REQPARAMS["check"]))$requestParams = $request->AdministrativniKontakt;else if(in_array("domain_payperson", $this->REQPARAMS["check"]))$requestParams = $request->KontaktZaPlacanje;$requestParams->Id = $this->REQPARAMS["cp_osobaid"];$response = $this->serviceClient->call('izmeniOsobu', $requestParams);echo json_encode($response);die();}}

There are lots of reasons to use PHP on your next project! Alas, brevity and readability aren't two of them.

puppetlabs50.png[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today! TheDailyWtf?d=yIl2AUoC8zA6b-q_I1U3CM
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments