Ubiquity  2.0.0
php rapid development framework
UString.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils;
4 
10 class UString {
11 
12  public static function startswith($hay, $needle) {
13  return \substr($hay, 0, strlen($needle)) === $needle;
14  }
15 
16  public static function endswith($hay, $needle) {
17  return \substr($hay, -strlen($needle)) === $needle;
18  }
19 
20  public static function getBooleanStr($value) {
21  $ret="false";
22  if ($value)
23  $ret="true";
24  return $ret;
25  }
26 
27  public static function isNull($s) {
28  return (!isset($s) || NULL === $s || "" === $s);
29  }
30 
31  public static function isNotNull($s) {
32  return (isset($s) && NULL !== $s && "" !== $s);
33  }
34 
35  public static function isBooleanTrue($s) {
36  return $s === true || $s === "true" || $s === 1 || $s === "1";
37  }
38 
39  public static function isBooleanFalse($s) {
40  return $s === false || $s === "false" || $s === 0 || $s === "0";
41  }
42 
43  public static function isBoolean($value) {
44  return \is_bool($value);
45  }
46 
47  public static function pluralize($count, $caption, $plural=NULL) {
48  if ($plural == NULL) {
49  $pluralChar="s";
50  if (self::endswith($caption, "au")) {
51  $pluralChar="x";
52  }
53  $plural=$caption . $pluralChar;
54  }
55  switch($count) {
56  case 0:
57  $result="aucun " . $caption;
58  break;
59  case 1:
60  $result=$count . " " . $caption;
61  break;
62  default:
63  $result=$count . " " . $plural;
64  break;
65  }
66  return $result;
67  }
68 
69  public static function firstReplace($haystack, $needle, $replace) {
70  $newstring=$haystack;
71  $pos=strpos($haystack, $needle);
72  if ($pos !== false) {
73  $newstring=\substr_replace($haystack, $replace, $pos, strlen($needle));
74  }
75  return $newstring;
76  }
77 
78  public static function replaceArray($haystack, $needle, $replaceArray) {
79  $result=$haystack;
80  foreach ( $replaceArray as $replace ) {
81  $result=self::firstReplace($result, $needle, $replace);
82  }
83  return $result;
84  }
85 }
86 
static isBooleanTrue($s)
Definition: UString.php:35
$replace
Definition: traits.php:14
static pluralize($count, $caption, $plural=NULL)
Definition: UString.php:47
static getBooleanStr($value)
Definition: UString.php:20
static isBoolean($value)
Definition: UString.php:43
static firstReplace($haystack, $needle, $replace)
Definition: UString.php:69
static replaceArray($haystack, $needle, $replaceArray)
Definition: UString.php:78
String utilities.
Definition: UString.php:10
static startswith($hay, $needle)
Definition: UString.php:12
static isNotNull($s)
Definition: UString.php:31
static isNull($s)
Definition: UString.php:27
static isBooleanFalse($s)
Definition: UString.php:39
static endswith($hay, $needle)
Definition: UString.php:16