My Java app converts its modal object to JSON (using the GSON Library).
This is just fine, but I have to get it back into a modal in PHP. Now I've created a modal (in my case it's, for example, GeoLocation
) which has several getters and setters.
Example:
class GeoLocation {
private $cellTowerId;
private $dateCreated;
private $dateModified;
private $geoLocationId;
private $geoLocationLatitude;
private $geoLocationLongitude;
private $geoLocationName;
private $geoLocationStatus;
private $geoLocationTime;
private $isSynch;
private $geoLocationAccuracy;
private $geoLocationAltitude;
private $geoLocationBearing;
private $geoLocationProvider;
private $geoLocationSpeed;
private $geoStreet;
private $geoPostalCode;
private $geoLocality;
private $geoCountryName;
private $geoCountryCode;
private $geoFeatureName;
private $geoPhone;
public function getCellTowerId() {
return $this->cellTowerId;
}
public function getDateCreated() {
return $this->dateCreated;
}
public function getDateModified() {
return $this->dateModified;
}
public function getGeoLocationId() {
return $this->geoLocationId;
}
public function getGeoLocationLatitude() {
return $this->geoLocationLatitude;
}
public function getGeoLocationLongitude() {
return $this->geoLocationLongitude;
}
public function getGeoLocationName() {
return $this->geoLocationName;
}
// and many more...
}
I have a loader class which processes the JSON and should re-create the object as soon as the set<Varname>()
method exists.
Are there any "security" problems so far or any way to improve it?
public function process() {
foreach (parent::getData() as $key) {
try {
$geolocation = new GeoLocation();
foreach ($key as $keyName => $keyValue) {
if (method_exists($geolocation, "set" . ucfirst($keyName))) {
call_user_func(array($geolocation, "set" . ucfirst($keyName)), $keyValue);
}
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
}
Since the class methods are always synchronized with the Java classes, the getter and setter methods should always exist. Is there maybe a way of doing the getters and setters dynamically using the JSON?
If there is JSON data which has no methods, like setJSONDataOfEvilStuff()
, then it's not necessarily so far.