-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGridSnapperService.php
More file actions
58 lines (47 loc) · 1.41 KB
/
GridSnapperService.php
File metadata and controls
58 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Modules\Core\Services;
use Modules\Core\DTOs\GridPositionDTO;
/**
* Service for snapping grid positions to grid boundaries.
*
* Ensures that block positions are aligned to the grid system
* to maintain consistent layout in the report builder.
*/
class GridSnapperService
{
private int $gridSize;
public function __construct(int $gridSize = 12)
{
$this->gridSize = $gridSize;
}
/**
* Snap a position to the grid.
*/
public function snap(GridPositionDTO $position): GridPositionDTO
{
$x = max(0, min($position->getX(), $this->gridSize - 1));
$y = max(0, $position->getY());
$width = max(1, min($position->getWidth(), $this->gridSize - $x));
$height = max(1, $position->getHeight());
return GridPositionDTO::create($x, $y, $width, $height);
}
/**
* Validate that a position fits within the grid.
*/
public function validate(GridPositionDTO $position): bool
{
if ($position->getX() < 0 || $position->getX() >= $this->gridSize) {
return false;
}
if ($position->getY() < 0) {
return false;
}
if ($position->getWidth() < 1) {
return false;
}
if ($position->getHeight() < 1) {
return false;
}
return ! ($position->getX() + $position->getWidth() > $this->gridSize);
}
}