Current file: /home/vbrandao/next2u/Dropbox/noiselabs.org/projects/noiselabs-php-toolkit/src/NoiseLabs/ToolKit/Helper.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00%0.00%
0.00% 0 / 1
0.00%0.00%
0.00% 0 / 4 CRAP
0.00%0.00%
0.00% 0 / 50
 
Helper
0.00%0.00%
0.00% 0 / 1
0.00%0.00%
0.00% 0 / 4
0.00%0.00%
0.00% 0 / 50
 sanitizeFileName($dangerous_filename, $platform = 'unix')
0.00%0.00%
0.00% 0 / 1 6
0.00%0.00%
0.00% 0 / 5
 buildArrayFromObjectsProperty(array $objects, $property)
0.00%0.00%
0.00% 0 / 1 12
0.00%0.00%
0.00% 0 / 7
 buildArrayFromObjectsMethod(array $objects, $method)
0.00%0.00%
0.00% 0 / 1 12
0.00%0.00%
0.00% 0 / 7
 getPreferredLanguage(array $available_languages = array('en')
0.00%0.00%
0.00% 0 / 1 90
0.00%0.00%
0.00% 0 / 30


       1                 : <?php                                                                                                              
       2                 : /**                                                                                                                
       3                 :  * This file is part of NoiseLabs-PHP-ToolKit                                                                      
       4                 :  *                                                                                                                 
       5                 :  * NoiseLabs-PHP-ToolKit is free software; you can redistribute it                                                 
       6                 :  * and/or modify it under the terms of the GNU Lesser General Public                                               
       7                 :  * License as published by the Free Software Foundation; either                                                    
       8                 :  * version 3 of the License, or (at your option) any later version.                                                
       9                 :  *                                                                                                                 
      10                 :  * NoiseLabs-PHP-ToolKit is distributed in the hope that it will be                                                
      11                 :  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty                                             
      12                 :  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                            
      13                 :  * Lesser General Public License for more details.                                                                 
      14                 :  *                                                                                                                 
      15                 :  * You should have received a copy of the GNU Lesser General Public                                                
      16                 :  * License along with NoiseLabs-PHP-ToolKit; if not, see                                                           
      17                 :  * <http://www.gnu.org/licenses/>.                                                                                 
      18                 :  *                                                                                                                 
      19                 :  * Copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>                                                   
      20                 :  *                                                                                                                 
      21                 :  *                                                                                                                 
      22                 :  * @category NoiseLabs                                                                                             
      23                 :  * @package ToolKit                                                                                                
      24                 :  * @version 0.1.1                                                                                                  
      25                 :  * @author Vítor Brandão <noisebleed@noiselabs.org>                                                              
      26                 :  * @copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>                                                  
      27                 :  */                                                                                                                
      28                 :                                                                                                                    
      29                 : namespace NoiseLabs\ToolKit;                                                                                       
      30                 :                                                                                                                    
      31                 : /**                                                                                                                
      32                 :  * Helper holds a collection of static methods, useful for generic purposes                                        
      33                 :  *                                                                                                                 
      34                 :  * @author Vítor Brandão <noisebleed@noiselabs.org>                                                              
      35                 :  */                                                                                                                
      36                 : class Helper                                                                                                       
      37               0 : {                                                                                                                  
      38                 :     /**                                                                                                            
      39                 :      * Returns a safe filename, for a given platform (OS), by replacing all                                        
      40                 :      * dangerous characters with an underscore.                                                                    
      41                 :      *                                                                                                             
      42                 :      * @since 0.1.0                                                                                                
      43                 :      *                                                                                                             
      44                 :      * @param string $dangerous_filename The source filename to be "sanitized"                                     
      45                 :      * @param string $platform The target OS                                                                       
      46                 :      *                                                                                                             
      47                 :      * @return Boolean string A safe version of the input filename                                                 
      48                 :      */                                                                                                            
      49                 :     public static function sanitizeFileName($dangerous_filename, $platform = 'unix')                               
      50                 :     {                                                                                                              
      51               0 :         if (in_array(strtolower($platform), array('unix', 'linux'))) {                                             
      52                 :                 // our list of "dangerous characters", add/remove characters if necessary                          
      53               0 :                 $dangerous_characters = array(" ", '"', "'", "&", "/", "\\", "?", "#");                            
      54               0 :         }                                                                                                          
      55                 :         else {                                                                                                     
      56                 :                 // no OS matched? return the original filename then...                                             
      57               0 :                 return $dangerous_filename;                                                                        
      58                 :         }                                                                                                          
      59                 :                                                                                                                    
      60                 :         // every forbidden character is replace by an underscore                                                   
      61               0 :         return str_replace($dangerous_characters, '_', $dangerous_filename);                                       
      62                 :     }                                                                                                              
      63                 :                                                                                                                    
      64                 :     /**                                                                                                            
      65                 :      * Returns an array made from property values extracted from each object in                                    
      66                 :      * the array of objects.                                                                                       
      67                 :      *                                                                                                             
      68                 :      * @since 0.1.0                                                                                                
      69                 :      *                                                                                                             
      70                 :      * @param array $objects The collection of objects holding the target property                                 
      71                 :      * @param string $property Property name to collect data from                                                  
      72                 :      *                                                                                                             
      73                 :      * @return array                                                                                               
      74                 :      */                                                                                                            
      75                 :     public static function buildArrayFromObjectsProperty(array $objects, $property)                                
      76                 :     {                                                                                                              
      77               0 :         $values = array();                                                                                         
      78                 :                                                                                                                    
      79               0 :         foreach ($objects as $object) {                                                                            
      80               0 :             if (property_exists($object, $property)) {                                                             
      81               0 :                 $values[] = $object->$property;                                                                    
      82               0 :             }                                                                                                      
      83               0 :         }                                                                                                          
      84                 :                                                                                                                    
      85               0 :         return $values;                                                                                            
      86                 :     }                                                                                                              
      87                 :                                                                                                                    
      88                 :     /**                                                                                                            
      89                 :      * Returns an array made from values extracted the array of obects using                                       
      90                 :      * the given method.                                                                                           
      91                 :      *                                                                                                             
      92                 :      * @since 0.1.0                                                                                                
      93                 :      *                                                                                                             
      94                 :      * @param array $objects The collection of objects holding the target method                                   
      95                 :      * @param string $property Method name to ask for data                                                         
      96                 :      *                                                                                                             
      97                 :      * @return array                                                                                               
      98                 :      */                                                                                                            
      99                 :     public static function buildArrayFromObjectsMethod(array $objects, $method)                                    
     100                 :     {                                                                                                              
     101               0 :         $values = array();                                                                                         
     102                 :                                                                                                                    
     103               0 :         foreach ($objects as $object) {                                                                            
     104               0 :             if (method_exists($object, $method)) {                                                                 
     105               0 :                 $values[] = $object->$method();                                                                    
     106               0 :             }                                                                                                      
     107               0 :         }                                                                                                          
     108                 :                                                                                                                    
     109               0 :         return $values;                                                                                            
     110                 :     }                                                                                                              
     111                 :                                                                                                                    
     112                 :     /**                                                                                                            
     113                 :      * An implementation of PECL's http_negotiate_language as posted on                                            
     114                 :      * http://www.php.net/manual/en/function.http-negotiate-language.php by                                        
     115                 :      * Anonymous (03-Nov-2008 11:23).                                                                              
     116                 :      *                                                                                                             
     117                 :      * This function negotiates the clients preferred language based on its                                        
     118                 :      * Accept-Language HTTP header. The qualifier is recognized and languages                                      
     119                 :      * without qualifier are rated highest. The qualifier will be decreased by                                     
     120                 :      * 10% for partial matches (i.e. matching primary language).                                                   
     121                 :      *                                                                                                             
     122                 :      * @since 0.1.0                                                                                                
     123                 :      *                                                                                                             
     124                 :      * @param array $available_languages Array with language-tag-strings that                                      
     125                 :      * are available                                                                                               
     126                 :      * @param string $default_language The language to pick if none available                                      
     127                 :      *                                                                                                             
     128                 :      * @return string Returns the negotiated language or the default language                                      
     129                 :      */                                                                                                            
     130                 :     public static function getPreferredLanguage(array $available_languages = array('en'), $default_language = 'en')
     131                 :       {                                                                                                            
     132                 :         // All $available_languages values must be lowercase                                                       
     133               0 :         $available_languages = array_map('strtolower', $available_languages);                                      
     134                 :                                                                                                                    
     135               0 :         if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {                                                            
     136               0 :             return $default_language;                                                                              
     137                 :         }                                                                                                          
     138                 :         else {                                                                                                     
     139               0 :             $http_accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];                                              
     140                 :         }                                                                                                          
     141                 :                                                                                                                    
     142                 :         /**                                                                                                        
     143                 :          * standard for HTTP_ACCEPT_LANGUAGE is defined under                                                      
     144                 :          * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4                                          
     145                 :          * pattern to find is therefore something like this:                                                       
     146                 :          *    1#( language-range [ ";" "q" "=" qvalue ] )                                                          
     147                 :          * where:                                                                                                  
     148                 :          *    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )                                           
     149                 :          *    qvalue         = ( "0" [ "." 0*3DIGIT ] )                                                            
     150                 :          *            | ( "1" [ "." 0*3("0") ] )                                                                   
     151                 :          */                                                                                                        
     152               0 :         preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" .                                             
     153               0 :                    "(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i",                                           
     154               0 :                    $http_accept_language, $hits, PREG_SET_ORDER);                                                  
     155                 :                                                                                                                    
     156                 :         // default language (in case of no hits) is the first in the array                                         
     157               0 :         $bestlang = $available_languages[0];                                                                       
     158               0 :         $bestqval = 0;                                                                                             
     159                 :                                                                                                                    
     160               0 :         foreach ($hits as $arr)                                                                                    
     161                 :         {                                                                                                          
     162                 :             // read data from the array of this hit                                                                
     163               0 :             $langprefix = strtolower ($arr[1]);                                                                    
     164               0 :             if (!empty($arr[3]))                                                                                   
     165               0 :             {                                                                                                      
     166               0 :                 $langrange = strtolower ($arr[3]);                                                                 
     167               0 :                 $language = $langprefix . "-" . $langrange;                                                        
     168               0 :             }                                                                                                      
     169                 :             else                                                                                                   
     170                 :             {                                                                                                      
     171               0 :                 $language = $langprefix;                                                                           
     172                 :             }                                                                                                      
     173                 :                                                                                                                    
     174               0 :             $qvalue = 1.0;                                                                                         
     175                 :                                                                                                                    
     176               0 :             if (!empty($arr[5])) $qvalue = floatval($arr[5]);                                                      
     177                 :                                                                                                                    
     178                 :             // find q-maximal language                                                                             
     179               0 :             if (in_array($language,$available_languages) && ($qvalue > $bestqval))                                 
     180               0 :             {                                                                                                      
     181               0 :                 $bestlang = $language;                                                                             
     182               0 :                 $bestqval = $qvalue;                                                                               
     183               0 :             }                                                                                                      
     184                 :             // if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does) 
     185               0 :             elseif (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval))                     
     186                 :             {                                                                                                      
     187               0 :                 $bestlang = $langprefix;                                                                           
     188               0 :                 $bestqval = $qvalue*0.9;                                                                           
     189               0 :             }                                                                                                      
     190               0 :         }                                                                                                          
     191                 :                                                                                                                    
     192               0 :         return $bestlang;                                                                                          
     193                 :     }                                                                                                              
     194                 : }                                                                                                                  
     195                 :                                                                                                                    

Generated by PHP_CodeCoverage 1.0.4 using PHP 5.3.6-pl0-gentoo and PHPUnit 3.5.13 at Wed Jun 1 11:12:17 WEST 2011.