forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamplePageUtils.php
More file actions
405 lines (357 loc) · 10.3 KB
/
Copy pathSamplePageUtils.php
File metadata and controls
405 lines (357 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Samples;
use Cloudinary\Asset\Image;
use Cloudinary\Transformation\Format;
/**
* @param $url
* @param $transformation
*
* @return string
*/
function getFormattedUrl($url, $transformation)
{
$urlStart = substr($url, 0, strPosX($url, '/', 4) + 1);
$urlEnd = str_replace($urlStart, '', $url);
$urlFolders = substr($urlEnd, 0, strPosX($urlEnd, '/', 2) + 1);
$urlEnd = str_replace($urlStart . $urlFolders . $transformation, '', $url);
return '<span class="url-start">' . $urlStart . '</span>' .
'<span class="url-folders">' . $urlFolders . '</span>' .
'<span class="url-transformation">' . $transformation . '</span>' .
'<span class="url-end">' . $urlEnd . '</span>';
}
/**
* @param $html
*
* @return string
*/
function getFormattedHtml($html)
{
return '<span class="html-tag">' . htmlspecialchars($html) . '</span>';
}
/**
* @param $code
*
* @param bool $keepSpaces
*
* @return bool|mixed|string
*/
function getColoredPhp($code, $keepSpaces = true)
{
$phpStr = [
'<span style="color: #FFFFFF"><?php</span>',
'<span style="color: #FFFFFF"><?php </span>',
'<?php ',
];
$result = highlight_string('<?php ' . $code, true);
if (! $keepSpaces) {
$result = strip_tags($result, '<span>');
$result = removeDoubleSpaces($result);
}
$result = str_replace($phpStr, '', $result);
if ($keepSpaces) {
return '<pre>' . $result . '</pre>';
}
return $result;
}
/**
* Find the position of the Xth occurrence of a substring in a string
*
* @param $haystack
* @param $needle
* @param int $number integer > 0
*
* @return int|false
*/
function strPosX($haystack, $needle, $number)
{
if ($number == 1) {
return strpos($haystack, $needle);
}
if ($number > 1) {
return strpos($haystack, $needle, strPosX($haystack, $needle, $number - 1) + strlen($needle));
}
return false;
}
/**
* @param $str
*
* @return mixed
*/
function removeDoubleSpaces($str)
{
while (strpos($str, ' ') !== false) {
$str = str_replace(' ', ' ', $str);
}
return $str;
}
/**
* @param $title
* @param $description
*
* @return string
*/
function getPageStart($title, $description)
{
return '
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content=' . $description . '>
<meta name="author" content="Cloudinary">
<title>' . $title . '</title>
<meta content="Learn how to upload files with only a few lines of PHP code, including cloud storage,
CDN delivery, and dynamic effects for images and media."
name="description">
<link href="https://fonts.googleapis.com/css?family=Roboto:500,400italic,300,700,500italic,400"
rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,500,700" rel="stylesheet">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf"
crossorigin="anonymous">
<link rel="stylesheet" href="styles.css">
<link rel="shortcut icon"
href="' . Image::fetch('https://cloudinary.com/favicon.png')->format(Format::ico()) . '"/>
</head>
<body>
<div class="d-flex" id="wrapper">
';
}
function getPageEnd()
{
return '
</div>
</body>
</html>
';
}
function getNavLinks($navLinks, $currentNavLink)
{
$result = '<ul class="nav nav-tabs h-100 bg-light" style="border-bottom:0">';
foreach ($navLinks as $index => $link) {
$url = $link['url'];
$text = $link['text'];
$active = $index == $currentNavLink ? ' active' : '';
$color = $index == $currentNavLink ? '#FFF' : '#000';
$result .= "<li class='nav-item'>
<a class='nav-link h-100 $active' style='color: $color; padding: .9rem 1rem' href='$url'>$text
</a>
</li>";
}
$result .= '</ul>';
return $result;
}
function getNavBar($navLinks, $currentNavLink)
{
return '
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom w-100 fixed-top pb-0">
<div class="humburger">
<button class="btn p-0 m-0" type="button" id="menu-toggle">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="col p-0 m-0" style="max-width:185px;">
<a href="https://cloudinary.com">
<img
src="https://cloudinary-res.cloudinary.com/image/upload/cloudinary_logo_for_white_bg.svg"
alt="Cloudinary"
/>
</a>
</div>
<div class="col p-0 m-0">
' . getNavLinks($navLinks, $currentNavLink) .
'</div>
</nav>';
}
function getContent($title, $groups, $htmlContent)
{
return '
<div id="page-content-wrapper" class="mt-5">
<div id="top" class="container ml-4 mt-4">
<div class="row">
<span class="icon-holder">
<span class="icon framework-php"></span>
</span>
<h1 class="mt-4 mb-5">' . $title . '</h1>
</div>
<main class="main">
<div>' .
$htmlContent . '
</div>' .
groupsToString($groups) . '
</main>
</div>
</div>';
}
function getSideBar($groups)
{
return '
<!-- Sidebar -->
<div class="border-right fixed" id="sidebar-wrapper">
<div class="sidebar-heading bg-light mt-6">
<a href="#top">
Cloudinary PHP SDK v2
</a>
</div>
<div class="sidebar-groups">' .
getSideBarGroups($groups) .
'</div>
</div>
<!-- /#sidebar-wrapper -->
';
}
function getSideBarGroups($groups)
{
$result = '';
foreach ($groups as $group) {
$name = $group['name'];
$icon = $group['iconClass'];
//fas fa-camera
//fas fa-video
$result .= '
<div class="d-flex border-top side-group-title-container align-items-center" >
<a class="ml-2 side-group-title">' . $name . ' <i class="' . $icon . '"></i></a>
</div >' .
getSideBarSubGroups($group['items']);
}
return $result;
}
/**
* @param $subGroups
*
* @return string
*/
function getSideBarSubGroups($subGroups)
{
$result = '';
foreach ($subGroups as $subGroup) {
$name = $subGroup['name'];
$trimmedName = getTrimmedStr($name);
$result .= '
<div class="ml-4">
<a href="#' . $trimmedName . '">
' . $name . '
</a>
</div>
';
}
return $result;
}
function getPageScripts($tabs = ['php', 'url'])
{
$result = '
<!-- Bootstrap core JavaScript -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<!-- Menu Toggle Script -->
<script>
$("#menu-toggle").click(function (e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
$("#page-content-wrapper").toggleClass("toggled");
});';
foreach ($tabs as $tab) {
$name = '.' . $tab . '-tab';
$result .= '
$(\'' . $name . '\').click(function (e) {
e.preventDefault();
$(".url-content").toggleClass("show active");
$(".php-content").toggleClass("show active");
$(".url-tab").toggleClass("active");
$(".php-tab").toggleClass("active");
});';
}
$result .= '
</script >';
return $result;
}
function groupsToString($groups)
{
$result = '';
foreach ($groups as $group) {
$result .= groupToString($group);
}
return $result;
}
/**
* @param $group
*
* @return string
*/
function groupToString($group)
{
$result = '';
foreach ($group['items'] as $subGroup) {
$result .= subGroupToString($subGroup);
}
return $result;
}
/**
* @param $str
*
* @return string|string[]|null
*/
function getTrimmedStr($str)
{
return preg_replace('/\s+/', '', (preg_replace('/[^A-Za-z0-9 ]/', '', $str)));
}
/**
* @param $subGroup
*
* @return string
*/
function subGroupToString($subGroup)
{
$trimmedName = getTrimmedStr($subGroup['name']);
$result = '<div>
<div id="' . $trimmedName . '" class="pt-5">
<h2 class="modal-title mb-0">
' . $subGroup['name'] . '
</h2>
</div>
<div>';
foreach ($subGroup['items'] as $sample) {
$result .= $sample;
}
$result .= '
</div>
</div>';
return $result;
}
/**
* @return string
*/
function getSampleTabs()
{
$result = '
<div class="row pb-5" >
<div class="col-12 tabs" >
<!-- Nav pills -->
<div class="col-12 tabs">
<div>
<ul class="nav nav-pills" role="tablist">';
foreach ($this->tabs as $key => $value) {
$result .= '
<li class="nav-item">
<a class="url-tab nav-link ' . ($key < 1 ? 'active' : '') . '" role="tab" aria-controls="' . $value .
'" aria-selected="' . ($key < 1 ? 'true' : 'false') . '">' . strtoupper($value) . '</a>
</li>
';
}
$result .= '
</ul>
</div>';
return $result;
}