1: <?php
2:
3: namespace IPay\Captcha;
4:
5: use Symfony\Component\DomCrawler\Crawler;
6:
7: final class CaptchaSolver
8: {
9: private const LENGTH_TO_NUMBER_MAPPING = [
10: 66 => 0,
11: 49 => 1,
12: 53 => 2,
13: 70 => 3,
14: 76 => 4,
15: 58 => 5,
16: 64 => 6,
17: 50 => 7,
18: 75 => 8,
19: 69 => 9,
20: ];
21:
22: public static function solve(string $svg): string
23: {
24: /** @var string[] */
25: $pathCommands = (new Crawler($svg))
26: ->filterXPath('//path[@fill!="none"]')
27: ->each(function (Crawler $node): string {
28: return (string) $node->attr('d');
29: });
30:
31: sort($pathCommands, SORT_NATURAL);
32:
33: $result = '';
34: foreach ($pathCommands as $pathCommand) {
35: /**
36: * Only keep path commands.
37: *
38: * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#path_commands
39: *
40: * @var string
41: */
42: $paths = preg_replace('/[^MLHVCSQTAZmlhvcsqtaz]/', '', $pathCommand);
43: $result .= static::LENGTH_TO_NUMBER_MAPPING[strlen($paths)];
44: }
45:
46: return $result;
47: }
48: }
49: