[String] leverage Stringable from PHP 8
This commit is contained in:
parent
5428fef8cb
commit
53b0f63bc3
@ -34,6 +34,7 @@
|
||||
"symfony/polyfill-intl-normalizer": "~1.0",
|
||||
"symfony/polyfill-mbstring": "~1.0",
|
||||
"symfony/polyfill-php73": "^1.11",
|
||||
"symfony/polyfill-php80": "^1.15",
|
||||
"symfony/polyfill-uuid": "^1.15"
|
||||
},
|
||||
"replace": {
|
||||
|
@ -27,7 +27,7 @@ use Symfony\Component\String\Exception\RuntimeException;
|
||||
*
|
||||
* @throws ExceptionInterface
|
||||
*/
|
||||
abstract class AbstractString implements \JsonSerializable
|
||||
abstract class AbstractString implements \Stringable, \JsonSerializable
|
||||
{
|
||||
public const PREG_PATTERN_ORDER = PREG_PATTERN_ORDER;
|
||||
public const PREG_SET_ORDER = PREG_SET_ORDER;
|
||||
|
@ -16,7 +16,7 @@ namespace Symfony\Component\String;
|
||||
*
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class LazyString implements \JsonSerializable
|
||||
class LazyString implements \Stringable, \JsonSerializable
|
||||
{
|
||||
private $value;
|
||||
|
||||
@ -50,14 +50,14 @@ class LazyString implements \JsonSerializable
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object|string|int|float|bool $value A scalar or an object that implements the __toString() magic method
|
||||
* @param string|int|float|bool|\Stringable $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function fromStringable($value): self
|
||||
{
|
||||
if (!self::isStringable($value)) {
|
||||
throw new \TypeError(sprintf('Argument 1 passed to %s() must be a scalar or an object that implements the __toString() magic method, %s given.', __METHOD__, \is_object($value) ? \get_class($value) : \gettype($value)));
|
||||
throw new \TypeError(sprintf('Argument 1 passed to %s() must be a scalar or a stringable object, %s given.', __METHOD__, \is_object($value) ? \get_class($value) : \gettype($value)));
|
||||
}
|
||||
|
||||
if (\is_object($value)) {
|
||||
@ -75,7 +75,7 @@ class LazyString implements \JsonSerializable
|
||||
*/
|
||||
final public static function isStringable($value): bool
|
||||
{
|
||||
return \is_string($value) || $value instanceof self || (\is_object($value) ? \is_callable([$value, '__toString']) : is_scalar($value));
|
||||
return \is_string($value) || $value instanceof self || (\is_object($value) ? method_exists($value, '__toString') : is_scalar($value));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,6 +90,9 @@ class LazyString implements \JsonSerializable
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
if (\is_string($this->value)) {
|
||||
|
@ -19,7 +19,8 @@
|
||||
"php": "^7.2.5",
|
||||
"symfony/polyfill-intl-grapheme": "~1.0",
|
||||
"symfony/polyfill-intl-normalizer": "~1.0",
|
||||
"symfony/polyfill-mbstring": "~1.0"
|
||||
"symfony/polyfill-mbstring": "~1.0",
|
||||
"symfony/polyfill-php80": "~1.15"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/error-handler": "^4.4|^5.0",
|
||||
|
Reference in New Issue
Block a user