Passed
Push — main ( f4e9d2...4a4379 )
by Jesse
01:41
created

SlidingPuzzleFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\Puzzle\SlidingPuzzle;
4
5
use Stratadox\PuzzleSolver\Puzzle;
6
use Stratadox\PuzzleSolver\PuzzleFactory;
7
use function explode;
8
use function preg_split;
9
use function str_replace;
10
11
final class SlidingPuzzleFactory implements PuzzleFactory
12
{
13
    /** @var string */
14
    private $separator;
15
16
    public function __construct(string $separator)
17
    {
18
        $this->separator = $separator;
19
    }
20
21
    public static function make(): PuzzleFactory
22
    {
23
        return new self(',');
24
    }
25
26
    public function fromString(string $puzzle): Puzzle
27
    {
28
        $pieces = [];
29
        $puzzle = str_replace(' ', '', $puzzle);
30
        foreach (preg_split("/(\n)|(\r)/", $puzzle) as $line) {
31
            if (empty($line)) {
32
                continue;
33
            }
34
            $row = [];
35
            foreach (explode($this->separator, $line) as $value) {
36
                $row[] = (int) $value;
37
            }
38
            $pieces[] = $row;
39
        }
40
        return SlidingPuzzle::withPieces(...$pieces);
41
    }
42
}
43