1 <?php
2 /**
3 * Contains Text class for Alphred
4 *
5 * PHP version 5
6 *
7 * @package Alphred
8 * @copyright Shawn Patrick Rice 2014
9 * @license http://opensource.org/licenses/MIT MIT
10 * @version 1.0.0
11 * @author Shawn Patrick Rice <rice@shawnrice.org>
12 * @link http://www.github.com/shawnrice/alphred
13 * @link http://shawnrice.github.io/alphred
14 * @since File available since Release 1.0.0
15 *
16 */
17
18 namespace Alphred;
19
20
21 /**
22 * Methods to apply "filters" to text to transform them
23 *
24 * Right now, this is, well, "special sauce" and not major. So use it with your own precaution.
25 * It is possibly, going to change.
26 *
27 * @todo Brainstorm what other filters should be written
28 * @since 1.0.0
29 *
30 */
31 class Text {
32
33 /**
34 * Convert a string to Title Case
35 *
36 * @param string $string string to be converted
37 * @return string the string in Title Case
38 */
39 public static function title_case( $string ) {
40 // This needs to be improved upon. Basically, it needs to account for sentence ending punctuation.
41 // Words that are not capitalized. Well, articles, conjunctions, and prepositions.
42 $lower = [
43 'the','a','an','and','but','or','for','nor','aboard','about','above','across','after','against','along',
44 'amid','among','anti','around','as','at','before','behind','below','beneath','beside','besides','between',
45 'beyond','but','by','concerning','considering','despite','down','during','except','excepting','excluding',
46 'following','for','from','in','inside','into','like','minus','near','of','off','on','onto','opposite',
47 'outside','over','past','per','plus','regarding','round','save','since','than','through','to','toward',
48 'towards','under','underneath','unlike','until','up','upon','versus','via','with','within','without'
49 ];
50
51 $starting = [ '“', '‘' ]; // Add in things like upside-down exclamation points and question marks
52 $stop = [ '.', '!', '?' ];
53 $words = explode( ' ', $string );
54 foreach ( $words as $k => $w ) :
55
56 // Grab the first and last characters to check for punctuation later
57 $first = substr( $w, 0, 1 );
58 $last = substr( $w, -1, 1 );
59
60 // remove all punctuation (except hyphens and en- and em-dashes) from the string
61 $w = preg_replace( '/(?![-])\p{P}/u', '', $w );
62 if ( ! in_array( $w, $lower ) || strlen( $w ) > 3 ) {
63 $words[ $k ] = ucfirst( $w );
64 } else {
65 $words[ $k ] = lcfirst( $w );
66 };
67
68 // Add back in the punctuation if it was there.
69 if ( ctype_punct( $first ) ) { $words[ $k ] = $first . $words[ $k ]; }
70 if ( ctype_punct( $last ) ) { $words[ $k ] = $words[ $k ] . $last; }
71
72 endforeach;
73 $words[0] = ucfirst( $words[0] );
74 return implode( ' ', $words );
75 }
76
77 /**
78 * Converts a string to CamelCase
79 *
80 * @param string $string the string to be converted
81 * @return string the string in CamelCase
82 */
83 public static function camel_case( $string ) {
84 // converts spaces to camelcase
85 $words = explode( ' ', $string );
86 foreach ( $words as $k => $w ) :
87 $words[ $k ] = ucfirst( $w );
88 endforeach;
89 $words[0] = lcfirst( $words[0] );
90
91 return implode( '', $words );
92 }
93
94 /**
95 * Adds underscores to a string
96 *
97 * @since 1.0.0
98 *
99 * @param string $string a string to add underscores to
100 * @return string a string with underscores
101 */
102 public static function underscore( $string ) {
103 // converts spaces to underscores
104 return str_replace( ' ', '_', $string );
105 }
106
107 /**
108 * Hyphenates a string
109 *
110 * @since 1.0.0
111 *
112 * @param string $string the string to hyphenate
113 * @return string a hyphenated string
114 */
115 public static function hyphenate( $string ) {
116 // converts spaces to hyphens
117 return str_replace( ' ', '-', $string );
118 }
119
120 /**
121 * Adds commas to list (uses Oxford Commas, sorry, it's just proper)
122 *
123 * @since 1.0.0
124 *
125 * @param array $list an array to be pushed into a comma-ified string
126 * @param boolean whether or not there is a suffix
127 * @return string a string with commas
128 */
129 public static function add_commas_to_list( $list, $suffix = false ) {
130 // We want a string, so let's convert it to one with an Oxford Comma
131 $string = '';
132 $count = 1;
133 foreach ( $list as $unit => $value ) :
134 // Concatenate the string with the units
135 $string .= $suffix ? "{$value} {$unit}" : $value;
136 if ( $count == count( $list ) ) {
137 // All done, so return
138 return $string;
139 } else if ( ( ( $count + 1 ) == count( $list ) ) && ( 2 == count( $list ) ) ) {
140 // There are only two units, so no comma
141 $string .= ' and ';
142 } else if ( ( $count + 1 ) == count( $list ) ) {
143 // Last unit, so add in the "and"
144 $string .= ', and ';
145 } else {
146 // We have more units, so just add in the comma
147 $string .= ', ';
148 }
149 $count++;
150 endforeach;
151
152 // We return the string further up.
153
154 }
155
156 }