Completed
Push — master ( 6cab57...287bd7 )
by Sam
02:34
created

src/Text.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file contains only the Text class
4
 *
5
 * @package Tabulate
6
 */
7
8
namespace WordPress\Tabulate;
9
10
/**
11
 * The Text class contains static methods for working with text
12
 */
13
class Text {
14
15
	/**
16
	 * Turn a spaced or underscored string to camelcase (with no spaces or underscores).
17
	 *
18
	 * @param string $str The string to format.
19
	 * @return string
20
	 */
21
	public static function camelcase( $str ) {
22
		return str_replace( ' ', '', ucwords( str_replace( '_', ' ', $str ) ) );
23
	}
24
25
	/**
26
	 * Apply the titlecase filter to a string: removing underscores, uppercasing
27
	 * initial letters, and performing a few common (and not-so-common) word
28
	 * replacements such as initialisms and punctuation.
29
	 *
30
	 * @param string|array   $value  The underscored and lowercase string to be titlecased, or an array of such strings.
31
	 * @param 'html'|'latex' $format The desired output format.
0 ignored issues
show
The doc-type 'html'|'latex' could not be parsed: Unknown type name "'html'" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
32
	 *
33
	 * @return string A properly-typeset title.
34
	 */
35
	public static function titlecase( $value, $format = 'html' ) {
36
37
		/**
38
		 * The mapping of words (and initialisms, etc.) to their titlecased
39
		 * counterparts for HTML output.
40
		 */
41
		$html_replacements = array(
42
			'id' => 'ID',
43
			'cant' => "can't",
44
			'in' => 'in',
45
			'at' => 'at',
46
			'of' => 'of',
47
			'for' => 'for',
48
			'sql' => 'SQL',
49
			'todays' => "Today's",
50
		);
51
52
		/**
53
		 * The mapping of words (and initialisms, etc.) to their titlecased
54
		 * counterparts for LaTeX output.
55
		 */
56
		$latex_replacements = array(
57
			'cant' => "can't",
58
		);
59
60
		/**
61
		 * Marshall the correct replacement strings.
62
		 */
63
		if ( 'latex' === $format ) {
64
			$replacements = array_merge( $html_replacements, $latex_replacements );
65
		} else {
66
			$replacements = $html_replacements;
67
		}
68
69
		/**
70
		 * Recurse if neccessary
71
		 */
72
		if ( is_array( $value ) ) {
73
			return array_map( array( self, 'titlecase' ), $value );
74
		} else {
75
			$out = ucwords( preg_replace( '|_|', ' ', $value ) );
76
			foreach ( $replacements as $search => $replacement ) {
77
				$out = preg_replace( "|\b$search\b|i", $replacement, $out );
78
			}
79
			return trim( $out );
80
		}
81
	}
82
83
	/**
84
	 * Format a MySQL-format date according to WP's preference.
85
	 *
86
	 * @param string $date The date string to format.
87
	 * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
88
	 */
89
	public static function wp_date_format( $date ) {
90
		return mysql2date( get_option( 'date_format' ), $date );
91
	}
92
93
	/**
94
	 * Format a MySQL-format time according to WP's preference.
95
	 *
96
	 * @param string $time The time string to format.
97
	 * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
98
	 */
99
	public static function wp_time_format( $time ) {
100
		return mysql2date( get_option( 'time_format' ), $time );
101
	}
102
}
103