1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Spreadsheet; |
5
|
|
|
|
6
|
|
|
use Spreadsheet\Exception\{ |
7
|
|
|
CellAlreadyExistsException, |
8
|
|
|
CellNotFoundException |
9
|
|
|
}; |
10
|
|
|
use Innmind\Immutable\{ |
11
|
|
|
Map, |
12
|
|
|
MapInterface |
13
|
|
|
}; |
14
|
|
|
|
15
|
|
|
final class Sheet implements SheetInterface |
16
|
|
|
{ |
17
|
|
|
private $name; |
18
|
|
|
private $columns; |
19
|
|
|
private $rows; |
20
|
|
|
|
21
|
9 |
|
public function __construct(string $name) |
22
|
|
|
{ |
23
|
9 |
|
$this->name = $name; |
24
|
9 |
|
$this->columns = new Map('scalar', ColumnInterface::class); |
25
|
9 |
|
$this->rows = new Map('scalar', RowInterface::class); |
26
|
9 |
|
} |
27
|
|
|
|
28
|
7 |
|
public function name(): string |
29
|
|
|
{ |
30
|
7 |
|
return $this->name; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
1 |
|
public function columns(): MapInterface |
37
|
|
|
{ |
38
|
1 |
|
return $this->columns; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
1 |
|
public function rows(): MapInterface |
45
|
|
|
{ |
46
|
1 |
|
return $this->rows; |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
public function add(CellInterface $cell): SheetInterface |
50
|
|
|
{ |
51
|
3 |
|
if ($this->has($cell->position())) { |
52
|
1 |
|
throw new CellAlreadyExistsException; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
return $this->position($cell); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
public function replace(CellInterface $cell): SheetInterface |
59
|
|
|
{ |
60
|
2 |
|
if (!$this->has($cell->position())) { |
61
|
1 |
|
throw new CellNotFoundException; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $this->position($cell); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function get(Position $position): CellInterface |
68
|
|
|
{ |
69
|
1 |
|
if (!$this->has($position)) { |
70
|
|
|
throw new CellNotFoundException; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this |
74
|
1 |
|
->columns |
75
|
1 |
|
->get($position->column()) |
76
|
1 |
|
->cells() |
77
|
1 |
|
->get($position->row()); |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
public function has(Position $position): bool |
81
|
|
|
{ |
82
|
|
|
if ( |
83
|
4 |
|
$this->columns->contains($position->column()) && |
84
|
4 |
|
$this->columns->get($position->column())->cells()->contains($position->row()) |
85
|
|
|
) { |
86
|
3 |
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
private function position(CellInterface $cell): self |
93
|
|
|
{ |
94
|
3 |
|
$column = $cell->position()->column(); |
95
|
3 |
|
$row = $cell->position()->row(); |
96
|
3 |
|
$columns = $this->columns; |
97
|
3 |
|
$rows = $this->rows; |
98
|
|
|
|
99
|
3 |
View Code Duplication |
if (!$columns->contains($column)) { |
|
|
|
|
100
|
3 |
|
$columns = $columns->put( |
101
|
|
|
$column, |
102
|
3 |
|
new Column($column, new Map('scalar', CellInterface::class)) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
View Code Duplication |
if (!$rows->contains($row)) { |
|
|
|
|
107
|
3 |
|
$rows = $rows->put( |
108
|
|
|
$row, |
109
|
3 |
|
new Row($row, new Map('scalar', CellInterface::class)) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
$columns = $columns->put( |
114
|
|
|
$column, |
115
|
3 |
|
new Column( |
116
|
|
|
$column, |
117
|
3 |
|
$columns->get($column)->cells()->put( |
118
|
|
|
$row, |
119
|
|
|
$cell |
120
|
|
|
) |
121
|
|
|
) |
122
|
|
|
); |
123
|
3 |
|
$rows = $rows->put( |
124
|
|
|
$row, |
125
|
3 |
|
new Row( |
126
|
|
|
$row, |
127
|
3 |
|
$rows->get($row)->cells()->put( |
128
|
|
|
$column, |
129
|
|
|
$cell |
130
|
|
|
) |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
|
134
|
3 |
|
$sheet = new self($this->name); |
135
|
3 |
|
$sheet->rows = $rows; |
136
|
3 |
|
$sheet->columns = $columns; |
137
|
|
|
|
138
|
3 |
|
return $sheet; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.