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 = ad
	form.domain_adminperson.object = AdministrativniKontakt
	form.domain_adminperson.field.adName = Ime
	form.domain_adminperson.field.adLastname = Prezime
	form.domain_adminperson.field.adEmail = Email
	form.domain_adminperson.field.adAddress = Adresa
	form.domain_adminperson.field.adCity = Grad
	form.domain_adminperson.field.adState = Drzava
	form.domain_adminperson.field.adTelephone = Tel
	form.domain_adminperson.field.adJMBG = MaticniBroj
	form.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.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!