Skip to content

Commit f30422f

Browse files
xurshudyanVolodya Khurshudyantaylorotwell
authored
[10.x] Add HandlesPotentiallyTranslatedString trait (#47488)
* Add HandlesPotentiallyTranslatedString trait * formatting * rename file --------- Co-authored-by: Volodya Khurshudyan <volodya.khurshudyan@softconstruct.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 27975ba commit f30422f

File tree

3 files changed

+62
-99
lines changed

3 files changed

+62
-99
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Illuminate\Translation;
4+
5+
trait CreatesPotentiallyTranslatedStrings
6+
{
7+
/**
8+
* Create a pending potentially translated string.
9+
*
10+
* @param string $attribute
11+
* @param string|null $message
12+
* @return \Illuminate\Translation\PotentiallyTranslatedString
13+
*/
14+
protected function pendingPotentiallyTranslatedString($attribute, $message)
15+
{
16+
$destructor = $message === null
17+
? fn ($message) => $this->messages[] = $message
18+
: fn ($message) => $this->messages[$attribute] = $message;
19+
20+
return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
21+
{
22+
/**
23+
* The callback to call when the object destructs.
24+
*
25+
* @var \Closure
26+
*/
27+
protected $destructor;
28+
29+
/**
30+
* Create a new pending potentially translated string.
31+
*
32+
* @param string $message
33+
* @param \Illuminate\Contracts\Translation\Translator $translator
34+
* @param \Closure $destructor
35+
*/
36+
public function __construct($message, $translator, $destructor)
37+
{
38+
parent::__construct($message, $translator);
39+
40+
$this->destructor = $destructor;
41+
}
42+
43+
/**
44+
* Handle the object's destruction.
45+
*
46+
* @return void
47+
*/
48+
public function __destruct()
49+
{
50+
($this->destructor)($this->toString());
51+
}
52+
};
53+
}
54+
}

src/Illuminate/Validation/ClosureValidationRule.php

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Illuminate\Contracts\Validation\Rule as RuleContract;
66
use Illuminate\Contracts\Validation\ValidatorAwareRule;
7-
use Illuminate\Translation\PotentiallyTranslatedString;
7+
use Illuminate\Translation\CreatesPotentiallyTranslatedStrings;
88

99
class ClosureValidationRule implements RuleContract, ValidatorAwareRule
1010
{
11+
use CreatesPotentiallyTranslatedStrings;
12+
1113
/**
1214
* The callback that validates the attribute.
1315
*
@@ -89,52 +91,4 @@ public function setValidator($validator)
8991

9092
return $this;
9193
}
92-
93-
/**
94-
* Create a pending potentially translated string.
95-
*
96-
* @param string $attribute
97-
* @param string|null $message
98-
* @return \Illuminate\Translation\PotentiallyTranslatedString
99-
*/
100-
protected function pendingPotentiallyTranslatedString($attribute, $message)
101-
{
102-
$destructor = $message === null
103-
? fn ($message) => $this->messages[] = $message
104-
: fn ($message) => $this->messages[$attribute] = $message;
105-
106-
return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
107-
{
108-
/**
109-
* The callback to call when the object destructs.
110-
*
111-
* @var \Closure
112-
*/
113-
protected $destructor;
114-
115-
/**
116-
* Create a new pending potentially translated string.
117-
*
118-
* @param string $message
119-
* @param \Illuminate\Contracts\Translation\Translator $translator
120-
* @param \Closure $destructor
121-
*/
122-
public function __construct($message, $translator, $destructor)
123-
{
124-
parent::__construct($message, $translator);
125-
126-
$this->destructor = $destructor;
127-
}
128-
129-
/**
130-
* Handle the object's destruction.
131-
*
132-
* @return void
133-
*/
134-
public function __destruct()
135-
{
136-
($this->destructor)($this->toString());
137-
}
138-
};
139-
}
14094
}

src/Illuminate/Validation/InvokableValidationRule.php

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use Illuminate\Contracts\Validation\Rule;
99
use Illuminate\Contracts\Validation\ValidationRule;
1010
use Illuminate\Contracts\Validation\ValidatorAwareRule;
11-
use Illuminate\Translation\PotentiallyTranslatedString;
11+
use Illuminate\Translation\CreatesPotentiallyTranslatedStrings;
1212

1313
class InvokableValidationRule implements Rule, ValidatorAwareRule
1414
{
15+
use CreatesPotentiallyTranslatedStrings;
16+
1517
/**
1618
* The invokable that validates the attribute.
1719
*
@@ -67,7 +69,8 @@ protected function __construct(ValidationRule|InvokableRule $invokable)
6769
public static function make($invokable)
6870
{
6971
if ($invokable->implicit ?? false) {
70-
return new class($invokable) extends InvokableValidationRule implements ImplicitRule {};
72+
return new class($invokable) extends InvokableValidationRule implements ImplicitRule {
73+
};
7174
}
7275

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

151154
return $this;
152155
}
153-
154-
/**
155-
* Create a pending potentially translated string.
156-
*
157-
* @param string $attribute
158-
* @param string|null $message
159-
* @return \Illuminate\Translation\PotentiallyTranslatedString
160-
*/
161-
protected function pendingPotentiallyTranslatedString($attribute, $message)
162-
{
163-
$destructor = $message === null
164-
? fn ($message) => $this->messages[] = $message
165-
: fn ($message) => $this->messages[$attribute] = $message;
166-
167-
return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
168-
{
169-
/**
170-
* The callback to call when the object destructs.
171-
*
172-
* @var \Closure
173-
*/
174-
protected $destructor;
175-
176-
/**
177-
* Create a new pending potentially translated string.
178-
*
179-
* @param string $message
180-
* @param \Illuminate\Contracts\Translation\Translator $translator
181-
* @param \Closure $destructor
182-
*/
183-
public function __construct($message, $translator, $destructor)
184-
{
185-
parent::__construct($message, $translator);
186-
187-
$this->destructor = $destructor;
188-
}
189-
190-
/**
191-
* Handle the object's destruction.
192-
*
193-
* @return void
194-
*/
195-
public function __destruct()
196-
{
197-
($this->destructor)($this->toString());
198-
}
199-
};
200-
}
201156
}

0 commit comments

Comments
 (0)