2015-09-13 12:36:05 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-26 14:48:27 +01:00
|
|
|
namespace Kanboard\Helper;
|
2015-09-13 12:36:05 +02:00
|
|
|
|
2015-10-26 14:48:27 +01:00
|
|
|
use Kanboard\Core\Markdown;
|
2016-01-24 17:50:51 +01:00
|
|
|
use Kanboard\Core\Base;
|
2015-09-13 12:36:05 +02:00
|
|
|
|
|
|
|
/**
|
2016-01-24 17:50:51 +01:00
|
|
|
* Text Helpers
|
2015-09-13 12:36:05 +02:00
|
|
|
*
|
|
|
|
* @package helper
|
|
|
|
* @author Frederic Guillot
|
|
|
|
*/
|
2016-01-24 17:50:51 +01:00
|
|
|
class Text extends Base
|
2015-09-13 12:36:05 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Markdown transformation
|
|
|
|
*
|
|
|
|
* @param string $text Markdown content
|
|
|
|
* @param array $link Link parameters for replacement
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function markdown($text, array $link = array())
|
|
|
|
{
|
2016-01-24 17:50:51 +01:00
|
|
|
$parser = new Markdown($this->container, $link);
|
2015-09-13 12:36:05 +02:00
|
|
|
$parser->setMarkupEscaped(MARKDOWN_ESCAPE_HTML);
|
|
|
|
return $parser->text($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a file size
|
|
|
|
*
|
|
|
|
* @param integer $size Size in bytes
|
|
|
|
* @param integer $precision Precision
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function bytes($size, $precision = 2)
|
|
|
|
{
|
|
|
|
$base = log($size) / log(1024);
|
|
|
|
$suffixes = array('', 'k', 'M', 'G', 'T');
|
|
|
|
|
|
|
|
return round(pow(1024, $base - floor($base)), $precision).$suffixes[(int)floor($base)];
|
|
|
|
}
|
|
|
|
|
2016-03-07 23:38:10 +01:00
|
|
|
/**
|
|
|
|
* Get the number of bytes from PHP size
|
|
|
|
*
|
|
|
|
* @param integer $val PHP size (example: 2M)
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function phpToBytes($val)
|
|
|
|
{
|
|
|
|
$val = trim($val);
|
|
|
|
$last = strtolower($val[strlen($val)-1]);
|
|
|
|
|
|
|
|
switch ($last) {
|
|
|
|
case 'g':
|
|
|
|
$val *= 1024;
|
|
|
|
case 'm':
|
|
|
|
$val *= 1024;
|
|
|
|
case 'k':
|
|
|
|
$val *= 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
2015-09-13 12:36:05 +02:00
|
|
|
/**
|
|
|
|
* Return true if needle is contained in the haystack
|
|
|
|
*
|
|
|
|
* @param string $haystack Haystack
|
|
|
|
* @param string $needle Needle
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function contains($haystack, $needle)
|
|
|
|
{
|
|
|
|
return strpos($haystack, $needle) !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a value from a dictionary
|
|
|
|
*
|
|
|
|
* @param mixed $id Key
|
|
|
|
* @param array $listing Dictionary
|
|
|
|
* @param string $default_value Value displayed when the key doesn't exists
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function in($id, array $listing, $default_value = '?')
|
|
|
|
{
|
|
|
|
if (isset($listing[$id])) {
|
|
|
|
return $this->helper->e($listing[$id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default_value;
|
|
|
|
}
|
|
|
|
}
|