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 HandlesPotentiallyTranslatedString trait #47488

Merged
merged 3 commits into from
Jun 21, 2023
Merged
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
54 changes: 54 additions & 0 deletions src/Illuminate/Translation/CreatesPotentiallyTranslatedStrings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Illuminate\Translation;

trait CreatesPotentiallyTranslatedStrings
{
/**
* Create a pending potentially translated string.
*
* @param string $attribute
* @param string|null $message
* @return \Illuminate\Translation\PotentiallyTranslatedString
*/
protected function pendingPotentiallyTranslatedString($attribute, $message)
{
$destructor = $message === null
? fn ($message) => $this->messages[] = $message
: fn ($message) => $this->messages[$attribute] = $message;

return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
{
/**
* The callback to call when the object destructs.
*
* @var \Closure
*/
protected $destructor;

/**
* Create a new pending potentially translated string.
*
* @param string $message
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Closure $destructor
*/
public function __construct($message, $translator, $destructor)
{
parent::__construct($message, $translator);

$this->destructor = $destructor;
}

/**
* Handle the object's destruction.
*
* @return void
*/
public function __destruct()
{
($this->destructor)($this->toString());
}
};
}
}
52 changes: 3 additions & 49 deletions src/Illuminate/Validation/ClosureValidationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Illuminate\Contracts\Validation\Rule as RuleContract;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Translation\PotentiallyTranslatedString;
use Illuminate\Translation\CreatesPotentiallyTranslatedStrings;

class ClosureValidationRule implements RuleContract, ValidatorAwareRule
{
use CreatesPotentiallyTranslatedStrings;

/**
* The callback that validates the attribute.
*
Expand Down Expand Up @@ -89,52 +91,4 @@ public function setValidator($validator)

return $this;
}

/**
* Create a pending potentially translated string.
*
* @param string $attribute
* @param string|null $message
* @return \Illuminate\Translation\PotentiallyTranslatedString
*/
protected function pendingPotentiallyTranslatedString($attribute, $message)
{
$destructor = $message === null
? fn ($message) => $this->messages[] = $message
: fn ($message) => $this->messages[$attribute] = $message;

return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
{
/**
* The callback to call when the object destructs.
*
* @var \Closure
*/
protected $destructor;

/**
* Create a new pending potentially translated string.
*
* @param string $message
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Closure $destructor
*/
public function __construct($message, $translator, $destructor)
{
parent::__construct($message, $translator);

$this->destructor = $destructor;
}

/**
* Handle the object's destruction.
*
* @return void
*/
public function __destruct()
{
($this->destructor)($this->toString());
}
};
}
}
55 changes: 5 additions & 50 deletions src/Illuminate/Validation/InvokableValidationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Translation\PotentiallyTranslatedString;
use Illuminate\Translation\CreatesPotentiallyTranslatedStrings;

class InvokableValidationRule implements Rule, ValidatorAwareRule
{
use CreatesPotentiallyTranslatedStrings;

/**
* The invokable that validates the attribute.
*
Expand Down Expand Up @@ -67,7 +69,8 @@ protected function __construct(ValidationRule|InvokableRule $invokable)
public static function make($invokable)
{
if ($invokable->implicit ?? false) {
return new class($invokable) extends InvokableValidationRule implements ImplicitRule {};
return new class($invokable) extends InvokableValidationRule implements ImplicitRule {
};
}

return new InvokableValidationRule($invokable);
Expand Down Expand Up @@ -150,52 +153,4 @@ public function setValidator($validator)

return $this;
}

/**
* Create a pending potentially translated string.
*
* @param string $attribute
* @param string|null $message
* @return \Illuminate\Translation\PotentiallyTranslatedString
*/
protected function pendingPotentiallyTranslatedString($attribute, $message)
{
$destructor = $message === null
? fn ($message) => $this->messages[] = $message
: fn ($message) => $this->messages[$attribute] = $message;

return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
{
/**
* The callback to call when the object destructs.
*
* @var \Closure
*/
protected $destructor;

/**
* Create a new pending potentially translated string.
*
* @param string $message
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Closure $destructor
*/
public function __construct($message, $translator, $destructor)
{
parent::__construct($message, $translator);

$this->destructor = $destructor;
}

/**
* Handle the object's destruction.
*
* @return void
*/
public function __destruct()
{
($this->destructor)($this->toString());
}
};
}
}