<?php
/**
 * A class for rendering html paragraph summaries.
 */
class view_widget_summary
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Renders the summary (shortened version) of a paragraph.
     * 
     * @param string $paragraph The paragraph.
     * @param integer (optional) $limit The word limit.
     * @param string (optional) $append The appending caption.
     * @throws invalidargumentexception If the $limit parameter is not numeric.
     * @return string The rendered paragraph.
     */
    public static function render($paragraph, $limit = 50, $append = '...')
    {
        if (!is_numeric($limit))
        {
	    throw new invalidargumentexception('Argument 2 passed to ' . __METHOD__ . '() must be numeric, ' . gettype($limit) . ' given.');
        }
        
        if ($limit < 0)
        {
            $limit = 0;
        }
        
        $paragraph = htmlspecialchars(preg_replace('~ +~', ' ', str_replace('&nbsp;', ' ', $paragraph)));
        $word = explode(' ', $paragraph);
        return (count($word) > $limit) ? implode(' ', array_slice($word, 0, $limit)) . $append : $paragraph;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
