Maslosoft Zamm API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/zamm
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/zamm/
*/
namespace Maslosoft\Zamm\Helpers;
/**
* Wrapper
* @property Wrapper $md Get markdown wrapped code
* @property Wrapper $html Get html `pre` wrapped code
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class InlineWrapper
{
protected $setup = false;
private $title = '';
private $text = '';
private $link = '';
private $isMd = false;
private $isHtml = false;
private $isRaw = false;
private static $def = [];
public function __construct($text = '', $link = '', $title = '')
{
$this->text = $text;
$this->link = $link;
$this->title = $title;
foreach (self::$def as $flag => $value)
{
$this->$flag = $value;
}
}
/**
* Setup defaults
*
* @see https://github.com/Maslosoft/Zamm/issues/4
* @return static
*/
public static function defaults()
{
$wrapper = new static;
$wrapper->setup = true;
return $wrapper;
}
public function __get($name)
{
return $this->$name();
}
public function md($value = true)
{
$this->setFlag('isMd', $value);
$this->setFlag('isHtml', !$value);
return $this;
}
public function html($value = true)
{
$this->setFlag('isHtml', $value);
$this->setFlag('isMd', !$value);
return $this;
}
/**
* Strip leading $ from variables and trailing () from methods
* @return InlineWrapper
*/
public function raw($value = true)
{
$this->setFlag('isRaw', $value);
return $this;
}
public function __toString()
{
if ($this->isRaw)
{
$text = trim($this->text, '$()');
}
else
{
$text = $this->text;
}
if ($this->isMd)
{
return $this->wrap("`$text`");
}
if ($this->isHtml)
{
return $this->wrap("<code>$text</code>");
}
return $this->wrap($this->text);
}
private function wrap($text)
{
if ($this->link)
{
if ($this->isMd)
{
return sprintf('[%s](%s "%s")', $text, $this->link, $this->title);
}
return sprintf('<a href="%s" class="api-link" title="%s">%s</a>', $this->link, $this->title, $text);
}
return $text;
}
private function setFlag($flag, $value = true)
{
$this->$flag = $value;
if ($this->setup)
{
self::$def[$flag] = $value;
}
}
}
API documentation generated by ApiGen