Skip to content

Commit

Permalink
feature #36059 [String] leverage Stringable from PHP 8 (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.1-dev branch.

Discussion
----------

[String] leverage Stringable from PHP 8

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Commits
-------

53b0f63 [String] leverage Stringable from PHP 8
  • Loading branch information
nicolas-grekas committed Mar 13, 2020
2 parents 5428fef + 53b0f63 commit 6f206c5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/String/AbstractString.php
Expand Up @@ -27,7 +27,7 @@
*
* @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;
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/String/LazyString.php
Expand Up @@ -16,7 +16,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class LazyString implements \JsonSerializable
class LazyString implements \Stringable, \JsonSerializable
{
private $value;

Expand Down Expand Up @@ -50,14 +50,14 @@ public static function fromCallable($callback, ...$arguments): self
}

/**
* @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)) {
Expand All @@ -75,7 +75,7 @@ public static function fromStringable($value): self
*/
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));
}

/**
Expand All @@ -90,6 +90,9 @@ final public static function resolve($value): string
return $value;
}

/**
* @return string
*/
public function __toString()
{
if (\is_string($this->value)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/String/composer.json
Expand Up @@ -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",
Expand Down

0 comments on commit 6f206c5

Please sign in to comment.