Gdn_Format::to PHP Method

to() public static method

Takes a mixed variable, formats it in the specified format type, and returns it.
public static to ( mixed $Mixed, string $FormatMethod ) : mixed
$Mixed mixed An object, array, or string to be formatted.
$FormatMethod string The method with which the variable should be formatted.
return mixed
    public static function to($Mixed, $FormatMethod)
    {
        // Process $Mixed based on its type.
        if (is_string($Mixed)) {
            if (in_array(strtolower($FormatMethod), self::$SanitizedFormats) && method_exists('Gdn_Format', $FormatMethod)) {
                $Mixed = self::$FormatMethod($Mixed);
            } elseif (function_exists('format' . $FormatMethod)) {
                deprecated('format' . $FormatMethod, 'gdn_formatter_' . $FormatMethod, '2015-10-26');
                $FormatMethod = 'format' . $FormatMethod;
                $Mixed = $FormatMethod($Mixed);
            } elseif (function_exists('gdn_formatter_' . $FormatMethod)) {
                $FormatMethod = 'gdn_formatter_' . $FormatMethod;
                $Mixed = $FormatMethod($Mixed);
            } elseif ($Formatter = Gdn::factory($FormatMethod . 'Formatter')) {
                $Mixed = $Formatter->format($Mixed);
            } else {
                $Mixed = Gdn_Format::text($Mixed);
            }
        } elseif (is_array($Mixed)) {
            foreach ($Mixed as $Key => $Val) {
                $Mixed[$Key] = self::to($Val, $FormatMethod);
            }
        } elseif (is_object($Mixed)) {
            foreach (get_object_vars($Mixed) as $Prop => $Val) {
                $Mixed->{$Prop} = self::to($Val, $FormatMethod);
            }
        }
        return $Mixed;
    }

Usage Example

 function formatBody($Object)
 {
     Gdn::controller()->fireEvent('BeforeCommentBody');
     $Object->FormatBody = Gdn_Format::to($Object->Body, $Object->Format);
     Gdn::controller()->fireEvent('AfterCommentFormat');
     return $Object->FormatBody;
 }
All Usage Examples Of Gdn_Format::to