2222
2323namespace FormatPHP ;
2424
25- use FormatPHP \Exception \InvalidArgumentException ;
26- use FormatPHP \Exception \MessageNotFoundException ;
27- use FormatPHP \Exception \UnableToGenerateMessageIdException ;
28- use FormatPHP \Extractor \IdInterpolator ;
2925use IteratorAggregate ;
3026use 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
4134 */
4235final 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}
0 commit comments