Skip to content

Commit 648fc0e

Browse files
authored
refactor: simplify Message and MessageCollection (#15)
1 parent 50db9da commit 648fc0e

18 files changed

Lines changed: 288 additions & 217 deletions

src/Format/Reader/FormatPHPReader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class FormatPHPReader implements ReaderInterface
4848
*/
4949
public function __invoke(Config $config, array $data, LocaleInterface $localeResolved): MessageCollection
5050
{
51-
$messages = new MessageCollection($config);
51+
$messages = new MessageCollection();
5252

5353
/**
5454
* @var string $messageId
@@ -57,7 +57,7 @@ public function __invoke(Config $config, array $data, LocaleInterface $localeRes
5757
foreach ($data as $messageId => $message) {
5858
$this->validateShape($messageId, $message);
5959

60-
$messages[] = new Message($localeResolved, $messageId, $message['defaultMessage']);
60+
$messages[] = new Message($messageId, $message['defaultMessage']);
6161
}
6262

6363
return $messages;

src/Format/Reader/SimpleReader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ class SimpleReader implements ReaderInterface
4848
*/
4949
public function __invoke(Config $config, array $data, LocaleInterface $localeResolved): MessageCollection
5050
{
51-
$messages = new MessageCollection($config);
51+
$messages = new MessageCollection();
5252

5353
foreach ($data as $messageId => $message) {
5454
$this->validateShape($messageId, $message);
5555
assert(is_string($messageId));
5656
assert(is_string($message));
5757

58-
$messages[$messageId] = new Message($localeResolved, $messageId, $message);
58+
$messages[$messageId] = new Message($messageId, $message);
5959
}
6060

6161
return $messages;

src/Format/Reader/SmartlingReader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SmartlingReader implements ReaderInterface
4848
*/
4949
public function __invoke(Config $config, array $data, LocaleInterface $localeResolved): MessageCollection
5050
{
51-
$messages = new MessageCollection($config);
51+
$messages = new MessageCollection();
5252

5353
unset($data['smartling']);
5454

@@ -59,7 +59,7 @@ public function __invoke(Config $config, array $data, LocaleInterface $localeRes
5959
foreach ($data as $messageId => $message) {
6060
$this->validateShape($messageId, $message);
6161

62-
$messages[] = new Message($localeResolved, $messageId, $message['message']);
62+
$messages[] = new Message($messageId, $message['message']);
6363
}
6464

6565
return $messages;

src/FormatPHP.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@
2222

2323
namespace FormatPHP;
2424

25+
use FormatPHP\Exception\InvalidArgumentException;
26+
use FormatPHP\Exception\UnableToGenerateMessageIdException;
2527
use FormatPHP\Intl\MessageFormat;
28+
use FormatPHP\Util\MessageCleaner;
29+
use FormatPHP\Util\MessageRetriever;
30+
31+
use function is_int;
2632

2733
/**
2834
* FormatPHP internationalization and localization
2935
*/
3036
class FormatPHP implements FormatterInterface
3137
{
38+
use MessageCleaner;
39+
use MessageRetriever;
40+
41+
private ConfigInterface $config;
3242
private MessageCollection $messages;
3343
private MessageFormat $messageFormat;
3444

@@ -39,6 +49,7 @@ public function __construct(
3949
ConfigInterface $config,
4050
MessageCollection $messages
4151
) {
52+
$this->config = $config;
4253
$this->messages = $messages;
4354
$this->messageFormat = new MessageFormat($config->getLocale());
4455
}
@@ -51,12 +62,28 @@ public function __construct(
5162
*/
5263
public function formatMessage(array $descriptor, array $values = []): string
5364
{
54-
$messagePattern = $this->messages->getMessageByDescriptor(new Descriptor(
55-
$descriptor['id'] ?? null,
56-
$descriptor['defaultMessage'] ?? null,
57-
$descriptor['description'] ?? null,
58-
));
65+
try {
66+
$messagePattern = $this->getMessageForDescriptor(
67+
$this->messages,
68+
new Descriptor(
69+
$descriptor['id'] ?? null,
70+
$descriptor['defaultMessage'] ?? null,
71+
$descriptor['description'] ?? null,
72+
),
73+
);
74+
} catch (UnableToGenerateMessageIdException $exception) {
75+
throw new InvalidArgumentException(
76+
'The message descriptor must have an ID or default message',
77+
is_int($exception->getCode()) ? $exception->getCode() : 0,
78+
$exception,
79+
);
80+
}
5981

60-
return $this->messageFormat->format($messagePattern, $values);
82+
return $this->messageFormat->format($this->cleanMessage($messagePattern), $values);
83+
}
84+
85+
protected function getConfig(): ConfigInterface
86+
{
87+
return $this->config;
6188
}
6289
}

src/Message.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,18 @@
2222

2323
namespace FormatPHP;
2424

25-
use FormatPHP\Intl\LocaleInterface;
26-
2725
/**
2826
* FormatPHP translation message
2927
*/
3028
class Message implements MessageInterface
3129
{
3230
private string $id;
33-
private LocaleInterface $locale;
3431
private string $message;
3532

3633
public function __construct(
37-
LocaleInterface $locale,
3834
string $id,
3935
string $message
4036
) {
41-
$this->locale = $locale;
4237
$this->id = $id;
4338
$this->message = $message;
4439
}
@@ -48,11 +43,6 @@ public function getId(): string
4843
return $this->id;
4944
}
5045

51-
public function getLocale(): LocaleInterface
52-
{
53-
return $this->locale;
54-
}
55-
5646
public function getMessage(): string
5747
{
5848
return $this->message;

src/MessageCollection.php

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,9 @@
2222

2323
namespace FormatPHP;
2424

25-
use FormatPHP\Exception\InvalidArgumentException;
26-
use FormatPHP\Exception\MessageNotFoundException;
27-
use FormatPHP\Exception\UnableToGenerateMessageIdException;
28-
use FormatPHP\Extractor\IdInterpolator;
2925
use IteratorAggregate;
3026
use Ramsey\Collection\AbstractCollection;
31-
32-
use function preg_replace;
33-
use function sprintf;
34-
use function trim;
27+
use Ramsey\Collection\Exception\InvalidArgumentException;
3528

3629
/**
3730
* FormatPHP collection of Message instances
@@ -41,25 +34,13 @@
4134
*/
4235
final class MessageCollection extends AbstractCollection implements IteratorAggregate
4336
{
44-
private ConfigInterface $config;
45-
46-
/**
47-
* @param array<array-key, MessageInterface> $data
48-
*/
49-
public function __construct(ConfigInterface $config, array $data = [])
50-
{
51-
parent::__construct($data);
52-
53-
$this->config = $config;
54-
}
55-
5637
public function getType(): string
5738
{
5839
return MessageInterface::class;
5940
}
6041

6142
/**
62-
* @throws \Ramsey\Collection\Exception\InvalidArgumentException
43+
* @throws InvalidArgumentException
6344
*
6445
* @inheritDoc
6546
*/
@@ -71,74 +52,4 @@ public function offsetSet($offset, $value): void
7152

7253
parent::offsetSet($offset, $value);
7354
}
74-
75-
/**
76-
* Looks up and returns a message for the given ID and locale
77-
*
78-
* @throws MessageNotFoundException
79-
*/
80-
public function getMessageById(string $id): string
81-
{
82-
return $this->cleanMessage($this->lookupMessage($id)->getMessage());
83-
}
84-
85-
/**
86-
* Looks up and returns a message for the given Descriptor and locale
87-
*
88-
* @throws InvalidArgumentException
89-
*/
90-
public function getMessageByDescriptor(DescriptorInterface $descriptor): string
91-
{
92-
$messageId = $this->buildMessageId($descriptor);
93-
94-
try {
95-
return $this->getMessageById($messageId);
96-
} catch (MessageNotFoundException $exception) {
97-
if ($descriptor->getDefaultMessage() !== null) {
98-
return $this->cleanMessage((string) $descriptor->getDefaultMessage());
99-
}
100-
}
101-
102-
return $messageId;
103-
}
104-
105-
/**
106-
* @throws MessageNotFoundException
107-
*/
108-
private function lookupMessage(string $messageId): MessageInterface
109-
{
110-
$message = $this[$messageId] ?? null;
111-
112-
if ($message === null) {
113-
throw new MessageNotFoundException(sprintf(
114-
'Unable to find message with ID "%s" for locale "%s"',
115-
$messageId,
116-
$this->config->getLocale()->toString(),
117-
));
118-
}
119-
120-
return $message;
121-
}
122-
123-
/**
124-
* @throws InvalidArgumentException
125-
*/
126-
private function buildMessageId(DescriptorInterface $descriptor): string
127-
{
128-
try {
129-
$messageId = (new IdInterpolator())->generateId(
130-
$descriptor,
131-
$this->config->getIdInterpolatorPattern(),
132-
);
133-
} catch (UnableToGenerateMessageIdException $exception) {
134-
$messageId = '';
135-
}
136-
137-
return $messageId;
138-
}
139-
140-
private function cleanMessage(string $message): string
141-
{
142-
return trim((string) preg_replace('/\n\s*/', ' ', $message));
143-
}
14455
}

src/MessageInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
namespace FormatPHP;
2424

25-
use FormatPHP\Intl\LocaleInterface;
26-
2725
/**
2826
* FormatPHP translation message
2927
*/
@@ -34,11 +32,6 @@ interface MessageInterface
3432
*/
3533
public function getId(): string;
3634

37-
/**
38-
* Returns the message locale
39-
*/
40-
public function getLocale(): LocaleInterface;
41-
4235
/**
4336
* Returns the string translation message
4437
*/

src/Util/DescriptorIdBuilder.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* This file is part of skillshare/formatphp
5+
*
6+
* skillshare/formatphp is open source software: you can distribute
7+
* it and/or modify it under the terms of the MIT License
8+
* (the "License"). You may not use this file except in
9+
* compliance with the License.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
* implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*
17+
* @copyright Copyright (c) Skillshare, Inc. <https://www.skillshare.com>
18+
* @license https://opensource.org/licenses/MIT MIT License
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace FormatPHP\Util;
24+
25+
use FormatPHP\ConfigInterface;
26+
use FormatPHP\DescriptorInterface;
27+
use FormatPHP\Exception\InvalidArgumentException;
28+
use FormatPHP\Exception\UnableToGenerateMessageIdException;
29+
use FormatPHP\Extractor\IdInterpolator;
30+
31+
/**
32+
* Provides tools for building message IDs for descriptors
33+
*/
34+
trait DescriptorIdBuilder
35+
{
36+
abstract protected function getConfig(): ConfigInterface;
37+
38+
/**
39+
* Builds a message ID for the given descriptor or returns its existing ID
40+
*
41+
* @throws InvalidArgumentException
42+
* @throws UnableToGenerateMessageIdException
43+
*/
44+
private function buildMessageId(DescriptorInterface $descriptor): string
45+
{
46+
return (new IdInterpolator())->generateId(
47+
$descriptor,
48+
$this->getConfig()->getIdInterpolatorPattern(),
49+
);
50+
}
51+
}

src/Util/MessageCleaner.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* This file is part of skillshare/formatphp
5+
*
6+
* skillshare/formatphp is open source software: you can distribute
7+
* it and/or modify it under the terms of the MIT License
8+
* (the "License"). You may not use this file except in
9+
* compliance with the License.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14+
* implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*
17+
* @copyright Copyright (c) Skillshare, Inc. <https://www.skillshare.com>
18+
* @license https://opensource.org/licenses/MIT MIT License
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace FormatPHP\Util;
24+
25+
use function preg_replace;
26+
use function trim;
27+
28+
/**
29+
* Provides tools for cleaning messages
30+
*/
31+
trait MessageCleaner
32+
{
33+
/**
34+
* Removes newlines and extra whitespace from the given message string
35+
*/
36+
private function cleanMessage(string $message): string
37+
{
38+
return trim((string) preg_replace('/\n\s*/', ' ', $message));
39+
}
40+
}

0 commit comments

Comments
 (0)