TreeMovePlan
in package
Move of a node to a chosen position among the children of the target parent. Shared translation of the choice "At the end / At the start / After item..." (the "Move to..." modal in the admin grid and the position in the tree edit form) into `afterId` + the {@see TreeMoveExpectation} contract.
The neighbours (prevId/nextId) are derived from the snapshot of the target parent's
children that the client received at render time (in lft order, without the moved node).
The server verifies the contract under the tree lock: if the list of children changed so
that the chosen position no longer lies between the same neighbours, the move ends with
TREE_STALE and nothing is written. Drag and drop would send the same neighbours if the
user dragged the node to the same place.
Pure logic without DB, see TreeMovePlanTest.
Table of Contents
Constants
- META_AFTER_ID : mixed = 'moveSubtreeAfterId'
- Entity save meta key: the move `afterId` ({@see \App\Core\Traits\Api\Models\Managers\TreeTrait::applyMetaSubtreeMove()}).
- META_NEIGHBOURS : mixed = 'moveSubtreeNeighbours'
- Entity save meta key: expected neighbours `['prevId' => ..., 'nextId' => ...]`.
- POSITION_AFTER : mixed = 'after'
- POSITION_END : mixed = 'end'
- POSITION_KEEP : mixed = 'keep'
- Keep the current place (edit form only); on a parent change it means to the end.
- POSITION_START : mixed = 'start'
Properties
Methods
- fromPosition() : self|null
- Turns a position choice + a children snapshot into a move plan; `null` means an invalid choice (unknown position, `after` without a sibling or with a sibling outside the snapshot, `keep`).
- parseSiblingIds() : array<int, int>
- Turns the children snapshot from the form hidden field (`"12,15,9"`) into a list of ids.
- positions() : array<int, string>
- toExpectation() : TreeMoveExpectation
- Returns the move contract; `$oldParentId` is the parent the node is under according to the client.
- toMeta() : array{moveSubtreeAfterId: int, moveSubtreeNeighbours: array{prevId: int, nextId: int}}
- Meta for saving the entity through a form (the move runs in Api `afterSave` inside the save transaction).
- __construct() : mixed
Constants
META_AFTER_ID
Entity save meta key: the move `afterId` ({@see \App\Core\Traits\Api\Models\Managers\TreeTrait::applyMetaSubtreeMove()}).
public
mixed
META_AFTER_ID
= 'moveSubtreeAfterId'
META_NEIGHBOURS
Entity save meta key: expected neighbours `['prevId' => ..., 'nextId' => ...]`.
public
mixed
META_NEIGHBOURS
= 'moveSubtreeNeighbours'
POSITION_AFTER
public
mixed
POSITION_AFTER
= 'after'
POSITION_END
public
mixed
POSITION_END
= 'end'
POSITION_KEEP
Keep the current place (edit form only); on a parent change it means to the end.
public
mixed
POSITION_KEEP
= 'keep'
POSITION_START
public
mixed
POSITION_START
= 'start'
Properties
$afterId read-only
public
int
$afterId
$nextId read-only
public
int
$nextId
$prevId read-only
public
int
$prevId
Methods
fromPosition()
Turns a position choice + a children snapshot into a move plan; `null` means an invalid choice (unknown position, `after` without a sibling or with a sibling outside the snapshot, `keep`).
public
static fromPosition(string $position, int|null $afterSiblingId, array<int, int> $siblingIds, int $nodeId) : self|null
Parameters
- $position : string
- $afterSiblingId : int|null
- $siblingIds : array<int, int>
-
children of the target parent in
lftorder (the moved node is skipped itself) - $nodeId : int
Return values
self|nullparseSiblingIds()
Turns the children snapshot from the form hidden field (`"12,15,9"`) into a list of ids.
public
static parseSiblingIds(mixed $raw) : array<int, int>
Parameters
- $raw : mixed
Return values
array<int, int>positions()
public
static positions() : array<int, string>
Return values
array<int, string> —valid position choices (without keep, which only the form knows)
toExpectation()
Returns the move contract; `$oldParentId` is the parent the node is under according to the client.
public
toExpectation(int|null $oldParentId) : TreeMoveExpectation
Parameters
- $oldParentId : int|null
Return values
TreeMoveExpectationtoMeta()
Meta for saving the entity through a form (the move runs in Api `afterSave` inside the save transaction).
public
toMeta() : array{moveSubtreeAfterId: int, moveSubtreeNeighbours: array{prevId: int, nextId: int}}
Return values
array{moveSubtreeAfterId: int, moveSubtreeNeighbours: array{prevId: int, nextId: int}}__construct()
private
__construct(int $afterId, int $prevId, int $nextId) : mixed
Parameters
- $afterId : int
- $prevId : int
- $nextId : int