summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2015-05-11 20:51:58 +0200
committerAndreas Gohr <andi@splitbrain.org>2015-05-11 20:51:58 +0200
commitef0c211b2ac87b8e3c6e85b600021389be6209bf (patch)
tree87d8c9bde8145040877b881343def061d9027314
parent1f5d8b65a983fe0914971ee0bb4e5e58cbf8c8a7 (diff)
downloadrpg-ef0c211b2ac87b8e3c6e85b600021389be6209bf.tar.gz
rpg-ef0c211b2ac87b8e3c6e85b600021389be6209bf.tar.bz2
added element query functions
-rw-r--r--inc/Form/Form.php93
1 files changed, 90 insertions, 3 deletions
diff --git a/inc/Form/Form.php b/inc/Form/Form.php
index 30e16615c..2c893fbbc 100644
--- a/inc/Form/Form.php
+++ b/inc/Form/Form.php
@@ -67,17 +67,80 @@ class Form extends Element {
return $this;
}
- #region Element adding functions
+ #region element query function
+
+ /**
+ * Returns the numbers of elements in the form
+ *
+ * @return int
+ */
+ public function elementCount() {
+ return count($this->elements);
+ }
+
+ /**
+ * Returns a reference to the element at a position.
+ * A position out-of-bounds will return either the
+ * first (underflow) or last (overflow) element.
+ *
+ * @param $pos
+ * @return Element
+ */
+ public function getElementAt($pos) {
+ if($pos < 0) $pos = count($this->elements) + $pos;
+ if($pos < 0) $pos = 0;
+ if($pos >= count($this->elements)) $pos = count($this->elements) - 1;
+ return $this->elements[$pos];
+ }
/**
- * Adds an element to the end of the form
+ * Gets the position of the first of a type of element
+ *
+ * @param string $type Element type to look for.
+ * @param int $offset search from this position onward
+ * @return false|int position of element if found, otherwise false
+ */
+ public function findPositionByType($type, $offset = 0) {
+ $len = $this->elementCount();
+ for($pos = $offset; $pos < $len; $pos++) {
+ if($this->elements[$pos]->getType() == $type) {
+ return $pos;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Gets the position of the first element matching the attribute
+ *
+ * @param string $name Name of the attribute
+ * @param string $value Value the attribute should have
+ * @param int $offset search from this position onward
+ * @return false|int position of element if found, otherwise false
+ */
+ public function findPositionByAttribute($name, $value, $offset = 0) {
+ $len = $this->elementCount();
+ for($pos = $offset; $pos < $len; $pos++) {
+ if($this->elements[$pos]->attr($name) == $value) {
+ return $pos;
+ }
+ }
+ return false;
+ }
+
+ #endregion
+
+ #region Element positioning functions
+
+ /**
+ * Adds or inserts an element to the form
*
* @param Element $element
* @param int $pos 0-based position in the form, -1 for at the end
* @return Element
*/
public function addElement(Element $element, $pos = -1) {
- if(is_a($element, 'Doku_Form2')) throw new \InvalidArgumentException('You can\'t add a form to a form');
+ if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form');
if($pos < 0) {
$this->elements[] = $element;
} else {
@@ -87,6 +150,30 @@ class Form extends Element {
}
/**
+ * Replaces an existing element with a new one
+ *
+ * @param Element $element the new element
+ * @param $pos 0-based position of the element to replace
+ */
+ public function replaceElement(Element $element, $pos) {
+ if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form');
+ array_splice($this->elements, $pos, 1, array($element));
+ }
+
+ /**
+ * Remove an element from the form completely
+ *
+ * @param $pos 0-based position of the element to remove
+ */
+ public function removeElement($pos) {
+ array_splice($this->elements, $pos, 1);
+ }
+
+ #endregion
+
+ #region Element adding functions
+
+ /**
* Adds a text input field
*
* @param $name