|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-dev |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Dev |
|
8
|
|
|
* @subpackage Apparat\Dev\Application |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Dev\Application\Utility; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
|
40
|
|
|
use Faker\Factory; |
|
41
|
|
|
use Faker\Generator; |
|
42
|
|
|
use Faker\Provider\Biased; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Random content generator |
|
46
|
|
|
* |
|
47
|
|
|
* @package Apparat\Dev |
|
48
|
|
|
* @subpackage Apparat\Dev\Application |
|
49
|
|
|
*/ |
|
50
|
|
|
class Random |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* Random markdown sample data |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected static $markdown = null; |
|
58
|
|
|
/** |
|
59
|
|
|
* Faker instance |
|
60
|
1 |
|
* |
|
61
|
|
|
* @var Generator |
|
62
|
|
|
*/ |
|
63
|
1 |
|
protected static $faker = null; |
|
64
|
1 |
|
/** |
|
65
|
1 |
|
* Random image URL |
|
66
|
1 |
|
* |
|
67
|
1 |
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
const RANDOM_IMAGE_URL = 'http://lorempixel.com/%s/%s/'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Return a markdown sample text |
|
73
|
|
|
* |
|
74
|
|
|
* @param boolean $images Inject images |
|
75
|
|
|
* @return string Markdown sample text |
|
76
|
|
|
* @see https://jaspervdj.be/lorem-markdownum |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function markdown($images = true) |
|
79
|
|
|
{ |
|
80
|
|
|
// One time loading of markdown sample data |
|
81
|
|
|
if (self::$markdown === null) { |
|
82
|
|
|
self::$markdown = require __DIR__.DIRECTORY_SEPARATOR.'Markdown.php'; |
|
83
|
|
|
shuffle(self::$markdown); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$markdown = self::$markdown[array_rand(self::$markdown)]; |
|
87
|
|
|
|
|
88
|
|
|
// If images should be injected |
|
89
|
|
|
if ($images) { |
|
90
|
|
|
$markdown = self::injectMarkdownImages($markdown); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $markdown; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Inject a random number of images (between 0 and 3) into a markdown source |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $markdown Markdown |
|
100
|
|
|
* @return string Markdown with images |
|
101
|
|
|
*/ |
|
102
|
|
|
protected static function injectMarkdownImages($markdown) |
|
103
|
|
|
{ |
|
104
|
|
|
$markdownBlocks = preg_split('%\R{2,}%', $markdown.PHP_EOL.PHP_EOL); |
|
105
|
|
|
|
|
106
|
|
|
// Iterate through a random number of images between 0 and 3 |
|
107
|
|
|
for ($image = 0, $imageMax = 3 - self::faker()->biasedNumberBetween(0, 3); $image < $imageMax; ++$image) { |
|
108
|
|
|
$markdownBlocks[array_rand($markdownBlocks)] .= PHP_EOL.PHP_EOL.self::randomMarkdownImage(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return trim(implode(PHP_EOL.PHP_EOL, $markdownBlocks)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Create and return a random image for Markdown use |
|
116
|
|
|
* |
|
117
|
|
|
* @return string Random image markdown |
|
118
|
|
|
*/ |
|
119
|
|
|
protected static function randomMarkdownImage() |
|
120
|
|
|
{ |
|
121
|
|
|
return ', rand(150, 300)).')'; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Create and return a Faker generator |
|
126
|
|
|
* |
|
127
|
|
|
* @return Generator Faker generator |
|
128
|
|
|
*/ |
|
129
|
|
|
protected static function faker() |
|
130
|
|
|
{ |
|
131
|
|
|
if (self::$faker === null) { |
|
132
|
|
|
self::$faker = Factory::create(); |
|
133
|
|
|
self::$faker->addProvider(Kernel::create(Biased::class)); |
|
134
|
|
|
} |
|
135
|
|
|
return self::$faker; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|