|
| 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\Format\Reader; |
| 24 | + |
| 25 | +use FormatPHP\Exception\InvalidMessageShapeException; |
| 26 | +use FormatPHP\Format\ReaderInterface; |
| 27 | +use FormatPHP\Message; |
| 28 | +use FormatPHP\MessageCollection; |
| 29 | + |
| 30 | +use function gettype; |
| 31 | +use function is_array; |
| 32 | +use function is_string; |
| 33 | +use function sprintf; |
| 34 | + |
| 35 | +/** |
| 36 | + * Returns a MessageCollection parsed from JSON-decoded data that was written |
| 37 | + * using {@see ChromeWriter} |
| 38 | + */ |
| 39 | +class ChromeReader implements ReaderInterface |
| 40 | +{ |
| 41 | + /** |
| 42 | + * @inheritdoc |
| 43 | + */ |
| 44 | + public function __invoke(array $data): MessageCollection |
| 45 | + { |
| 46 | + $messages = new MessageCollection(); |
| 47 | + |
| 48 | + /** |
| 49 | + * @var string $messageId |
| 50 | + * @var array{message: string} $message |
| 51 | + */ |
| 52 | + foreach ($data as $messageId => $message) { |
| 53 | + $this->validateShape($messageId, $message); |
| 54 | + |
| 55 | + $messages[] = new Message($messageId, $message['message']); |
| 56 | + } |
| 57 | + |
| 58 | + return $messages; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param array-key $messageId |
| 63 | + * @param mixed $message |
| 64 | + * |
| 65 | + * @throws InvalidMessageShapeException |
| 66 | + */ |
| 67 | + private function validateShape($messageId, $message): void |
| 68 | + { |
| 69 | + if (!is_string($messageId)) { |
| 70 | + throw new InvalidMessageShapeException(sprintf( |
| 71 | + '%s expects a string message ID; received %s', |
| 72 | + static::class, |
| 73 | + gettype($messageId), |
| 74 | + )); |
| 75 | + } |
| 76 | + |
| 77 | + if (!is_array($message) || !is_string($message['message'] ?? null)) { |
| 78 | + throw new InvalidMessageShapeException(sprintf( |
| 79 | + '%s expects a string message property; message does not exist or is not a string', |
| 80 | + static::class, |
| 81 | + )); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments