Php classes (I think)

Is there a way to create a php class (or function) that "simplifies" this

ucfirst(str_replace('_',' ',html_entity_decode(trim($variable), ENT_QUOTES))));

$ variable can "come" from anywhere in the world, for example, from another function or just a "standard" variable

+5
source share
2 answers

If you use it more than once, I would definitely include it in the function. This way you will not repeat all the code.

function functionName($input){
    return ucfirst(str_replace('_',' ',html_entity_decode(trim($input), ENT_QUOTES)));
}

echo functionName($variable);
+3
source

( ), . . , , , . , .

- .

interface IFilter {
    /**
     * @param Mixed $value The value to be filtered
     * @return Mixed The filtered value
     */
    public function filter($value);
}

IFilter. , , , , filter(), $value. , doc , , . :

class ucFirstFilter implements IFilter
{
    public function filter($value) {
        return ucfirst($value);
    }
}

class TrimFilter implements IFilter
{
    public function filter($value) {
        return trim($value);
    }
}

, PHP. :

$trimFilter = new TrimFilter;
echo trimFilter->filter('   trim me   ');
// returns 'trim me'

, :

class SeparatorToSeparatorFilter implements IFilter
{
    protected $_separator;
    protected $_replacement;
    public function __construct($separator = '_', $replacement = ' ')
    {
        $this->_separator = $separator;
        $this->_replacement = $replacement;
    }
    public function filter($value) {
        return str_replace($this->_separator, $this->_replacement, $value);
    }
}

class HtmlEntityDecodeFilter implements IFilter
{
    protected $_quoteStyle;
    protected $_charset;
    public function __construct($quoteStyle=ENT_COMPAT, $charset='ISO-8859-1')
    {
        $this->_quoteStyle = $quoteStyle;
        $this->_charset = $charset;
    }
    public function filter($value) {
        return html_entity_decode($value, $this->_quoteStyle, $this->_charset);
    }
}

, . , , . . :

$trimFilter = new TrimFilter;
$separatorFilter = new SeparatorToSeparatorFilter('-');
echo $separatorFilter->filter($trimFilter->filter('   trim-me   '));
// returns 'trim me';

. Dont. . . , , , , FilterChain:

class FilterChain implements IFilter
{
    protected $_filters;
    public function __construct()
    {
        $this->_filters = new SplObjectStorage;
    }
    public function chain(IFilter $filter)
    {
        $this->_filters->attach($filter);
        return $this;
    }
    public function remove(IFilter $filter)
    {
        $this->_filters->detach($filter);
        return $this;
    }
    public function filter($value) {
        foreach($this->_filters as $filter) {
            $value = $filter->filter($value);
        }
        return $value;
    }
}

FilterChain , IFilter, filter() , , chain() , $value:

$filterChain = new FilterChain;
$filterChain->chain(new ucFirstFilter)
            ->chain(new SeparatorToSeparatorFilter)
            ->chain(new HtmlEntityDecodeFilter(ENT_QUOTES, 'UTF-8'))
            ->chain(new TrimFilter);

echo $filterChain->filter('  i am a "string_to_be_filtered"  ');
// outputs 'i am a "string to be filtered"'

FilterChain IFilter, FilterChains. . , ,

$chain1 = new FilterChain;
$chain1->chain(new ucFirstFilter)
       ->chain(new SeparatorToSeparatorFilter);

$chain2 = new FilterChain;
$chain2->chain($chain1);
$chain2->chain(new HtmlEntityDecodeFilter(ENT_QUOTES, 'UTF-8'))
       ->chain(new TrimFilter);

, , . , , , - , . , , trim(), , . FilterChain . FilterChain - , .

, , Zend_Filter (, ZF).

+4

All Articles