Kontakt - Datenschutz
Subversion
<?php
class Model {
private $_values = [];
private $_formats = [];
public function __construct() {
$this->addGetFormat('raw' , function($value) {return $value;});
$this->addGetFormat('lowercase' , function($value) {return strtolower($value);});
$this->addGetFormat('uppercase' , function($value) {return strtoupper($value);});
$this->addGetFormat('specialchars', function($value) {return htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');});
$this->addGetFormat('noquotes' , function($value) {return htmlspecialchars($value, ENT_NOQUOTES | ENT_HTML5, 'UTF-8');});
}
public function addGetFormat($formatName, callable $callback) {
$this->_formats[$formatName] = $callback;
}
// replaceGetFormat
// overwriteGetFormat
// deleteGetFormat
// removeGetFormat
public function __get($name) {
// $obj->foo§uppercase
if(preg_match('#^([^§]+)§([^§]+)$#', $name, $matches)) {
$name = $matches[1];
$format = $matches[2];
} else {
$format = 'raw';
}
$value = $this->_values[$name];
return $this->_formats[$format]($value);
}
public function __set($name, $value) {
$this->_values[$name] = $value;
}
}
$obj = new Model();
$obj->foo = 'aBcDeF " &';
echo 'raw : ', $obj->foo, "\n";
echo 'uppercase : ', $obj->foo§uppercase, "\n";
echo 'lowercase : ', $obj->foo§lowercase, "\n";
echo 'specialchars: ', $obj->foo§specialchars, "\n";
echo 'noquotes : ', $obj->foo§noquotes, "\n";
$obj->addGetFormat('reverse', function($value) {return strrev($value);});
echo 'reverse : ', $obj->foo§reverse, "\n";
?>