From 08624f717923618ae2b022f79c29aa478ef32db2 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 7 Aug 2019 10:38:09 -0500 Subject: [PATCH] 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` --- CHANGELOG.md | 10 +++++++ src/Directives/WordPress.php | 8 +++++- src/Utilities.php | 54 ++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74f6fcf..710152d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## v1.1.1 (08-07-2019) + +### Enhancements +- 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. + +### Bug fixes +- Fix passing an array as the third parameter to `@image` + ## v1.1.0 (08-07-2019) ### Enhancements diff --git a/src/Directives/WordPress.php b/src/Directives/WordPress.php index fed3253..5090a1b 100644 --- a/src/Directives/WordPress.php +++ b/src/Directives/WordPress.php @@ -295,9 +295,15 @@ ]); } + if (! empty($expression->get(3))) { + $expression = $expression->replace([ + 2 => Util::clean($expression->slice(2)->all()) + ]); + } + if (! empty($expression->get(2)) && ! Util::isArray($expression->get(2))) { $expression = $expression->replace([ - 2 => Util::wrap(['alt' => $expression->get(2)]) + 2 => Util::toString(['alt' => $expression->get(2)]) ]); } diff --git a/src/Utilities.php b/src/Utilities.php index 0acddc8..d6a28cd 100644 --- a/src/Utilities.php +++ b/src/Utilities.php @@ -45,6 +45,39 @@ public static function wrap($value) return $value; } + /** + * Unwraps the passed string from the passed delimiter. + * + * @param string $value + * @param string $delimiter + * @return string + */ + public static function unwrap($value, $delimiter = "'") + { + if (Str::startsWith($value, $delimiter)) { + $value = Str::replaceFirst($delimiter, '', $value); + } + + if (Str::endsWith($value, $delimiter)) { + $value = Str::replaceLast($delimiter, '', $value); + } + + return $value; + } + + /** + * Combine and clean a malformed array formed from a parsed expression. + * + * @param array $expression + * @return string + */ + public static function clean($expression) + { + return Util::unwrap( + Util::toString($expression, true) + ); + } + /** * Dives for an ACF field or sub field and returns the value if it exists. * @@ -76,23 +109,34 @@ public static function field($field, $id = null) /** * Convert expression to a string. * - * @param mixed $expression - * @param string $keys + * @param mixed $expression + * @param boolean $single * @return string */ - public static function toString($expression, $keys = '') + public static function toString($expression, $single = false) { if (! is_array($expression)) { return self::wrap($expression); } + $keys = ''; + foreach ($expression as $key => $value) { - $keys .= self::wrap($key) . ' => ' . self::wrap($value) . ','; + if ($single) { + $keys .= self::wrap($value) . ','; + } else { + $keys .= self::wrap($key) . ' => ' . self::wrap($value) . ', '; + } } $keys = trim(Str::replaceLast(',', '', $keys)); - return "[{$keys}]"; + if (! $single) { + $keys = Str::start($keys, '['); + $keys = Str::finish($keys, ']'); + } + + return $keys; } /**