1 <?php
 2 /**
 3  * @package     FrameworkOnFramework
 4  * @subpackage  utils
 5  * @copyright   Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
 6  * @license     GNU General Public License version 2 or later; see LICENSE.txt
 7  */
 8 // Protect from unauthorized access
 9 defined('FOF_INCLUDED') or die;
10 
11 /**
12  * Helper class with utilitarian functions concerning strings
13  *
14  * @package  FrameworkOnFramework
15  * @since    2.0
16  */
17 abstract class FOFStringUtils
18 {
19     /**
20      * Convert a string into a slug (alias), suitable for use in URLs. Please
21      * note that transliteration suupport is rudimentary at this stage.
22      *
23      * @param   string  $value  A string to convert to slug
24      *
25      * @return  string  The slug
26      */
27     public static function toSlug($value)
28     {
29         // Remove any '-' from the string they will be used as concatonater
30         $value = str_replace('-', ' ', $value);
31 
32         // Convert to ascii characters
33         $value = self::toASCII($value);
34 
35         // Lowercase and trim
36         $value = trim(strtolower($value));
37 
38         // Remove any duplicate whitespace, and ensure all characters are alphanumeric
39         $value = preg_replace(array('/\s+/', '/[^A-Za-z0-9\-_]/'), array('-', ''), $value);
40 
41         // Limit length
42         if (strlen($value) > 100)
43         {
44             $value = substr($value, 0, 100);
45         }
46 
47         return $value;
48     }
49 
50     /**
51      * Convert common norhern European languages' letters into plain ASCII. This
52      * is a rudimentary transliteration.
53      *
54      * @param   string  $value  The value to convert to ASCII
55      *
56      * @return  string  The converted string
57      */
58     public static function toASCII($value)
59     {
60         $string = htmlentities(utf8_decode($value), null, 'ISO-8859-1');
61         $string = preg_replace(
62             array('/ß/', '/&(..)lig;/', '/&([aouAOU])uml;/', '/&(.)[^;]*;/'), array('ss', "$1", "$1" . 'e', "$1"), $string
63         );
64 
65         return $string;
66     }
67 
68     /**
69      * Convert a string to a boolean.
70      *
71      * @param   string  $string  The string.
72      *
73      * @return  boolean  The converted string
74      */
75     public static function toBool($string)
76     {
77         $string = trim((string) $string);
78 
79         if ($string == 'true')
80         {
81             return true;
82         }
83 
84         if ($string == 'false')
85         {
86             return false;
87         }
88 
89         return (bool) $string;
90     }
91 }
92