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
|
|
|
* |
61
|
|
|
* @var Generator |
62
|
|
|
*/ |
63
|
|
|
protected static $faker = null; |
64
|
|
|
/** |
65
|
|
|
* Random image URL |
66
|
|
|
* |
67
|
|
|
* @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
|
2 |
|
public static function markdown($images = true) |
79
|
|
|
{ |
80
|
|
|
// One time loading of markdown sample data |
81
|
2 |
|
if (self::$markdown === null) { |
82
|
1 |
|
self::$markdown = require __DIR__.DIRECTORY_SEPARATOR.'Markdown.php'; |
83
|
1 |
|
shuffle(self::$markdown); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
2 |
|
$markdown = self::$markdown[array_rand(self::$markdown)]; |
87
|
|
|
|
88
|
|
|
// If images should be injected |
89
|
2 |
|
if ($images) { |
90
|
2 |
|
$markdown = self::injectMarkdownImages($markdown); |
91
|
2 |
|
} |
92
|
|
|
|
93
|
2 |
|
return $markdown; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return a random apparat URL with a particular type |
98
|
|
|
* |
99
|
|
|
* @param string $objectType Object type |
100
|
|
|
* @return string Random apparat URL |
101
|
|
|
*/ |
102
|
1 |
|
public static function apparatUrl($objectType) |
103
|
|
|
{ |
104
|
1 |
|
$randomDatetime = self::faker()->dateTime; |
105
|
|
|
$dateParts = [ |
106
|
1 |
|
$randomDatetime->format('Y'), |
107
|
1 |
|
$randomDatetime->format('m'), |
108
|
1 |
|
$randomDatetime->format('d'), |
109
|
1 |
|
$randomDatetime->format('H'), |
110
|
1 |
|
$randomDatetime->format('i'), |
111
|
1 |
|
$randomDatetime->format('s'), |
112
|
1 |
|
]; |
113
|
1 |
|
$urlParts = array_slice($dateParts, 0, intval(getenv('OBJECT_DATE_PRECISION'))); |
114
|
1 |
|
$objectId = rand(1, 1000); |
115
|
1 |
|
$urlParts[] = $objectId.'-'.$objectType; |
116
|
1 |
|
return '/'.implode('/', $urlParts); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Inject a random number of images (between 0 and 3) into a markdown source |
121
|
|
|
* |
122
|
|
|
* @param string $markdown Markdown |
123
|
|
|
* @return string Markdown with images |
124
|
|
|
*/ |
125
|
2 |
|
protected static function injectMarkdownImages($markdown) |
126
|
|
|
{ |
127
|
2 |
|
$markdownBlocks = preg_split('%\R{2,}%', $markdown.PHP_EOL.PHP_EOL); |
128
|
|
|
|
129
|
|
|
// Iterate through a random number of images between 0 and 3 |
130
|
2 |
|
for ($image = 0, $imageMax = 3 - self::faker()->biasedNumberBetween(0, 3); $image < $imageMax; ++$image) { |
131
|
2 |
|
$markdownBlocks[array_rand($markdownBlocks)] .= PHP_EOL.PHP_EOL.self::randomMarkdownImage(); |
132
|
2 |
|
} |
133
|
|
|
|
134
|
2 |
|
return trim(implode(PHP_EOL.PHP_EOL, $markdownBlocks)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create and return a random image for Markdown use |
139
|
|
|
* |
140
|
|
|
* @return string Random image markdown |
141
|
|
|
*/ |
142
|
2 |
|
protected static function randomMarkdownImage() |
143
|
|
|
{ |
144
|
2 |
|
return ', rand(150, 300)).')'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Create and return a Faker generator |
149
|
|
|
* |
150
|
|
|
* @return Generator Faker generator |
151
|
|
|
*/ |
152
|
2 |
|
protected static function faker() |
153
|
|
|
{ |
154
|
2 |
|
if (self::$faker === null) { |
155
|
1 |
|
self::$faker = Factory::create(); |
156
|
1 |
|
self::$faker->addProvider(Kernel::create(Biased::class)); |
157
|
1 |
|
} |
158
|
2 |
|
return self::$faker; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|