| Total Complexity | 7 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class GridSerializer |
||
| 12 | { |
||
| 13 | public static function serialize(Grid $grid): string |
||
| 21 | } |
||
| 22 | |||
| 23 | public static function deserialize(string $string): Grid |
||
| 24 | { |
||
| 25 | if (strlen($string) !== Grid::NUMBER_OF_LOCATIONS) { |
||
| 26 | throw new LengthException(); |
||
| 27 | } |
||
| 28 | $grid = new Grid(); |
||
| 29 | for ($i = 0; $i < Grid::NUMBER_OF_LOCATIONS; $i++) { |
||
| 30 | $location = self::getLocationByIndex($i); |
||
| 31 | $value = (int) $string[$i]; |
||
| 32 | if (Grid::valueIsValid($value)) { |
||
| 33 | $grid->set($location, $value); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | return $grid; |
||
| 38 | } |
||
| 39 | |||
| 40 | public static function getLocationByIndex(int $index): Location |
||
| 46 | } |
||
| 47 | } |
||
| 48 |