Exemple #1
0
 public function testRandomString()
 {
     $re = '|^[A-Za-z][A-Za-z0-9]*$|';
     $this->assertTrue(preg_match($re, $s = FWU::randomString()));
     $this->assertEqual(strlen($s), 40);
     $this->assertTrue(preg_match($re, $s = FWU::randomString(1)));
     $this->assertEqual(strlen($s), 1);
 }
Exemple #2
0
 private static function _parse($content)
 {
     // create DOMDocument instance
     $dom = new DOMDocument();
     if (!$dom->loadXML('<root>' . $content . '</root>')) {
         throw new Exception('menu plugin: PHP DOM couldn\'t parse menu XML.');
     }
     $dom->normalizeDocument();
     // process <a> elements
     $dom_links = $dom->getElementsByTagName('a');
     $links = array();
     foreach ($dom_links as $dom_link) {
         // get `l' attribute
         if ($dom_link->hasAttribute('l')) {
             $link_id = trim($dom_link->getAttribute('l'));
             if (!$link_id) {
                 throw new Exception('menu plugin: link id cannot be empty.');
             }
             $dom_link->removeAttribute('l');
         } else {
             $link_id = null;
         }
         // add placeholders
         $placeholders = array();
         $placeholders['class'] = '--' . FWU::randomString() . '--';
         if ($dom_link->hasAttribute('class')) {
             $dom_link->setAttribute('class', trim($dom_link->getAttribute('class') . ' ' . $placeholders['class']));
         } else {
             $dom_link->setAttribute('class', $placeholders['class']);
         }
         if (!$dom_link->hasAttribute('id')) {
             $placeholders['id'] = '--' . FWU::randomString() . '--';
             $dom_link->setAttribute('id', $placeholders['id']);
         } else {
             $placeholders['id'] = null;
         }
         // add to $links array
         $links[] = array('id' => $link_id, 'placeholders' => $placeholders);
     }
     // done
     return array('links' => $links, 'html' => preg_replace('|^\\s*<\\?xml version="1.0"\\?>\\s*<root>(.*)</root>\\s*$|s', '$1', $dom->saveXML()));
 }