helper_plugin_data::_cleanData PHP Method

_cleanData() public method

Makes sure the given data fits with the given type
public _cleanData ( string $value, string | array $type ) : string
$value string
$type string | array
return string
    function _cleanData($value, $type)
    {
        $value = trim($value);
        if (!$value and $value !== '0') {
            return '';
        }
        if (is_array($type)) {
            if (isset($type['enum']) && !preg_match('/(^|,\\s*)' . preg_quote_cb($value) . '($|\\s*,)/', $type['enum'])) {
                return '';
            }
            $type = $type['type'];
        }
        switch ($type) {
            case 'dt':
                if (preg_match('/^(\\d\\d\\d\\d)-(\\d\\d?)-(\\d\\d?)$/', $value, $m)) {
                    return sprintf('%d-%02d-%02d', $m[1], $m[2], $m[3]);
                }
                if ($value === '%now%') {
                    return $value;
                }
                return '';
            case 'url':
                if (!preg_match('!^[a-z]+://!i', $value)) {
                    $value = 'http://' . $value;
                }
                return $value;
            case 'mail':
                $email = '';
                $name = '';
                $parts = preg_split('/\\s+/', $value);
                do {
                    $part = array_shift($parts);
                    if (!$email && mail_isvalid($part)) {
                        $email = strtolower($part);
                        continue;
                    }
                    $name .= $part . ' ';
                } while ($part);
                return trim($email . ' ' . $name);
            case 'page':
            case 'nspage':
                return cleanID($value);
            default:
                return $value;
        }
    }

Usage Example

 function testCleanData()
 {
     $helper = new helper_plugin_data();
     $this->assertEquals('', $helper->_cleanData('   ', ''));
     $this->assertEquals('', $helper->_cleanData('', ''));
     $this->assertEquals('', $helper->_cleanData(null, ''));
     $this->assertEquals('', $helper->_cleanData(false, ''));
     $this->assertEquals('', $helper->_cleanData('', 'dt'));
     $this->assertEquals('', $helper->_cleanData('this is not a date', 'dt'));
     $this->assertEquals('1234-01-01', $helper->_cleanData('1234-1-1', 'dt'));
     $this->assertEquals('1234-01-01', $helper->_cleanData('1234-01-01', 'dt'));
     $this->assertEquals('%now%', $helper->_cleanData('%now%', 'dt'));
     $this->assertEquals('', $helper->_cleanData('1234-01-011', 'dt'));
     $this->assertEquals('http://bla', $helper->_cleanData('bla', 'url'));
     $this->assertEquals('http://bla', $helper->_cleanData('http://bla', 'url'));
     $this->assertEquals('https://bla', $helper->_cleanData('https://bla', 'url'));
     $this->assertEquals('tell://bla', $helper->_cleanData('tell://bla', 'url'));
     $this->assertEquals('*****@*****.**', $helper->_cleanData('*****@*****.**', 'mail'));
     $this->assertEquals('[email protected] bla', $helper->_cleanData('[email protected] bla', 'mail'));
     $this->assertEquals('[email protected] bla word', $helper->_cleanData('[email protected] bla word', 'mail'));
     $this->assertEquals('[email protected] bla bla word', $helper->_cleanData('bla [email protected] bla word', 'mail'));
     $this->assertEquals('[email protected] bla bla word', $helper->_cleanData(' bla [email protected] bla word ', 'mail'));
     $this->assertEquals('123', $helper->_cleanData('123', 'page'));
     $this->assertEquals('123_123', $helper->_cleanData('123 123', 'page'));
     $this->assertEquals('123', $helper->_cleanData('123', 'nspage'));
     $this->assertEquals('test', $helper->_cleanData('test', ''));
     $this->assertEquals('test', $helper->_cleanData('test', array('type' => '')));
     $this->assertEquals('', $helper->_cleanData('test', array('type' => '', 'enum' => 'some other')));
 }