closedquestion-8.x-3.x-dev/src/Question/Textlabel/TextLabel.php

src/Question/Textlabel/TextLabel.php
<?php

namespace Drupal\closedquestion\Question\Textlabel;

/**
 * Class TextLabel.
 *
 * Textlabels are used to turn a math number or a string into a text string or a
 * piece of html.
 *
 * @package Drupal\closedquestion\Question\Textlabel
 */
class TextLabel {

  /**
   * The unique id of this TextLabel.
   *
   * @var string
   */
  public $id;

  /**
   * The list of triggers used to set the content of this TextLabel.
   *
   * @var TriggerNumber[]
   */
  public $triggers = array();

  /**
   * Get the id of this text label.
   *
   * @return string
   *   The id of this TextLabel.
   */
  public function getId() {
    return $this->id;
  }

  /**
   * Check the triggers of this Label.
   *
   * Check against the given input and return the
   * text of the first trigger that matches.
   *
   * @param mixed $input
   *   The input to check the triggers against.
   *
   * @return string
   *   The text of the matching trigger (if any).
   */
  public function getValue($input) {
    foreach ($this->triggers as $trigger) {
      if ($trigger->matches($input)) {
        return $trigger->getText();
      }
    }
  }

  /**
   * Initialise this TextLabel by using values from the given XML node.
   *
   * @param object $node
   *   DOMElement The node to use for initialisation.
   * @param object $context
   *   The question or other object that the mapping can
   *   query for things like the current answer, draggables, hotspots and the
   *   parsing of html.
   */
  public function initFromNode($node, $context) {
    $attribs = $node->attributes;
    $item = $attribs->getNamedItem('id');
    if ($item !== NULL) {
      $this->id = $item->value;
    }

    foreach ($node->childNodes as $child) {
      switch (strtolower($child->nodeName)) {
        case 'triggernumber':
          $trigger = new TriggerNumber();
          $trigger->initFromNode($child, $context);
          $this->triggers[] = $trigger;
          break;
      }
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc