public function notifyObservers()
 {
     for ($i = 0; $i < $this->observers->size(); $i++) {
         $observer = $this->observers->get($i);
         $observer->update($this->temperature, $this->humidity, $this->pressure);
     }
 }
 public function hasNext()
 {
     if ($this->position >= $this->items->size()) {
         return FALSE;
     } else {
         return TRUE;
     }
 }
Пример #3
0
 function testAddSameElementTwice()
 {
     $list = new ArrayList("string");
     $list->add("Hello there");
     $list->add("Hello there");
     $this->assertEqual(2, $list->size());
 }
Пример #4
0
 /**
  * Se este objeto foi alterado, como indicado pelo método hasChanged, em seguida, 
  * notificar todos os seus observadores e, em seguida, chamar o método clearChanged 
  * para indicar que este objeto não mudou.
  * @param \Prime\util\Object $arg
  */
 public function notifyObservers()
 {
     for ($index = 0; $index < count($this->observers->size()); $index++) {
         $observer = $this->observers->get($index);
         /* @var $observer IObserver */
         $observer->update($this);
     }
     $this->clearChanged();
 }
Пример #5
0
/**
 * Turns any supplied ArrayList or array into a HTML5 valid table. A few 
 * things to note on this function, first is that this function is dependant
 * on the second argument to supply the Table Matrix.
 * 
 * How does the Table Matrix work? Since the array needs some list of 
 * information to pull out of the items that are in the array.
 * 
 * So for example if your array looks like this:
 *      $arr = [
 *          {name: "My Name", date: "My Date", password: "******"},
 *          {name: "Start Name", date: "Some Date", password: "******"},
 *          {name: "Some Name", date: "End Date", password: "******"}
 *      ];
 * 
 * Then most likely your table Matrix would be something like this:
 *      $matrix = [
 *          {name: "Name", method: "getName"},
 *          {date: "Date", method: "getDate"}
 *      ];
 * 
 * Why use this? Well two reasons, most obvious to help formatting, the 
 * other is to stop unwanted data being leaked into the table (In this case
 * the object $passsword)
 * 
 * @param ArrayList|array $table_data
 * @param ArrayList|array $table_matrix
 * @param ArrayList|array $classes
 * @param string $id
 * @param int $maxlength
 */
