3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @xmlNamespace http://www.webservices.nl/soap/ * @xmlType * @xmlName RiskAddress */ class Address { /** * @xmlType string * @xmlRender value * @xmlName postcode * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_postcode; /** * @xmlType string * @xmlRender value * @xmlName building * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_building; /** * @xmlType string * @xmlRender value * @xmlName street * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_street; /** * @xmlType integer * @xmlRender value * @xmlName house_number * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_house_number; /** * @xmlType string * @xmlRender value * @xmlName house_number_addition * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_house_number_addition; /** * @xmlType string * @xmlRender value * @xmlName city * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_city; /** * @xmlType string * @xmlRender value * @xmlName municipality * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_municipality; /** * @xmlType string * @xmlRender value * @xmlName state * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_state; /** * @xmlType string * @xmlRender value * @xmlName country * @xmlNamespace http://www.webservices.nl/soap/ */ protected $_country; /** * @return string */ public function getPostcode() { return $this->_postcode; } /** * @param string $postcode */ public function setPostcode($postcode) { $this->_postcode = (string) $postcode; } /** * @return string */ public function getBuilding() { return $this->_building; } /** * @param string $building */ public function setBuilding($building) { $this->_building = (string) $building; } /** * @return string */ public function getStreet() { return $this->_street; } /** * @param string $street */ public function setStreet($street) { $this->_street = (string) $street; } /** * @return integer */ public function getHouseNumber() { return $this->_house_number; } /** * @param (integer) $houseNumber */ public function setHouseNumber($houseNumber) { $this->_house_number = (integer) $houseNumber; } /** * @return string */ public function getHouseNumberAddition() { return $this->_house_number_addition; } /** * @param string $houseNumberAddition */ public function setHouseNumberAddition($houseNumberAddition) { $this->_house_number_addition = (string) $houseNumberAddition; } /** * @return string */ public function getCity() { return $this->_city; } /** * @param string $city */ public function setCity($city) { $this->_city = (string) $city; } /** * @return string */ public function getMunicipality() { return $this->_municipality; } /** * @param string $municipality */ public function setMunicipality($municipality) { $this->_municipality = $municipality; } /** * @return string */ public function getState() { return $this->_state; } /** * @param string $state */ public function setState($state) { $this->_state = (string) $state; } /** * @return string */ public function getCountry() { return $this->_country; } /** * @param string $country */ public function setCountry($country) { $this->_country = (string) $country; } } class AddressHelper { public static function parse(Address $address, $parts) { $parts = func_get_args(); $address = array_shift($parts); $format = '~^(.*?\n)??' . '(?<street>[^0-9].*|.*?[^0-9])[\b\s]*?' . '(?<number>[0-9]{1,5}?)[\b\s\-]*?' . '(?<addition>[^\s]*?)[\b\s,]*?' . '(?<postcode>[0-9]{4}\s?[A-Z]{2})[\b\s,]*?' . '(?<city>[\w][\w\s]*?)' . '(\n.*)??$~msiU'; if (preg_match($format, implode(PHP_EOL, $parts), $args)) { $street = $number = $postcode = $city = $addition = null; extract($args, EXTR_OVERWRITE); $address->setStreet($street); $address->setHouseNumber($number); $address->setHouseNumberAddition($addition); $address->setPostcode($postcode); $address->setCity(strtoupper(trim($city))); } return $address; } } class Marshaller { protected $_includeNilValues = true; public function __construct($includeNilValues = true) { $this->_includeNilValues = $includeNilValues; } public function marshall($object) { return $this->_marshallObject($object); } protected function _marshallObject($object) { $output = array(); $refClass = new ReflectionClass($object); foreach ($refClass->getProperties(ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC) as $refProp) { $propValue = $object->{$this->_getPropertyAccessor($refProp->getName())}(); $this->_marshallProperty($refProp->getName(), $propValue, $output); } return $output; } protected function _marshallProperty($propName, &$propValue, array &$output) { if (($propValue === null || $propValue === '') && $this->_includeNilValues === false) return; if (is_object($propValue)) { $this->_assignArrayValue($output, $this->_getPropertyArrayKey($propName), $this->_marshallObject($propValue)); } elseif (is_array($propValue)) { $arrayOutput = array(); foreach ($propValue as $nestedPropVal) { $array = array(); $this->_marshallProperty($this->_getPropertyArrayKey($propName), $nestedPropVal, $array); $arrayOutput = array_merge_recursive($arrayOutput, array(array_shift($array))); } if ('' !== implode('', $arrayOutput)) $output[$this->_getPropertyArrayKey($propName)] = $arrayOutput; } else { $this->_assignArrayValue($output, $this->_getPropertyArrayKey($propName), $propValue); } } protected function _assignArrayValue(array &$output, $key, $value) { // remove empty arrays when include nilvalues is set to true if (false === $this->_includeNilValues && is_array($value) && empty($value) && '' == implode('', $value)) { return; } if (false === $this->_includeNilValues && (is_null($value) || $value === '')) { return; } $output[$key] = $value; } /** * Convert a class property name to an array key * @param string $propName * @return string */ protected function _getPropertyArrayKey($propName) { return ltrim($propName, '_'); } /** * Get the accessor for a class property name * @param string $propName * @return string */ protected function _getPropertyAccessor($propName) { $propName = ltrim($propName, '_'); $propParts = explode('_', $propName); foreach ($propParts as &$propPart) { $propPart = ucfirst($propPart); } return 'get' . implode('', $propParts); } public function includeNilValues() { $this->_includeNilValues = true; } public function excludeNilValues() { $this->_includeNilValues = false; } } //$parsedAddress = AddressHelper::parse(new Address(), null, null, null, null, null)); $address = new Address(); $marshaller = new Marshaller(true); $marshaller->marshall($address); $marshaller->marshall($address); var_dump($marshaller->marshall($address));

preferences:
39.05 ms | 402 KiB | 5 Q