Source for file Util.php

Documentation is available at Util.php

  1. <?
  2.  
  3. /** Provides utility methods to convert variables to a specified type.
  4.  *
  5.  * @package    php-lastfm-api
  6.  * @author  Felix Bruns <felixbruns@web.de>
  7.  * @version    1.0
  8.  */
  9. class Util {
  10.     /** Returns the string value of a variable.
  11.      *
  12.      * @param    mixed    $var    An object.
  13.      * @return    string            A string.
  14.      *
  15.      * @static
  16.      * @access    public
  17.      */
  18.     public static function toString($var){
  19.         return ($var && trim($var))?strval($var):'';
  20.     }
  21.  
  22.     /** Returns the integer value of a variable.
  23.      *
  24.      * @param    mixed    $var    An object.
  25.      * @return    integer            An integer.
  26.      *
  27.      * @static
  28.      * @access    public
  29.      */
  30.     public static function toInteger($var){
  31.         return ($var && trim($var))?intval($var):0;
  32.     }
  33.  
  34.     /** Returns the floating-point value of a variable.
  35.      *
  36.      * @param    mixed    $var    An object.
  37.      * @return    float            A floating-point number.
  38.      *
  39.      * @static
  40.      * @access    public
  41.      */
  42.     public static function toFloat($var){
  43.         return ($var && trim($var))?floatval($var):0.0;
  44.     }
  45.  
  46.     /** Returns the boolean value of a variable.
  47.      *
  48.      * @param    mixed    $var    An object.
  49.      * @return    boolean            A boolean.
  50.      *
  51.      * @static
  52.      * @access    public
  53.      */
  54.     public static function toBoolean($var){
  55.         switch(Util::toString($var)){
  56.             case 'true':
  57.                     return true;
  58.             case 'false':
  59.                     return false;
  60.             default:
  61.                 return !!intval($var);
  62.         }
  63.     }
  64.  
  65.     /** Returns the unix timestamp value of a variable.
  66.      *
  67.      * @param    mixed    $var    An object.
  68.      * @return    integer            A unix timestamp.
  69.      *
  70.      * @static
  71.      * @access    public
  72.      */
  73.     public static function toTimestamp($var){
  74.         return ($var && trim($var))?strtotime(strval($var)):0;
  75.     }
  76.  
  77.     /** Returns the image type value of a variable.
  78.      *
  79.      * @param    mixed    $var    An object.
  80.      * @return    integer            An image type.
  81.      *
  82.      * @static
  83.      * @access    public
  84.      */
  85.     public static function toImageType($var){
  86.         switch(Util::toString($var)){
  87.             case 'small':
  88.                     return Media::IMAGE_SMALL;
  89.             case 'medium':
  90.                     return Media::IMAGE_MEDIUM;
  91.             case 'large':
  92.                     return Media::IMAGE_LARGE;
  93.             case 'hude':
  94.                     return Media::IMAGE_HUGE;
  95.             case 'extralarge':
  96.                     return Media::IMAGE_EXTRALARGE;
  97.             case 'original':
  98.                     return Media::IMAGE_ORIGINAL;
  99.             default:
  100.                 return Media::IMAGE_UNKNOWN;
  101.         }
  102.     }
  103.  
  104.     /** Converts any string or array of strings to UTF8.
  105.      *
  106.      * @param    mixed    $object    A String or an array.
  107.      * @return    mixed            UTF8-string or array.
  108.      *
  109.      * @static
  110.      * @access    public
  111.      */
  112.     public static function toUTF8($object){
  113.         if(is_array($object)){
  114.             return array_map(array('Util''toUTF8')$object);
  115.         }
  116.  
  117.         return mb_convert_encoding($object"UTF-8""auto");
  118.     }
  119. }
  120.  
  121. ?>

Documentation generated on Mon, 22 Dec 2008 16:57:52 +0100 by phpDocumentor 1.4.1