Passed
Push — master ( 70faa8...7219a6 )
by Davide
02:16
created

LaravelCards::getCardSnippetOccurrences()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelCards;
4
5
class LaravelCards
6
{
7
    /*protected $postModelConfig = [];
8
9
    public function __construct()
10
    {
11
        $this->postModelConfig = config('laravel-cards.models.post');
12
    }*/
13
14
    /**************************************************************************/
15
16
    /**
17
     *  Provide the post data array (post_title, post_body, post_image).
18
     *
19
     *  @param int $postId
20
     *  @return  \DavideCasiraghi\LaravelCards\Models\Post    $ret
21
     **/
22
    public static function getPost($postId)
23
    {
24
        //$postModel = $this->postModelConfig['class'];
25
        $postModel =  config('laravel-cards.models.post.class');
26
        $ret = $postModel::where('id', $postId)->first();
27
28
        return $ret;
29
    }
30
31
    /**************************************************************************/
32
33
    /**
34
     *  Find the card snippet occurances in the text.
35
     *
36
     *  @param string $text
37
     *  @return array $matches
38
     **/
39
    public static function getCardSnippetOccurrences($text)
40
    {
41
        $re = '/{\#
42
                \h+card
43
                \h+(post_id|img_alignment|img_col_size|bkg_color|text_color|container_wrap)=\[([^]]*)]
44
                \h+((?1))=\[([^]]*)]
45
                \h+((?1))=\[([^]]*)] 
46
                \h+((?1))=\[([^]]*)]
47
                \h+((?1))=\[([^]]*)] 
48
                \h+((?1))=\[([^]]*)] 
49
                \h*\#}/x';
50
51
        if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) {
52
            return $matches;
53
        } else {
54
            return;
55
        }
56
    }
57
58
    /**************************************************************************/
59
60
    /**
61
     *  Returns the plugin parameters.
62
     *
63
     *  @param array $matches
64
     *  @return array $ret
65
     **/
66
    public static function getParameters($matches)
67
    {
68
        $ret = [];
69
70
        // Get activation string parameters (from article)
71
        $ret['token'] = $matches[0];
72
        //dump($matches);
73
74
        $ret['post_id'] = $matches[2];
75
        $ret['img_col_size_class'] = 'col-md-'.$matches[6];
76
        $textColSize = 12 - $matches[6];
77
        $ret['text_col_size_class'] = 'col-md-'.$textColSize;
78
        $backgroundColor = $matches[8];
79
        $ret['bkg_color'] = 'background-color: '.$backgroundColor.';';
80
        $textColor = $matches[10];
81
        $ret['text_color'] = 'color: '.$textColor.';';
82
        $containerWrap = $matches[12];
83
        $ret['container_wrap'] = ($containerWrap == 'true') ? 1 : 0;
84
85
        //dd($ret['bkg_color']);
86
        // Image alignment
87
        //$ret['img_alignment'] = $matches[4];
88
        $imageAlignment = $matches[4];
89
90
        switch ($imageAlignment) {
91
             case 'left':
92
                 $ret['img_col_order_class'] = 'order-md-1';
93
                 $ret['text_col_order_class'] = 'order-md-2';
94
                 break;
95
             case 'right':
96
                 $ret['img_col_order_class'] = 'order-md-2';
97
                 $ret['text_col_order_class'] = 'order-md-1';
98
                 break;
99
         }
100
101
        //dump($ret);
102
103
        return $ret;
104
    }
105
106
    /**************************************************************************/
107
108
    /**
109
     *  Prepare the card HTML.
110
     *
111
     *  @param array $parameters
112
     *  @param \DavideCasiraghi\LaravelCards\Models\Post $post
113
     *
114
     *  @return string $ret
115
     **/
116
    public static function prepareCardHtml($parameters, $post)
117
    {
118
        if (! is_null($post)) {
119
            $ret = "<div class='row featurette' style='".$parameters['bkg_color'].' '.$parameters['text_color']."'>";
120
            if ($parameters['container_wrap']) {
121
                $ret .= "<div class='container'>";
122
            }
123
            $ret .= "<div class='text ".$parameters['text_col_size_class'].' my-auto px-4 '.$parameters['text_col_order_class']."'>";
124
            $ret .= "<h2 class='featurette-heading mt-5'>".$post['title'].'</h2>';
125
            $ret .= "<div class='lead mb-4'>".$post['body'].'</div>';
126
            $ret .= '</div>';
127
            $ret .= "<div class='image ".$parameters['img_col_size_class'].' '.$parameters['img_col_order_class']."'>";
128
            if (! empty($post['post_image_src'])) {
129
                $ret .= "<img class='featurette-image img-fluid mx-auto' src='".$post['image']."' alt='".$post['image_alt']."'>";
130
            }
131
            $ret .= '</div>';
132
            if ($parameters['container_wrap']) {
133
                $ret .= '</div>';
134
            }
135
            $ret .= '</div>';
136
        } else {
137
            $ret = "<div class='alert alert-warning' role='alert'>The post with id ".$parameters['post_id'].' has not been found.</div>';
138
        }
139
140
        return $ret;
141
    }
142
143
    /**************************************************************************/
144
145
    /**
146
     *  Return the same text with the cards HTML replaced
147
     *  where the token strings has been found.
148
     *
149
     *  @param string $text
150
     *  @return string $ret
151
     **/
152
    public function replace_card_snippets_with_template($text)
153
    {
154
        $matches = self::getCardSnippetOccurrences($text);
155
156
        foreach ($matches as $key => $single_gallery_matches) {
157
            $parameters = self::getParameters($single_gallery_matches);
158
            $post = self::getPost($parameters['post_id']);
159
            $cardHtml = self::prepareCardHtml($parameters, $post);
160
161
            // Substitute the card html to the token that has been found
162
            $text = str_replace($parameters['token'], $cardHtml, $text);
163
        }
164
165
        $ret = $text;
166
167
        return $ret;
168
    }
169
}
170