Skip to content

Commit 08624f7

Browse files
committed
v1.1.1
* Added `Util::unwrap()` which simply unwraps the passed string from the passed delimiter. * Added `Util::clean()` to combine and clean malformed arrays formed from a parsed expressions. * `Util::toString()` now accepts a second parameter `$single` for working with flattened or malformed arrays. * Fix passing an array as the third parameter to `@image`
1 parent a2d02bd commit 08624f7

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## v1.1.1 (08-07-2019)
2+
3+
### Enhancements
4+
- Added `Util::unwrap()` which simply unwraps the passed string from the passed delimiter.
5+
- Added `Util::clean()` to combine and clean malformed arrays formed from a parsed expressions.
6+
- `Util::toString()` now accepts a second parameter `$single` for working with flattened or malformed arrays.
7+
8+
### Bug fixes
9+
- Fix passing an array as the third parameter to `@image`
10+
111
## v1.1.0 (08-07-2019)
212

313
### Enhancements

src/Directives/WordPress.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,15 @@
295295
]);
296296
}
297297

298+
if (! empty($expression->get(3))) {
299+
$expression = $expression->replace([
300+
2 => Util::clean($expression->slice(2)->all())
301+
]);
302+
}
303+
298304
if (! empty($expression->get(2)) && ! Util::isArray($expression->get(2))) {
299305
$expression = $expression->replace([
300-
2 => Util::wrap(['alt' => $expression->get(2)])
306+
2 => Util::toString(['alt' => $expression->get(2)])
301307
]);
302308
}
303309

src/Utilities.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,39 @@ public static function wrap($value)
4545
return $value;
4646
}
4747

48+
/**
49+
* Unwraps the passed string from the passed delimiter.
50+
*
51+
* @param string $value
52+
* @param string $delimiter
53+
* @return string
54+
*/
55+
public static function unwrap($value, $delimiter = "'")
56+
{
57+
if (Str::startsWith($value, $delimiter)) {
58+
$value = Str::replaceFirst($delimiter, '', $value);
59+
}
60+
61+
if (Str::endsWith($value, $delimiter)) {
62+
$value = Str::replaceLast($delimiter, '', $value);
63+
}
64+
65+
return $value;
66+
}
67+
68+
/**
69+
* Combine and clean a malformed array formed from a parsed expression.
70+
*
71+
* @param array $expression
72+
* @return string
73+
*/
74+
public static function clean($expression)
75+
{
76+
return Util::unwrap(
77+
Util::toString($expression, true)
78+
);
79+
}
80+
4881
/**
4982
* Dives for an ACF field or sub field and returns the value if it exists.
5083
*
@@ -76,23 +109,34 @@ public static function field($field, $id = null)
76109
/**
77110
* Convert expression to a string.
78111
*
79-
* @param mixed $expression
80-
* @param string $keys
112+
* @param mixed $expression
113+
* @param boolean $single
81114
* @return string
82115
*/
83-
public static function toString($expression, $keys = '')
116+
public static function toString($expression, $single = false)
84117
{
85118
if (! is_array($expression)) {
86119
return self::wrap($expression);
87120
}
88121

122+
$keys = '';
123+
89124
foreach ($expression as $key => $value) {
90-
$keys .= self::wrap($key) . ' => ' . self::wrap($value) . ',';
125+
if ($single) {
126+
$keys .= self::wrap($value) . ',';
127+
} else {
128+
$keys .= self::wrap($key) . ' => ' . self::wrap($value) . ', ';
129+
}
91130
}
92131

93132
$keys = trim(Str::replaceLast(',', '', $keys));
94133

95-
return "[{$keys}]";
134+
if (! $single) {
135+
$keys = Str::start($keys, '[');
136+
$keys = Str::finish($keys, ']');
137+
}
138+
139+
return $keys;
96140
}
97141

98142
/**

0 commit comments

Comments
 (0)