| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\FieldsAPI\Concerns; |
| 4 |
|
| 5 |
use Give\Framework\FieldsAPI\Contracts\Collection; |
| 6 |
use Give\Framework\FieldsAPI\Contracts\Node; |
| 7 |
|
| 8 |
/** |
| 9 |
* Stores an reference to the node being moved for a fluent API. |
| 10 |
* Combines `remove` and `insert*` methods for a declarative API. |
| 11 |
*/ |
| 12 |
class MoveNodeProxy |
| 13 |
{ |
| 14 |
|
| 15 |
/** @var Collection */ |
| 16 |
protected $collection; |
| 17 |
|
| 18 |
/** @var Node */ |
| 19 |
protected $targetNode; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param Collection $collection |
| 23 |
*/ |
| 24 |
public function __construct(Collection $collection) |
| 25 |
{ |
| 26 |
$this->collection = $collection; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @param Node $node |
| 31 |
*/ |
| 32 |
public function move($node) |
| 33 |
{ |
| 34 |
$this->targetNode = $node; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param string $name The name of the node after which the target node should be inserted. |
| 39 |
*/ |
| 40 |
public function after($name) |
| 41 |
{ |
| 42 |
$this->collection->remove($this->targetNode->getName()); |
| 43 |
$this->collection->insertAfter($name, $this->targetNode); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param string $name The name of the node before which the target node should be inserted. |
| 48 |
*/ |
| 49 |
public function before($name) |
| 50 |
{ |
| 51 |
$this->collection->remove($this->targetNode->getName()); |
| 52 |
$this->collection->insertBefore($name, $this->targetNode); |
| 53 |
} |
| 54 |
} |
| 55 |
|