Пример #1
0
 public function testEach()
 {
     $collection = new Collection(array(1, 2, 3, 4));
     $test = array();
     $collection->each(function ($value) use(&$test) {
         $test[] = $value * 2;
     });
     $this->assertEquals(array(2, 4, 6, 8), $test);
 }
Пример #2
0
 /**
  * @param string|object $extension
  *
  * @return boolean
  */
 public function hasExtension($extension)
 {
     if ($extension instanceof ExtensionInterface) {
         $extension = $extension->getName();
     }
     return $this->extensions->exists($extension);
 }
Пример #3
0
 /**
  * @param Text       $body
  * @param Collection $baseTags
  *
  * @return Collection
  */
 protected function parseBody(Text $body, Collection $baseTags)
 {
     $rows = new Collection();
     $body->split('/\\n/')->each(function (Text $row, $index) use($baseTags, &$rows) {
         $row->trim()->trim('|');
         $cells = new Collection();
         try {
             $row->split('/\\|/')->each(function (Text $cell, $index) use(&$baseTags, &$cells) {
                 /* @var Tag $tag */
                 $tag = clone $baseTags->get($index);
                 $this->markdown->emit('inline', array($cell));
                 $tag->setText($cell->trim());
                 $cells->add($tag);
             });
         } catch (\OutOfBoundsException $e) {
             throw new SyntaxError(sprintf('Too much cells on table body (row #%d).', $index), $this, $row, $this->markdown, $e);
         }
         if ($baseTags->count() != $cells->count()) {
             throw new SyntaxError('Unexpected number of table cells in body.', $this, $row, $this->markdown);
         }
         $rows->add($cells);
     });
     return $rows;
 }
 /**
  * Make links out of things like `<http://example.com/>`
  *
  * @param Text $text
  */
 public function processAutoLink(Text $text)
 {
     if (!$text->contains('<')) {
         return;
     }
     $text->replace('{<((?:https?|ftp):[^\'">\\s]+)>}', function (Text $w, Text $url) {
         $this->markdown->emit('escape.special_chars', [$url->replace('/(?<!\\\\)_/', '\\\\_')]);
         return $this->getRenderer()->renderLink($url, ['href' => $url->getString()]);
     });
     /** @noinspection PhpUnusedParameterInspection */
     $text->replace('{
         <
         (?:mailto:)?
         (
             [-.\\w]+
             \\@
             [-a-z0-9]+(\\.[-a-z0-9]+)*\\.[a-z]+
         )
         >
     }ix', function (Text $w, Text $address) {
         $address = "mailto:" . $address;
         $encode = array(function ($char) {
             return '&#' . ord($char) . ';';
         }, function ($char) {
             return '&#x' . dechex(ord($char)) . ';';
         }, function ($char) {
             return $char;
         });
         $chars = new Collection(str_split($address));
         $chars->apply(function ($char) use($encode) {
             if ($char == '@') {
                 return $encode[rand(0, 1)]($char);
             } elseif ($char != ':') {
                 $rand = rand(0, 100);
                 $key = $rand > 90 ? 2 : ($rand < 45 ? 0 : 1);
                 return $encode[$key]($char);
             }
             return $char;
         });
         $address = $chars->join();
         $text = $chars->slice(7)->join();
         return $this->getRenderer()->renderLink($text, ['href' => $address]);
     });
 }