Skip to content

Commit db46bc5

Browse files
authored
Adapt other components to support php8. (#3673)
1 parent 6c60996 commit db46bc5

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

src/Concerns/FormatsMessages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function getDisplayableValue(string $attribute, $value)
9797
return $line;
9898
}
9999

100-
return $value;
100+
return (string) $value;
101101
}
102102

103103
/**
@@ -291,7 +291,7 @@ protected function replaceInputPlaceholder(string $message, string $attribute):
291291
$actualValue = $this->getValue($attribute);
292292

293293
if (is_scalar($actualValue) || is_null($actualValue)) {
294-
$message = str_replace(':input', $actualValue, $message);
294+
$message = str_replace(':input', $this->getDisplayableValue($attribute, $actualValue), $message);
295295
}
296296

297297
return $message;

src/Concerns/ReplacesAttributes.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected function replaceGt(string $message, string $attribute, string $rule, a
164164
return str_replace(':value', $parameters[0], $message);
165165
}
166166

167-
return str_replace(':value', $this->getSize($attribute, $value), $message);
167+
return str_replace(':value', (string) $this->getSize($attribute, $value), $message);
168168
}
169169

170170
/**
@@ -176,7 +176,7 @@ protected function replaceLt(string $message, string $attribute, string $rule, a
176176
return str_replace(':value', $parameters[0], $message);
177177
}
178178

179-
return str_replace(':value', $this->getSize($attribute, $value), $message);
179+
return str_replace(':value', (string) $this->getSize($attribute, $value), $message);
180180
}
181181

182182
/**
@@ -188,7 +188,7 @@ protected function replaceGte(string $message, string $attribute, string $rule,
188188
return str_replace(':value', $parameters[0], $message);
189189
}
190190

191-
return str_replace(':value', $this->getSize($attribute, $value), $message);
191+
return str_replace(':value', (string) $this->getSize($attribute, $value), $message);
192192
}
193193

194194
/**
@@ -200,7 +200,7 @@ protected function replaceLte(string $message, string $attribute, string $rule,
200200
return str_replace(':value', $parameters[0], $message);
201201
}
202202

203-
return str_replace(':value', $this->getSize($attribute, $value), $message);
203+
return str_replace(':value', (string) $this->getSize($attribute, $value), $message);
204204
}
205205

206206
/**

src/Concerns/ValidatesAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ protected function allFailingRequired(array $attributes): bool
14761476
* Get the size of an attribute.
14771477
*
14781478
* @param mixed $value
1479-
* @return mixed
1479+
* @return float|int
14801480
*/
14811481
protected function getSize(string $attribute, $value)
14821482
{

src/Rules/In.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(array $values)
4141
public function __toString(): string
4242
{
4343
$values = array_map(function ($value) {
44-
return '"' . str_replace('"', '""', $value) . '"';
44+
return '"' . str_replace('"', '""', (string) $value) . '"';
4545
}, $this->values);
4646

4747
return $this->rule . ':' . implode(',', $values);

src/Rules/NotIn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(array $values)
3939
public function __toString(): string
4040
{
4141
$values = array_map(function ($value) {
42-
return '"' . str_replace('"', '""', $value) . '"';
42+
return '"' . str_replace('"', '""', (string) $value) . '"';
4343
}, $this->values);
4444

4545
return $this->rule . ':' . implode(',', $values);

tests/Cases/ValidationValidatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,6 +4599,27 @@ public function testValidateAfter()
45994599
$this->assertTrue($v->passes());
46004600
}
46014601

4602+
public function testInputIsReplacedByItsDisplayableValue()
4603+
{
4604+
$frameworks = [
4605+
1 => 'Laravel',
4606+
2 => 'Symfony',
4607+
3 => 'Rails',
4608+
];
4609+
4610+
$trans = $this->getIlluminateArrayTranslator();
4611+
$trans->addLines(['validation.framework_php' => ':input is not a valid PHP Framework'], 'en');
4612+
4613+
$v = new Validator($trans, ['framework' => 3], ['framework' => 'framework_php']);
4614+
$v->addExtension('framework_php', function ($attribute, $value, $parameters, $validator) {
4615+
return in_array($value, [1, 2]);
4616+
});
4617+
$v->addCustomValues(['framework' => $frameworks]);
4618+
4619+
$this->assertFalse($v->passes());
4620+
$this->assertSame('Rails is not a valid PHP Framework', $v->messages()->first('framework'));
4621+
}
4622+
46024623
public function getIlluminateArrayTranslator()
46034624
{
46044625
return new Translator(

0 commit comments

Comments
 (0)