index/src/XStringTrait.php

65 lines
1.5 KiB
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
// XStringTrait.php
// Created: 2021-06-22
// Updated: 2022-02-27
namespace Index;
use BadMethodCallException;
/**
* Provides method implementations that are common between all string types.
* @internal
*/
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 XString::toBool($this);
}
public function toInt(int $base = 10): int {
return XString::toInt($this, $base);
}
public function toFloat(): float {
return XString::toFloat($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));
}
}