Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
#45 Add rootline and rootline item class
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Kleinhans committed Dec 13, 2016
1 parent 5f11183 commit 4fbac73
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/StackFormation/PreProcessor/Rootline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace StackFormation\PreProcessor;

/**
* Class Rootline
*
* The rootline shows gives the transformers access to the parent objects (all generations)
* since sometimes you need to change things outside of the current value (e.g. when replacing the whole node)
*/
class Rootline extends \ArrayObject {

/**
* @return array
*/
protected function getKeys() {
return array_keys($this->getArrayCopy());
}

/**
* @param $index
* @return mixed
*/
public function indexGet($index) {
$keys = $this->getKeys();
return $this->offsetGet($keys[$index]);
}

public function removeLast() {
$keys = $this->getKeys();

// TODO
@$this->offsetUnset($keys[$this->count()]);
}

/**
* @param int $generation
* @return mixed
*/
public function parent($generation = 1) {
$keys = $this->getKeys();
return $this->offsetGet($keys[$this->count() - $generation]);
}
}
38 changes: 38 additions & 0 deletions src/StackFormation/PreProcessor/RootlineItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace StackFormation\PreProcessor;

/**
* Class RootlineItem
*
* Since you not only need the reference to the parent(s) but also their key in relation to their parent
* (e.g. in case you need to delete them), this rootline item represents the item itself (value) and the position (key)
*/
class RootlineItem {

protected $key;
protected $value;

/**
* @param string $key
* @param mixed $value
*/
public function __construct($key, $value) {
$this->key = $key;
$this->value = $value;
}

/**
* @return string
*/
public function getKey() {
return $this->key;
}

/**
* @return mixed
*/
public function getValue() {
return $this->value;
}
}

0 comments on commit 4fbac73

Please sign in to comment.