Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add ArrayAccess to Stringable #46279

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Support;

use ArrayAccess;
use Closure;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Traits\Conditionable;
Expand All @@ -10,7 +11,7 @@
use JsonSerializable;
use Symfony\Component\VarDumper\VarDumper;

class Stringable implements JsonSerializable
class Stringable implements JsonSerializable, ArrayAccess
{
use Conditionable, Macroable, Tappable;

Expand Down Expand Up @@ -396,7 +397,6 @@ public function lower()
/**
* Convert GitHub flavored Markdown into HTML.
*
* @param array $options
driesvints marked this conversation as resolved.
Show resolved Hide resolved
* @return static
*/
public function markdown(array $options = [])
Expand All @@ -407,7 +407,6 @@ public function markdown(array $options = [])
/**
* Convert inline Markdown into HTML.
*
* @param array $options
* @return static
*/
public function inlineMarkdown(array $options = [])
Expand Down Expand Up @@ -512,7 +511,6 @@ public function parseCallback($default = null)
/**
* Call the given callback and return a new string.
*
* @param callable $callback
* @return static
*/
public function pipe(callable $callback)
Expand Down Expand Up @@ -578,7 +576,6 @@ public function reverse()
/**
* Repeat the string.
*
* @param int $times
* @return static
*/
public function repeat(int $times)
Expand Down Expand Up @@ -821,7 +818,6 @@ public function substrReplace($replace, $offset = 0, $length = null)
/**
* Swap multiple keywords in a string with other keywords.
*
* @param array $map
* @return static
*/
public function swap(array $map)
Expand Down Expand Up @@ -1197,8 +1193,6 @@ public function toDate($format = null, $tz = null)

/**
* Convert the object to a string when JSON encoded.
*
* @return string
*/
public function jsonSerialize(): string
{
Expand All @@ -1225,4 +1219,24 @@ public function __toString()
{
return (string) $this->value;
}

public function offsetExists(mixed $offset): bool
{
return isset($this->value[$offset]);
}

public function offsetGet(mixed $offset): string
{
return $this->value[$offset];
}

public function offsetSet(mixed $offset, mixed $value): void
{
$this->value[$offset] = $value;
}

public function offsetUnset(mixed $offset): void
{
unset($this->value[$offset]);
}
}
9 changes: 9 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1179,4 +1179,13 @@ public function testToDateThrowsException()

$this->stringable('not a date')->toDate();
}

public function testArrayAccess()
{
$str = $this->stringable('my string');
$this->assertSame('m', $str[0]);
$this->assertSame('s', $str[4]);
driesvints marked this conversation as resolved.
Show resolved Hide resolved
$this->assertTrue(isset($str[2]));
$this->assertFalse(isset($str[10]));
}
}