Kontakt - Datenschutz

Subversion

<?php
Trait GetFormatTrait {
    private $_values = [];


    public function __get($name) {
        // $obj->foo§uppercase
        if(preg_match('#^([^§]+)§([^§]+)$#', $name, $matches)) {
            $name   = $matches[1];
            $format = $matches[2];

            $value  = $this->_values[$name];


            switch($format) {
                case 'lowercase'   : return strtolower($value); break;
                case 'uppercase'   : return strtoupper($value); break;
                case 'specialchars': return htmlspecialchars($value, ENT_QUOTES   | ENT_HTML5, 'UTF-8'); break;
                case 'noquotes'    : return htmlspecialchars($value, ENT_NOQUOTES | ENT_HTML5, 'UTF-8'); break;

                default:
                    throw new \Exception("Unknown format '{$format}'.");
                    break;
            }
        } else {
            return $this->_values[$name];
        }
    }


    public function __set($name, $value) {
        $this->_values[$name] = $value;
    }
}
?>