Completed
Push — master ( 337d70...6470c4 )
by Mihail
04:13
created

Text::cut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace Ffcms\Core\Helper;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Helper\Type\Str;
8
9
class Text
10
{
11
    const WYSIWYG_BREAK_HTML = '<div style="page-break-after: always">';
12
13
    /**
14
     * Make snippet from text or html-text content.
15
     * @param string $text
16
     * @param int $length
17
     * @return string
18
     */
19
    public static function snippet($text, $length = 150)
20
    {
21
        // check if valid string is passed
22
        if (!Obj::isString($text)) {
23
            return null;
24
        }
25
26
        $breakerPos = mb_strpos($text, self::WYSIWYG_BREAK_HTML, null, 'UTF-8');
27
        // offset is founded, try to split preview from full text
28
        if ($breakerPos !== false) {
29
            $text = Str::sub($text, 0, $breakerPos);
30
        } else { // page breaker is not founded, lets get a fun ;D
31
            // find first paragraph ending
32
            $breakerPos = mb_strpos($text, '</p>', null, 'UTF-8');
33
            // no paragraph's ? lets try to get <br[\/|*]>
34
            if ($breakerPos === false) {
35
                $breakerPos = mb_strpos($text, '<br', null, 'UTF-8');
36
            } else {
37
                // add length('</p>')
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
                $breakerPos+= 4;
39
            }
40
            // cut text from position caret before </p> (+4 symbols to save item as valid)
41
            if ($breakerPos !== false) {
42
                $text = Str::sub($text, 0, $breakerPos);
43
            }
44
        }
45
46
        // if breaker position is still undefined - lets make 'text cut' for defined length and remove all html tags
47
        if ($breakerPos === false) {
48
            $text = strip_tags($text);
49
            $text = self::cut($text, 0, $length);
50
        }
51
        return $text;
52
    }
53
54
    /**
55
     * Cut text starting from $start pointer to $end pointer in UTF-8 mod
56
     * @param string $text
57
     * @param int $start
58
     * @param int $end
59
     * @return string
60
     */
61
    public static function cut($text, $start = 0, $end = 0)
62
    {
63
        return Str::sub($text, $start, $end);
64
    }
65
66
}