Skip to content

Commit a34076c

Browse files
committed
Fixed FakerContent generator for repeater and flexible content.
1 parent 523e327 commit a34076c

File tree

1 file changed

+82
-41
lines changed

1 file changed

+82
-41
lines changed

framework/Supports/FakerContent.php

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,75 @@ protected function __construct() {
5555
* @return array
5656
*/
5757
public function flexible_content( $data = array() ) {
58-
$flexible_content = array();
59-
if ( empty( $data ) && ! class_exists( 'acf' ) ) {
60-
return $flexible_content;
61-
}
62-
foreach ( $data as $layout => $fields ) {
63-
foreach ( $fields as $field_value ) {
64-
$flexible_content[] = array_merge( $field_value, array( 'acf_fc_layout' => $layout ) );
65-
}
58+
if ( empty( $data ) || ! class_exists( 'acf' ) ) {
59+
return array();
6660
}
6761

68-
return $flexible_content;
62+
return $data;
63+
}
64+
65+
/**
66+
* Prepare flexible layout data array
67+
*
68+
* @param string $layout_name Flexible content layout name.
69+
* @param array $data Layout fields data.
70+
*
71+
* @return array
72+
*/
73+
public function flexible_layout( $layout_name, $data ) {
74+
return array_merge( $data, array( 'acf_fc_layout' => $layout_name ) );
6975
}
7076

7177
/**
7278
* Generated array for repeater fields.
7379
*
74-
* @param array $data Repeater fields data.
80+
* @param int|int[] $qty Min/max qty to generate.
81+
* @param callable $callback Repeater fields data generator callback.
7582
*
7683
* @return array
7784
*/
78-
public function repeater( $data = array() ) {
79-
$repeater = array();
80-
if ( empty( $data ) ) {
81-
return $repeater;
85+
public function repeater( $qty, $callback ) {
86+
// validate qty.
87+
$qty = $this->normalize_qty( $qty );
88+
if ( $qty[0] < 1 ) {
89+
return array();
8290
}
83-
foreach ( $data as $fields ) {
84-
$repeater[] = $fields;
91+
92+
// generate data.
93+
$data = array();
94+
$num = $this->faker->numberBetween( $qty[0], $qty[1] );
95+
for ( $i = 0; $i < $num; $i++ ) {
96+
$data[] = $callback();
97+
}
98+
99+
return $data;
100+
}
101+
102+
/**
103+
* Normalize qty to standard range array.
104+
*
105+
* @param int|int[] $qty Number or range.
106+
*
107+
* @return array|bool
108+
*/
109+
protected function normalize_qty( $qty ) {
110+
if ( ! is_array( $qty ) ) {
111+
$qty = array( $qty );
112+
}
113+
if ( count( $qty ) < 2 && (int) $qty[1] < (int) $qty[0] ) {
114+
$qty[1] = $qty[0];
115+
}
116+
$qty[0] = (int) $qty[0];
117+
$qty[1] = (int) $qty[1];
118+
119+
if ( $qty[0] < 1 ) {
120+
return false;
85121
}
86122

87-
return $repeater;
123+
return [
124+
$qty[0],
125+
$qty[1],
126+
];
88127
}
89128

90129
/**
@@ -121,12 +160,18 @@ public function html_text(
121160
/**
122161
* Get fake words.
123162
*
124-
* @param int $chars Chars number.
163+
* @param int|int[] $qty Words number or range.
125164
*
126165
* @return string
127166
*/
128-
public function words( $chars = 3 ) {
129-
return ucfirst( TextBase::words( $chars, true ) );
167+
public function words( $qty = 3 ) {
168+
$qty = $this->normalize_qty( $qty );
169+
if ( $qty[0] < 1 ) {
170+
return '';
171+
}
172+
173+
$nb = $this->faker->numberBetween( $qty[0], $qty[1] );
174+
return $this->faker->words( $nb, true );
130175
}
131176

132177
/**
@@ -139,7 +184,8 @@ public function words( $chars = 3 ) {
139184
* @return int|string
140185
*/
141186
public function attachment_generated( $width = 1100, $height = 800, $type = 'id' ) {
142-
$attach_url = "http://via.placeholder.com/{$width}x{$height}/";
187+
$color = substr( md5( microtime( true ), false ), 0, 6 );
188+
$attach_url = "http://via.placeholder.com/{$width}x{$height}/$color";
143189

144190
if ( 'id' !== $type ) {
145191
return $attach_url;
@@ -181,8 +227,20 @@ public function attachment_generated( $width = 1100, $height = 800, $type = 'id'
181227
*
182228
* @return int
183229
*/
184-
public function number() {
185-
return rand( 1, 99 );
230+
public function percent() {
231+
return $this->faker->numberBetween( 0, 100 );
232+
}
233+
234+
/**
235+
* Generate random number.
236+
*
237+
* @param int $min min value.
238+
* @param int $max max value.
239+
*
240+
* @return int
241+
*/
242+
public function number( $min = 1, $max = 99 ) {
243+
return $this->faker->numberBetween( $min, $max );
186244
}
187245

188246
/**
@@ -241,21 +299,4 @@ public function email() {
241299
return $this->faker->safeEmail;
242300
}
243301

244-
/**
245-
* Get fake domain.
246-
*
247-
* @return string
248-
*/
249-
public function domain() {
250-
return $this->faker->domainName;
251-
}
252-
253-
/**
254-
* Get fake IP address.
255-
*
256-
* @return string
257-
*/
258-
public function ip() {
259-
return $this->faker->localIpv4;
260-
}
261-
}
302+
}

0 commit comments

Comments
 (0)