11<?php
2+
23namespace Pion \Support \Eloquent \Position \Query ;
34
45use Pion \Support \Eloquent \Position \Traits \PositionTrait ;
@@ -23,22 +24,20 @@ class MoveQuery extends AbstractPositionQuery
2324 protected $ position ;
2425
2526 /**
26- * Comparision condition for old position value.
27+ * Comparison condition for old position value.
2728 * In default is for decrement.
2829 * @var string
2930 */
30- protected $ oldComparisionCondition = '> ' ;
31+ protected $ oldComparisonCondition = '> ' ;
3132 /**
32- * Comparision condition for new position value.
33+ * Comparison condition for new position value.
3334 * In default is for decrement.
3435 * @var string
3536 */
36- protected $ newComparisionCondition = '<= ' ;
37+ protected $ newComparisonCondition = '<= ' ;
3738
3839
3940 /**
40- * PositionQuery constructor.
41- *
4241 * @param Model|PositionTrait $model
4342 * @param int $position
4443 * @param int|null $oldPosition
@@ -56,7 +55,7 @@ public function __construct($model, $position, $oldPosition)
5655 $ this ->positionColumn = $ model ->getPositionColumn ();
5756
5857 // Build the comparision condition
59- $ this ->buildComparisionCondition ();
58+ $ this ->buildComparisonCondition ();
6059
6160 // Prepare the query
6261 $ this ->query = $ this ->buildQuery ();
@@ -76,6 +75,15 @@ public function runQuery($query)
7675 if ($ this ->increment ) {
7776 return $ query ->increment ($ this ->positionColumn );
7877 } else {
78+ // Get the last position and move the position
79+ $ lastPosition = $ query ->max ($ this ->positionColumn ) ?: 0 ;
80+
81+ // If the set position is out of the bounds of current items, force new position
82+ if ($ this ->position > $ lastPosition ) {
83+ $ this ->position = $ lastPosition ;
84+ $ this ->model ()->setPosition ($ this ->position );
85+ }
86+
7987 return $ query ->decrement ($ this ->positionColumn );
8088 }
8189 }
@@ -90,18 +98,18 @@ protected function buildQuery()
9098 $ query = parent ::buildQuery ();
9199
92100 // Build the where condition for the position. This will ensure to update only required rows
93- return $ query ->where ($ this ->positionColumn , $ this ->oldComparisionCondition , $ this ->oldPosition )
94- ->where ($ this ->positionColumn , $ this ->newComparisionCondition , $ this ->position );
101+ return $ query ->where ($ this ->positionColumn , $ this ->oldComparisonCondition , $ this ->oldPosition )
102+ ->where ($ this ->positionColumn , $ this ->newComparisonCondition , $ this ->position );
95103 }
96104
97105 /**
98- * Builds the correct comparision condition
106+ * Builds the correct comparison condition
99107 */
100- protected function buildComparisionCondition ()
108+ protected function buildComparisonCondition ()
101109 {
102110 if ($ this ->increment ) {
103- $ this ->oldComparisionCondition = '< ' ;
104- $ this ->newComparisionCondition = '>= ' ;
111+ $ this ->oldComparisonCondition = '< ' ;
112+ $ this ->newComparisonCondition = '>= ' ;
105113 }
106114 }
107115 //endregion
0 commit comments