Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 11 additions & 5 deletions src/Modifiers/Modify.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ public function fetch()
*/
public function __toString()
{
if (! is_string($this->value) && ! method_exists($this->value, '__toString')) {
throw new ModifierException(
'Attempted to access modified value as a string, but encountered ['.get_class($this->value).']'
);
$value = $this->value;

if ($value === null || is_scalar($value)) {
return (string) $value;
}

if (is_object($value) && method_exists($value, '__toString')) {
return (string) $value;
}

return (string) $this->value;
throw new ModifierException(
'Attempted to access modified value as a string, but encountered ['.get_debug_type($value).']'
);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Modifiers/FluentModifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Tests\Modifiers;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Fields\Values;
use Statamic\Modifiers\Modifier;
use Statamic\Modifiers\ModifierException;
use Statamic\Modifiers\Modify;
use Tests\TestCase;

Expand Down Expand Up @@ -38,6 +40,42 @@ public function passing_a_values_instance_into_it_will_not_convert_it_to_an_arra
$this->assertSame($values, $result);
}

#[Test]
#[DataProvider('scalarValues')]
public function it_casts_scalar_and_null_values_to_string($value, $expected)
{
$this->assertSame($expected, (string) Modify::value($value));
}

public static function scalarValues()
{
return [
'integer zero' => [0, '0'],
'positive integer' => [42, '42'],
'negative integer' => [-7, '-7'],
'float' => [1.5, '1.5'],
'true' => [true, '1'],
'false' => [false, ''],
'null' => [null, ''],
'string' => ['hello', 'hello'],
];
}

#[Test]
public function it_casts_a_modified_integer_chain_result_to_string()
{
$this->assertSame('5', (string) Modify::value(0)->add(5));
}

#[Test]
public function it_throws_when_casting_an_array_to_string()
{
$this->expectException(ModifierException::class);
$this->expectExceptionMessage('Attempted to access modified value as a string, but encountered [array]');

(string) Modify::value(['foo' => 'bar']);
}

#[Test]
public function values_instances_get_converted_to_an_array_when_passing_to_a_modifier()
{
Expand Down
Loading