Issues (8)

src/LaravelJumbotronImages.php (4 issues)

1
<?php
2
3
namespace DavideCasiraghi\LaravelJumbotronImages;
4
5
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage;
6
7
class LaravelJumbotronImages
8
{
9
    /**
10
     * Return a Jumbotron image.
11
     *
12
     * @param  int  $jumbotronImageId
13
     * @return \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage
14
     */
15
    /*public function getJumbotronImage($jumbotronImageId)
16
    {
17
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
18
        $jumbotronImage->parameters = $this->getParametersArray($jumbotronImage);
19
20
        $ret = $jumbotronImage;
21
22
        return $ret;
23
    }*/
24
25
    /***************************************************************************/
26
27
    /**
28
     * Show a Jumbotron Image.
29
     *
30
     * @param  int  $jumbotronImageId
31
     * @return \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage
32
     */
33 3
    public function showJumbotronImage($jumbotronImageId)
34
    {
35 3
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
36
37
        //$jumbotronImageParameters = $this->getParametersArray($jumbotronImage);
38 3
        $jumbotronImageParameters = ($jumbotronImage) ? $this->getParametersArray($jumbotronImage) : null;
39
40 3
        return view('laravel-jumbotron-images::show-jumbotron-image', compact('jumbotronImage'))
41 3
            ->with('jumbotronImageParameters', $jumbotronImageParameters);
42
    }
43
44
    /***************************************************************************/
45
46
    /**
47
     * Return an array with the parameters for the show-jumbotron-image view.
48
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage  $jumbotronImage
49
     * @return array
50
     */
51 3
    public static function getParametersArray($jumbotronImage)
52
    {
53
        $ret = [
54 3
            'cover_opacity' => 'opacity: '.$jumbotronImage->cover_opacity.';',
55 3
            'background_color' => 'background: #'.$jumbotronImage->background_color.';',
56 3
            'image' => 'background-image:url(/storage/images/jumbotron_images/'.$jumbotronImage->image_file_name.');',
57 3
            'text_horizontal_alignment' => 'text-align: '.$jumbotronImage->text_horizontal_alignment.';',
58
        ];
59 3
        $ret['white_moon'] = ($jumbotronImage->white_moon == 1) ? ' moon-curve ' : '';
60 3
        $ret['scroll_down_arrow'] = ($jumbotronImage->scroll_down_arrow == 1) ? "<div class='scroll-arrow white'><span>SCROLL DOWN</span><img src='/vendor/laravel-jumbotron-images/assets/images/angle-down-regular.svg'></div>" : '';
61
62
        /* Parallax - The element is defined with stellar plugin like: <section class="parallax" data-stellar-background-ratio="0.5" ><span>Summer</span></section>*/
63 3
        $ret['parallax'] = ($jumbotronImage->parallax == 1) ? ' parallax' : '';
64 3
        $ret['parallax_ratio'] = ($jumbotronImage->parallax == 1) ? "data-stellar-background-ratio='0.5'" : '';
65
66
        /* Text Width */
67 3
        if ($jumbotronImage->text_width != 100) {
68 3
            switch ($jumbotronImage->text_horizontal_alignment) {
69 3
                case 'left':	// Left
70 1
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%;';
71 1
                break;
72 3
                case 'center': // Center
73 2
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%; margin: auto;';
74 2
                break;
75 2
                case 'right': // Right
76 1
                    $ret['text_width'] = 'width: '.$jumbotronImage->text_width.'%; float: right;';
77 1
                break;
78
            }
79
        }
80
81 3
        return $ret;
82
    }
83
84
    /**************************************************************************/
85
86
    /**
87
     *  Find the card snippet occurances in the text.
88
     *
89
     *  @param string $text
90
     *  @return array $matches
91
     **/
92 2
    public static function getJumbotronSnippetOccurrences($text)
93
    {
94 2
        $re = '/{\#
95
                \h+jumbotron
96
                \h+(id)=\[([^]]*)]
97
                \h*\#}/x';
98
99 2
        if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) {
100 2
            return $matches;
101
        } else {
102 1
            return;
103
        }
104
    }
105
106
    /**************************************************************************/
107
108
    /**
109
     *  Return the same text with the jumbotrons HTML replaced
110
     *  where the token strings has been found.
111
     *
112
     *  @param string $text
113
     *  @return string $ret
114
     **/
115 1
    public function replaceJumbotronSnippetsWithTemplate($text)
116
    {
117 1
        $matches = self::getJumbotronSnippetOccurrences($text);
118
119 1
        if ($matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
120 1
            foreach ($matches as $key => $single_jumbotron_matches) {
121 1
                $snippetParameters = self::getSnippetParameters($single_jumbotron_matches);
122 1
                $jumbotron = self::getJumbotron($snippetParameters['jumbotron_id']);
0 ignored issues
show
The assignment to $jumbotron is dead and can be removed.
Loading history...
123 1
                $jumbotronView = self::showJumbotronImage($snippetParameters['jumbotron_id']);
0 ignored issues
show
Bug Best Practice introduced by
The method DavideCasiraghi\LaravelJ...s::showJumbotronImage() 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

123
                /** @scrutinizer ignore-call */ 
124
                $jumbotronView = self::showJumbotronImage($snippetParameters['jumbotron_id']);
Loading history...
124 1
                $jumbotronHtml = $jumbotronView->render();
125
126
                // Substitute the jumbotron html to the token that has been found
127 1
                $text = str_replace($snippetParameters['token'], $jumbotronHtml, $text);
0 ignored issues
show
It seems like $jumbotronHtml can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $replace of str_replace() does only seem to accept string|string[], maybe add an additional type check? ( Ignorable by Annotation )

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

127
                $text = str_replace($snippetParameters['token'], /** @scrutinizer ignore-type */ $jumbotronHtml, $text);
Loading history...
128
            }
129
        }
130
131 1
        $ret = $text;
132
133 1
        return $ret;
134
    }
135
136
    /**************************************************************************/
137
138
    /**
139
     *  Provide the post data array (post_title, post_body, post_image).
140
     *
141
     *  @param int $jumbotronId
142
     *  @return  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage    $ret
143
     **/
144 2
    public static function getJumbotron($jumbotronId)
145
    {
146 2
        $ret = JumbotronImage::where('id', $jumbotronId)->first();
147
148 2
        return $ret;
149
    }
150
151
    /**************************************************************************/
152
153
    /**
154
     *  Returns the snippet parameters.
155
     *
156
     *  @param array $matches
157
     *  @return array $ret
158
     **/
159 2
    public static function getSnippetParameters($matches)
160
    {
161 2
        $ret = [];
162
163
        // Get activation string parameters (from article)
164 2
        $ret['token'] = $matches[0];
165
        //dump($matches);
166 2
        $ret['jumbotron_id'] = $matches[2];
167
168
        //dump($ret);
169 2
        return $ret;
170
    }
171
}
172