PHP 8.4+ Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
CodeHydrater/src/classes/strings/string_facade.php

60 lines
1.7 KiB

<?php
declare(strict_types = 1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace CodeHydrater\strings;
class string_facade {
private static ?bool $mb_default = null;
/**
* Set the default multibyte behavior.
* Pass `true` to force multibyte, `false` to force single-byte, or `null` to auto-detect.
*/
public static function set_multibyte_default(?bool $flag): void {
self::$mb_default = $flag;
}
protected static function is_multibyte($str) {
// Use override if set
if (self::$mb_default !== null) {
return self::$mb_default;
}
$enabled = extension_loaded('mbstring');
if ($enabled === false) {
return false;
}
return mb_detect_encoding($str, mb_detect_order(), true) !== false &&
preg_match('/[^\x00-\x7F]/', $str);
}
protected static function get_class($str) {
return self::is_multibyte($str) ? 'mb_string_fns' : 'string_fns';
}
public static function get_fn($method, ...$args) {
if (empty($args)) {
throw new InvalidArgumentException("At least one argument is required for multibyte check.");
}
$class = "\\CodeHydrater\\strings\\" . self::get_class($args[0]);
if (!method_exists($class, $method)) {
throw new BadMethodCallException("Method $method does not exist in class $class.");
}
return call_user_func_array([$class, $method], $args);
}
// Optional: Static passthrough
public static function __callStatic($method, $args) {
return self::get_fn($method, ...$args);
}
}