3v4l.org

run code in 300+ PHP versions simultaneously
<?php class DataMapper { /** * @var array */ private $rawData; /** * @var array */ private $parsedData; /** * @var array */ private $mapping = [ 'dbField1' => 'outputField1', 'dbField2' => ['outputField2' => ['parseFn1', 'parseFn2']] ]; public function __construct(array $data) { $this->rawData = $data; $this->mapData(); } /** * @param $value * @return mixed */ private function parseFn1($value) { return $value * 2; } /** * @param $value * @return string */ private function parseFn2($value) { return $value . "$"; } // Yes, I know the methods shouldn't be here but it rather be decoupled in another class and // injected with a DI it but I wrote it like this with example purposes only private function mapData() { foreach($this->mapping as $dbFieldName => $content) { if(isset($this->rawData[$dbFieldName])) { if(is_string($content)) { $parsedData[$content] = $this->rawData[$dbFieldName]; } else if (is_array($content)) { $frontFieldName = key($content); $functionsArray = reset($content); $value = $this->rawData[$dbFieldName]; foreach($functionsArray as $functionName) { $value = $this->{$functionName}($value); } $parsedData[$frontFieldName] = $value; } } } } /** * Simple getter * @return array */ public function getParseData() { return $this->parsedData; } } // Usually the data comes from the DB so it would be in some kind of Row Set structure $data = ['dbField1' => 5, 'dbField2' => 13]; // The data is handed to the Holder which parses it $dm = new DataMapper($data); // The data is parsed so it can be var_dump($dm);
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
object(DataMapper)#1 (3) { ["rawData":"DataMapper":private]=> array(2) { ["dbField1"]=> int(5) ["dbField2"]=> int(13) } ["parsedData":"DataMapper":private]=> NULL ["mapping":"DataMapper":private]=> array(2) { ["dbField1"]=> string(12) "outputField1" ["dbField2"]=> array(1) { ["outputField2"]=> array(2) { [0]=> string(8) "parseFn1" [1]=> string(8) "parseFn2" } } } }
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/bA9hE on line 16
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/bA9hE on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/bA9hE on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/bA9hE on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/bA9hE on line 6
Process exited with code 255.

preferences:
284.21 ms | 401 KiB | 455 Q