Completed
Push — master ( 62dcb4...b3a7fb )
by Davide
07:56 queued 05:40
created

replace_card_snippets_with_template()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
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 function getPost($postId)
23
    {
24
        $postModel = $this->postModelConfig['class'];
25
        $ret = $postModel::where('id', $postId)->first();
26
27
        return $ret;
28
    }
29
30
    /**************************************************************************/
31
32
    /**
33
     *  Find the card snippet occurances in the text.
34
     *
35
     *  @param string $text
36
     *  @return array $matches
37
     **/
38
    public function getCardSnippetOccurrences($text)
39
    {
40
        $re = '/{\#
41
                \h+card
42
                \h+(post_id|img_alignment|img_col_size|bkg_color|text_color|container_wrap)=\[([^]]*)]
43
                \h+((?1))=\[([^]]*)]
44
                \h+((?1))=\[([^]]*)] 
45
                \h+((?1))=\[([^]]*)]
46
                \h+((?1))=\[([^]]*)] 
47
                \h+((?1))=\[([^]]*)] 
48
                \h*\#}/x';
49
50
        if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) {
51
            return $matches;
52
        } else {
53
            return;
54
        }
55
    }
56
57
    /**************************************************************************/
58
59
    /**
60
     *  Returns the plugin parameters.
61
     *
62
     *  @param array $matches
63
     *  @return array $ret
64
     **/
65
    public function getParameters($matches)
66
    {
67
        $ret = [];
68
69
        // Get activation string parameters (from article)
70
        $ret['token'] = $matches[0];
71
        //dump($matches);
72
73
        $ret['post_id'] = $matches[2];
74
        $ret['img_col_size_class'] = 'col-md-'.$matches[6];
75
        $textColSize = 12 - $matches[6];
76
        $ret['text_col_size_class'] = 'col-md-'.$textColSize;
77
        $backgroundColor = $matches[8];
78
        $ret['bkg_color'] = 'background-color: '.$backgroundColor.';';
79
        $textColor = $matches[10];
80
        $ret['text_color'] = 'color: '.$textColor.';';
81
        $containerWrap = $matches[12];
82
        $ret['container_wrap'] = ($containerWrap == 'true') ? 1 : 0;
83
84
        //dd($ret['bkg_color']);
85
        // Image alignment
86
        //$ret['img_alignment'] = $matches[4];
87
        $imageAlignment = $matches[4];
88
89
        switch ($imageAlignment) {
90
             case 'left':
91
                 $ret['img_col_order_class'] = 'order-md-1';
92
                 $ret['text_col_order_class'] = 'order-md-2';
93
                 break;
94
             case 'right':
95
                 $ret['img_col_order_class'] = 'order-md-2';
96
                 $ret['text_col_order_class'] = 'order-md-1';
97
                 break;
98
         }
99
100
        //dump($ret);
101
102
        return $ret;
103
    }
104
105
    /**************************************************************************/
106
107
    /**
108
     *  Prepare the card HTML.
109
     *
110
     *  @param array $parameters
111
     *  @param \DavideCasiraghi\LaravelCards\Models\Post $post
112
     *
113
     *  @return string $ret
114
     **/
115
    public function prepareCardHtml($parameters, $post)
116
    {
117
        if (! is_null($post)) {
118
            $ret = "<div class='row featurette' style='".$parameters['bkg_color'].' '.$parameters['text_color']."'>";
119
            if ($parameters['container_wrap']) {
120
                $ret .= "<div class='container'>";
121
            }
122
            $ret .= "<div class='text ".$parameters['text_col_size_class'].' my-auto px-4 '.$parameters['text_col_order_class']."'>";
123
            $ret .= "<h2 class='featurette-heading mt-5'>".$post['title'].'</h2>';
124
            $ret .= "<div class='lead mb-4'>".$post['body'].'</div>';
125
            $ret .= '</div>';
126
            $ret .= "<div class='image ".$parameters['img_col_size_class'].' '.$parameters['img_col_order_class']."'>";
127
            if (! empty($post['post_image_src'])) {
128
                $ret .= "<img class='featurette-image img-fluid mx-auto' src='".$post['image']."' alt='".$post['image_alt']."'>";
129
            }
130
            $ret .= '</div>';
131
            if ($parameters['container_wrap']) {
132
                $ret .= '</div>';
133
            }
134
            $ret .= '</div>';
135
        } else {
136
            $ret = "<div class='alert alert-warning' role='alert'>The post with id ".$parameters['post_id'].' has not been found.</div>';
137
        }
138
139
        return $ret;
140
    }
141
142
    /**************************************************************************/
143
144
    /**
145
     *  Return the same text with the cards HTML replaced
146
     *  where the token strings has been found.
147
     *
148
     *  @param string $text
149
     *  @return string $ret
150
     **/
151
    public function replace_card_snippets_with_template($text)
152
    {
153
        $matches = self::getCardSnippetOccurrences($text);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelC...ardSnippetOccurrences() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        /** @scrutinizer ignore-call */ 
154
        $matches = self::getCardSnippetOccurrences($text);
Loading history...
154
155
        foreach ($matches as $key => $single_gallery_matches) {
156
            $parameters = self::getParameters($single_gallery_matches);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelC...lCards::getParameters() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
            /** @scrutinizer ignore-call */ 
157
            $parameters = self::getParameters($single_gallery_matches);
Loading history...
157
            $post = self::getPost($parameters['post_id']);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelC...LaravelCards::getPost() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
            /** @scrutinizer ignore-call */ 
158
            $post = self::getPost($parameters['post_id']);
Loading history...
158
            $cardHtml = self::prepareCardHtml($parameters, $post);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelC...ards::prepareCardHtml() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
            /** @scrutinizer ignore-call */ 
159
            $cardHtml = self::prepareCardHtml($parameters, $post);
Loading history...
159
160
            // Substitute the card html to the token that has been found
161
            $text = str_replace($parameters['token'], $cardHtml, $text);
162
        }
163
164
        $ret = $text;
165
166
        return $ret;
167
    }
168
}
169