|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Helper\ComponentPosition; |
|
15
|
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Daniel West <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ComponentPositionSortValueHelper |
|
22
|
|
|
{ |
|
23
|
|
|
public function calculateSortValue(ComponentPosition $componentPosition, ?int $originalSortValue): void |
|
24
|
|
|
{ |
|
25
|
|
|
$sortCollection = $componentPosition->getSortCollection(); |
|
26
|
|
|
$sortValueSet = null !== $componentPosition->sortValue; |
|
27
|
|
|
if (!$sortCollection) { |
|
28
|
|
|
if (!$sortValueSet) { |
|
29
|
|
|
$componentPosition->setSortValue(0); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (null !== $originalSortValue) { |
|
36
|
|
|
$moveTo = $componentPosition->sortValue; |
|
37
|
|
|
if ($moveTo === $originalSortValue) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$positionIsSame = static function (ComponentPosition $posA, ComponentPosition $posB) { |
|
42
|
|
|
return $posA->getId() === $posB->getId(); |
|
43
|
|
|
}; |
|
44
|
|
|
|
|
45
|
|
|
if ($moveTo > $originalSortValue) { |
|
46
|
|
|
// value increased |
|
47
|
|
|
foreach ($sortCollection as $existingComponentPosition) { |
|
48
|
|
|
if ($positionIsSame($existingComponentPosition, $componentPosition)) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
if ( |
|
52
|
|
|
$existingComponentPosition->sortValue > $originalSortValue && |
|
53
|
|
|
$existingComponentPosition->sortValue <= $moveTo |
|
54
|
|
|
) { |
|
55
|
|
|
--$existingComponentPosition->sortValue; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// value decreased |
|
63
|
|
|
foreach ($sortCollection as $existingComponentPosition) { |
|
64
|
|
|
if ($positionIsSame($existingComponentPosition, $componentPosition)) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
if ( |
|
68
|
|
|
$existingComponentPosition->sortValue < $originalSortValue && |
|
69
|
|
|
$existingComponentPosition->sortValue >= $moveTo |
|
70
|
|
|
) { |
|
71
|
|
|
++$existingComponentPosition->sortValue; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!$sortValueSet) { |
|
79
|
|
|
/** @var ComponentPosition|null $lastPosition */ |
|
80
|
|
|
$lastPosition = $sortCollection->last(); |
|
81
|
|
|
if ($lastPosition) { |
|
82
|
|
|
$nextValue = $lastPosition->sortValue + 1; |
|
83
|
|
|
$componentPosition->setSortValue($nextValue); |
|
84
|
|
|
} else { |
|
85
|
|
|
$componentPosition->setSortValue(0); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
foreach ($sortCollection as $existingComponentPosition) { |
|
90
|
|
|
if ($existingComponentPosition->sortValue >= $componentPosition->sortValue) { |
|
91
|
|
|
++$existingComponentPosition->sortValue; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|