TextHelper::markdownAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Helper;
13
14
use Jitamin\Foundation\Base;
15
use Jitamin\Foundation\Markdown;
16
17
/**
18
 * Text Helpers.
19
 */
20
class TextHelper extends Base
21
{
22
    /**
23
     * HTML escaping.
24
     *
25
     * @param string $value Value to escape
26
     *
27
     * @return string
28
     */
29
    public function e($value)
30
    {
31
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
32
    }
33
34
    /**
35
     * Markdown transformation.
36
     *
37
     * @param string $text
38
     * @param bool   $isPublicLink
39
     *
40
     * @return string
41
     */
42
    public function markdown($text, $isPublicLink = false)
43
    {
44
        $parser = new Markdown($this->container, $isPublicLink);
45
        $parser->setMarkupEscaped(MARKDOWN_ESCAPE_HTML);
46
47
        return $parser->text($text);
48
    }
49
50
    /**
51
     * Escape Markdown text that need to be stored in HTML attribute.
52
     *
53
     * @param string $text
54
     *
55
     * @return mixed
56
     */
57
    public function markdownAttribute($text)
58
    {
59
        return htmlentities($this->markdown($text), ENT_QUOTES, 'UTF-8');
60
    }
61
62
    /**
63
     * Format a file size.
64
     *
65
     * @param int $size      Size in bytes
66
     * @param int $precision Precision
67
     *
68
     * @return string
69
     */
70
    public function bytes($size, $precision = 2)
71
    {
72
        $base = log($size) / log(1024);
73
        $suffixes = ['', 'k', 'M', 'G', 'T'];
74
75
        return round(pow(1024, $base - floor($base)), $precision).$suffixes[(int) floor($base)];
76
    }
77
78
    /**
79
     * Get the number of bytes from PHP size.
80
     *
81
     * @param int $val PHP size (example: 2M)
82
     *
83
     * @return int
84
     */
85
    public function phpToBytes($val)
86
    {
87
        $val = trim($val);
88
        $last = strtolower($val[strlen($val) - 1]);
89
90
        switch ($last) {
91
            case 'g':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
92
                $val *= 1024;
93
            case 'm':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
94
                $val *= 1024;
95
            case 'k':
96
                $val *= 1024;
97
        }
98
99
        return $val;
100
    }
101
102
    /**
103
     * Return true if needle is contained in the haystack.
104
     *
105
     * @param string $haystack Haystack
106
     * @param string $needle   Needle
107
     *
108
     * @return bool
109
     */
110
    public function contains($haystack, $needle)
111
    {
112
        return strpos($haystack, $needle) !== false;
113
    }
114
115
    /**
116
     * Return a value from a dictionary.
117
     *
118
     * @param mixed  $id            Key
119
     * @param array  $listing       Dictionary
120
     * @param string $default_value Value displayed when the key doesn't exists
121
     *
122
     * @return string
123
     */
124
    public function in($id, array $listing, $default_value = '?')
125
    {
126
        if (isset($listing[$id])) {
127
            return $this->helper->text->e($listing[$id]);
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Helper\TextHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
        }
129
130
        return $default_value;
131
    }
132
}
133