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.
67 lines
1.8 KiB
67 lines
1.8 KiB
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
/**
|
|
* @author Robert Strutts
|
|
* @copyright (c) 2026, Robert Strutts
|
|
* @license MIT
|
|
*/
|
|
namespace IOcornerstone\Framework\String;
|
|
|
|
use IOcornerstone\Framework\Exception\BadMethodCallException;
|
|
|
|
/**
|
|
* Description of StringFacade
|
|
*
|
|
* @author Robert Strutts
|
|
*/
|
|
class StringFacade
|
|
{
|
|
private static ?bool $mbDefault = null;
|
|
|
|
/**
|
|
* Set the default multibyte behavior.
|
|
* Pass `true` to force multibyte, `false` to force single-byte, or `null` to auto-detect.
|
|
*/
|
|
public static function setMultibyteDefault(?bool $flag): void {
|
|
self::$mbDefault = $flag;
|
|
}
|
|
|
|
protected static function isMultibyte($str) {
|
|
// Use override if set
|
|
if (self::$mbDefault !== null) {
|
|
return self::$mbDefault;
|
|
}
|
|
|
|
$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 getClass($str) {
|
|
return self::isMultibyte($str) ? 'mbStringFns' : 'StringFns';
|
|
}
|
|
|
|
public static function getFn($method, ...$args) {
|
|
if (empty($args)) {
|
|
throw new InvalidArgumentException("At least one argument is required for multibyte check.");
|
|
}
|
|
|
|
$class = "\\IOcornerstone\\Framework\\String\\" . self::getClass($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::getFn($method, ...$args);
|
|
}
|
|
}
|
|
|