# NodeContext

> Represents the immutable target position and depth offset of a node insertion or move relative to a target node.

[API reference](/docs/api/index.md) › [Nested sets behavior](/docs/extensions/nested-sets-behavior/reference/index.md) › NodeContext

| final readonly class | `yii2\extensions\nestedsets\NodeContext` |
| --- | --- |
| Source | [`src/NodeContext.php`](https://github.com/yii2-extensions/nested-sets-behavior/blob/main/src/NodeContext.php#L12) |

Represents the immutable target position and depth offset of a node insertion or move relative to a target node.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-depthLevelDelta"></span>`$depthLevelDelta` readonly constructor | `int` | Depth offset relative to the target node: `1` for a child, `0` for a sibling. | `NodeContext` |
| <span id="property-targetPositionValue"></span>`$targetPositionValue` readonly constructor | `int` | Left value the inserted or moved node takes. | `NodeContext` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a context for a position relative to the target node. | `NodeContext` |
| [`forAppendTo()`](#method-forAppendTo) static | Creates the context that places a node as the last child of the target node. | `NodeContext` |
| [`forInsertAfter()`](#method-forInsertAfter) static | Creates the context that places a node as the next sibling of the target node. | `NodeContext` |
| [`forInsertBefore()`](#method-forInsertBefore) static | Creates the context that places a node as the previous sibling of the target node. | `NodeContext` |
| [`forPrependTo()`](#method-forPrependTo) static | Creates the context that places a node as the first child of the target node. | `NodeContext` |
| [`getTargetDepth()`](#method-getTargetDepth) | Returns the depth of the target node. | `NodeContext` |
| [`getTargetTreeValue()`](#method-getTargetTreeValue) | Returns the tree identifier of the target node. | `NodeContext` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(yii\db\ActiveRecord $targetNode, int $targetPositionValue, int $depthLevelDelta)
```

Creates a context for a position relative to the target node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$targetNode` | [`yii\db\ActiveRecord`](/docs/api/yii/db/activerecord/index.md) | Node the insertion or move is relative to. |
| `$targetPositionValue` | `int` | Left value the inserted or moved node takes. |
| `$depthLevelDelta` | `int` | Depth offset relative to the target node: `1` for a child, `0` for a sibling. |

### forAppendTo() <span id="method-forAppendTo"></span>

```php
public static function forAppendTo(yii\db\ActiveRecord $targetNode, "rgt" $rightAttribute): self
```

Creates the context that places a node as the last child of the target node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$targetNode` | [`yii\db\ActiveRecord`](/docs/api/yii/db/activerecord/index.md) | Node that becomes the parent. |
| `$rightAttribute` | `"rgt"` | Name of the right boundary attribute. |

**Returns**

`self`: Context positioned at the target right value.

### forInsertAfter() <span id="method-forInsertAfter"></span>

```php
public static function forInsertAfter(yii\db\ActiveRecord $targetNode, "rgt" $rightAttribute): self
```

Creates the context that places a node as the next sibling of the target node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$targetNode` | [`yii\db\ActiveRecord`](/docs/api/yii/db/activerecord/index.md) | Node that becomes the previous sibling. |
| `$rightAttribute` | `"rgt"` | Name of the right boundary attribute. |

**Returns**

`self`: Context positioned right after the target right value.

### forInsertBefore() <span id="method-forInsertBefore"></span>

```php
public static function forInsertBefore(yii\db\ActiveRecord $targetNode, "lft" $leftAttribute): self
```

Creates the context that places a node as the previous sibling of the target node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$targetNode` | [`yii\db\ActiveRecord`](/docs/api/yii/db/activerecord/index.md) | Node that becomes the next sibling. |
| `$leftAttribute` | `"lft"` | Name of the left boundary attribute. |

**Returns**

`self`: Context positioned at the target left value.

### forPrependTo() <span id="method-forPrependTo"></span>

```php
public static function forPrependTo(yii\db\ActiveRecord $targetNode, "lft" $leftAttribute): self
```

Creates the context that places a node as the first child of the target node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$targetNode` | [`yii\db\ActiveRecord`](/docs/api/yii/db/activerecord/index.md) | Node that becomes the parent. |
| `$leftAttribute` | `"lft"` | Name of the left boundary attribute. |

**Returns**

`self`: Context positioned right after the target left value.

### getTargetDepth() <span id="method-getTargetDepth"></span>

```php
public function getTargetDepth("depth" $depthAttribute): int
```

Returns the depth of the target node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$depthAttribute` | `"depth"` | Name of the depth attribute. |

**Returns**

`int`: Depth of the target node.e

### getTargetTreeValue() <span id="method-getTargetTreeValue"></span>

```php
public function getTargetTreeValue(false|string $treeAttribute): mixed
```

Returns the tree identifier of the target node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$treeAttribute` | `false\|string` | Name of the tree attribute, or `false` when multiple trees are disabled. |

**Returns**

`mixed`: Tree identifier, or `null` when `$treeAttribute` is `false`.