function generateTable($table_data, $table_matrix, $classes = null, $id = null, $maxlength = -1)
{
    //Validate
    if (!is_array($table_data) && !$table_data instanceof ArrayList) {
        throw new Exception('Table Data must be an Array/ArrayList');
    }
    if (!is_array($table_matrix) && !$table_matrix instanceof ArrayList) {
        throw new Exception('Table Matrix is invalid.');
    }
    //Translate
    if (is_array($table_data)) {
        $table_data = new ArrayList($table_data);
    }
    if (is_array($table_matrix)) {
        $table_matrix = new ArrayList($table_matrix);
    }
    //Coordinate
    $x = '<table';
    if ($classes !== null) {
        if (is_string($classes)) {
            $x .= ' class="' . $classes . '"';
        } else {
            if (is_array($classes) || $classes instanceof ArrayList) {
                if (is_array($classes)) {
                    $classes = new ArrayList($classes);
                }
                $x .= ' class="' . $classes->implode(' ') . '"';
            }
        }
    }
    if ($id !== null && is_string($id)) {
        $x .= ' id="' . $id . '"';
    }
    $x .= '>';
    //Now we can form our table header
    $x .= '<thead><tr>';
    foreach ($table_matrix as $thead) {
        if (!is_array($thead) && !$thead instanceof ArrayList) {
            throw new Exception('Malformed table matrix.');
        }
        if (is_array($thead)) {
            $thead = new ArrayList($thead);
        }
        if (!$thead->isKeySet('name')) {
            throw new Exception('Malformed table matrix. (Need col name)');
        }
        if (!$thead->isKeySet('method')) {
            throw new Exception('Malformed table matrix. (Need col method)');
        }
        $x .= '<th>' . $thead["name"] . '</th>';
    }
    $x .= '</tr></thead>';
    //Now the table data.
    $x .= '<tbody>';
    for ($i = 0; $i < $table_data->size(); $i++) {
        $obj = $table_data[$i];
        //Now iterate over the matrix
        $x .= '<tr>';
        foreach ($table_matrix as $thead) {
            $args = array();
            if (is_array($thead)) {
                $thead = new ArrayList($thead);
            }
            if ($thead->isKeySet("args")) {
                $args = $def["args"];
            }
            $method = $thead["method"];
            $result;
            //Definition
            if (is_array($method) || $method instanceof ArrayList) {
                if (is_array($method)) {
                    $method = new ArrayList($method);
                }
                /*
                 * If $method is an array then the expected result from a
                 * call_user_func_array on $array[0] is another object.
                 * 
                 * Basically we're going to run through until we reach 
                 * $method->size() on each returned object.
                 * 
                 * e.g. if I have the following:
                 * {
                 *      getChild() {
                 *          return {getName() {"Simon"}};
                 *      }
                 * }
                 * 
                 * I would have the following $method:
                 * ["getChild", "getName"]
                 * 
                 * To overcomplicate again, if I had:
                 *  {
                 *      getChild() {
                 *          return {
                 *              getButt() {
                 *                  return {getName() { "Simon"}};
                 *              }
                 *          };
                 *      }
                 *  }
                 * 
                 * And my $method would be:
                 * ["getChild", "getButt", "getName"]
                 * 
                 * TECHNICALLY speaking you can supply args, I will make
                 * this better in the future but for now I wouldn't.
                 */
                $result = $obj;
                foreach ($method as $func) {
                    $result = call_user_func_array(array($result, $func), $args);
                }
            } else {
                $result = call_user_func_array(array($obj, $thead["method"]), $args);
            }
            //HTML Fix up for I.E.
            if ($result === null || !isset($result) || $result == '') {
                $result = '&nbsp;';
            }
            $x .= '<td>' . $result . '</td>';
        }
        $x .= '</tr>';
        if ($maxlength != -1 && $i >= $maxlength) {
            break;
        }
    }
    $x .= '</tbody>';
    //Finally close up the table.
    $x .= '</table>';
    return $x;
}
Пример #6
0
 public function calculate($infix)
 {
     $postfix = new ArrayList();
     $stack = new ArrayList();
     $i = 0;
     $temp = "";
     $type = $this->getType($infix[0]);
     $lasttype = TType::OPERATION;
     $infix = trim($infix);
     while ($i < strlen($infix)) {
         $type = $this->getType($infix[$i]);
         $temp = "";
         //Get Token name
         while ($type == $this->getType($infix[$i])) {
             $temp .= $infix[$i];
             $i++;
             if ($i == strlen($infix) || $type == TType::OPERATION || $infix[$i - 1] == '(') {
                 break;
             }
         }
         //Negatives Vorzeichen zu -1* umgeschrieben (Bsp.: -3 => (-1)*3
         if ($lasttype == TType::OPERATION && $type == TType::OPERATION) {
             if ($temp == '-') {
                 $postfix->add(new Token("-1", TType::NUMBER));
                 $temp = "*";
             } else {
                 $postfix->add(new Token("1", TType::NUMBER));
                 $temp = "*";
             }
         }
         //Fehlender Operator vor Funktionen wird ergänzt
         if ($type == TType::NUMBER && $lasttype == TType::FUNC || $type == TType::FUNC && $lasttype == TType::NUMBER) {
             $i -= strlen($temp);
             $type = TType::OPERATION;
             $temp = "*";
         }
         //Add Token to Tokenlist
         switch ($type) {
             case TType::NUMBER:
                 $postfix->add(new Token($temp, $type));
                 break;
             case TType::OPERATION:
                 for ($j = $stack->size() - 1; $j > -1; $j--) {
                     if ($this->getValue($temp) > $this->getValue($stack->get($j))) {
                         $stack->add($temp);
                         break;
                     } else {
                         $postfix->add(new Token($stack->get($j), TType::OPERATION));
                         $stack->remove($j);
                     }
                 }
                 if ($stack->size() == 0) {
                     $stack->add($temp);
                 }
                 break;
             case TType::FUNC:
                 if (in_array($temp, $this->availableFunc)) {
                     $func = new FunctionC($temp);
                     $sub = substr($infix, $i);
                     $pos = $this->getLastBracket($sub);
                     $sub = substr($sub, 0, $pos);
                     while (strpos($sub, ',') !== false) {
                         $pos2 = strpos($sub, ',');
                         if (strlen($func->term) === false && $temp == "integral(") {
                             $func->term = substr($sub, 0, $pos2);
                         } else {
                             $func->parameters->add($this->calculate(substr($sub, 0, $pos2)));
                         }
                         $sub = substr($sub, $pos2 + 1);
                     }
                     $func->parameters->add($this->calculate($sub));
                     $i += $pos + 1;
                     $postfix->add(new Token(strval($func->calculate()), TType::NUMBER));
                     $type = TType::NUMBER;
                 }
                 if ($this->variable->size() != 0) {
                     foreach ($this->variable->arrayList as $v) {
                         if ($temp == $v->name) {
                             $postfix->add(new Token(strval($v->wert), TType::NUMBER));
                             $temp = "";
                             $type = TType::NUMBER;
                         }
                     }
                 }
                 break;
         }
         $lasttype = $type;
     }
     //Add operation stack to postfix
     for ($j = $stack->size() - 1; $j > -1; $j--) {
         $postfix->add(new Token($stack->get($j), TType::OPERATION));
         $stack->remove($j);
     }
     //Calculate postfix--> result
     /* Lese alle Tokens der postfix Liste nacheinander ein.
      * Schreibe alle Zahlen in einen Stack, wird eine Operation gelesen, so führe die Operation mit den letzten
      * beiden hinzugefügten Zahlen aus, lösche die beiden Zahlen und ersetze sie mit ihrem Ergebnis
      */
     $result = 0.0;
     for ($i = 0; $i < $postfix->size(); $i++) {
         switch ($postfix->get($i)->type) {
             case TType::NUMBER:
                 $stack->add($postfix->get($i)->name);
                 break;
             case TType::OPERATION:
                 switch ($postfix->get($i)->name) {
                     case "+":
                         $result = doubleval($stack->get($stack->size() - 2)) + doubleval($stack->get($stack->size() - 1));
                         break;
                     case "-":
                         $result = doubleval($stack->get($stack->size() - 2)) - doubleval($stack->get($stack->size() - 1));
                         break;
                     case "*":
                         $result = doubleval($stack->get($stack->size() - 2)) * doubleval($stack->get($stack->size() - 1));
                         break;
                     case "/":
                         $result = doubleval($stack->get($stack->size() - 2)) / doubleval($stack->get($stack->size() - 1));
                         break;
                     case "%":
                         $result = doubleval($stack->get($stack->size() - 2)) % doubleval($stack->get($stack->size() - 1));
                         break;
                     case "^":
                         $result = pow(doubleval($stack->get($stack->size() - 2)), doubleval($stack->get($stack->size() - 1)));
                         break;
                 }
                 $stack->remove($stack->size() - 2);
                 $stack->remove($stack->size() - 1);
                 $stack->add(strval($result));
                 break;
         }
     }
     return doubleval($stack->get(0));
 }
Пример #7
0
 /**
  * This method tests the add, remove and
  * size method of the ArrayList.
  *
  * @return void
  */
 public function testAddAndRemoveAndSize()
 {
     // initialize a new ArrayList
     $list = new ArrayList();
     $this->assertEquals(0, $list->size());
     $list->add("Element 1");
     $list->add("Element 2");
     $list->add("Element 3");
     $this->assertEquals(3, $list->size());
     $list->remove(2);
     $this->assertEquals(2, $list->size());
 }
Пример #8
0
//Create our class
class MyClass
{
    public $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
}
//Create our Objects
$object1 = new MyClass('Hello');
$object2 = new MyClass('World');
$object3 = new MyClass('How');
$object4 = new MyClass('Are');
$object5 = new MyClass('You');
//Create our ArrayList
$myList = new ArrayList();
$myList->add($object1);
$myList->add($object2);
$myList->add($object3);
$myList->add($object4);
$myList->add($object5);
//Create our loop
for ($i = 0; $i < $myList->size(); $i++) {
    echo $myList[$i]->name . " ";
}
//Will Print: "Hello World How Are You "
foreach ($myList as $obj) {
    echo $obj->name . " ";
}
//Will Print: "Hello World How Are You "