index/src/XStringTrait.php
2023-11-09 12:58:04 +00:00

66 lines
1.6 KiB
PHP

<?php
// XStringTrait.php
// Created: 2021-06-22
// Updated: 2023-11-09
namespace Index;
use BadMethodCallException;
/**
* Provides method implementations that are common between all string types.
* @internal
* @deprecated Will be removed along with IString, AString and WString.
*/
trait XStringTrait {
/**
* Gets the length of the string.
*
* For compliance with the Countable interface.
*
* @see https://www.php.net/manual/en/class.countable.php
* @return int Length of the string.
*/
public function count(): int {
return $this->getLength();
}
/**
* Necessary to comply with the ArrayAccess interface.
*
* @internal
*/
public function offsetSet(mixed $offset, mixed $value): void {
throw new BadMethodCallException('Strings are immutable.');
}
/**
* Necessary to comply with the ArrayAccess interface.
*
* @internal
*/
public function offsetUnset(mixed $offset): void {
throw new BadMethodCallException('Strings are immutable.');
}
public function toBool(): bool {
return boolval((string)$this);
}
public function toInt(int $base = 10): int {
return $base === 10 ? (int)(string)$this : intval((string)$this, $base);
}
public function toFloat(): float {
return (float)(string)$this;
}
public function escape(
int $flags = ENT_COMPAT | ENT_HTML5,
?string $encoding = null,
bool $doubleEncoding = true
): IString {
return new AString(XString::escape($this, $flags, $encoding, $doubleEncoding));
}
}