Skip to content

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
Log1x committed Aug 7, 2019
1 parent a2d02bd commit 08624f7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
]);
}

Expand Down
54 changes: 49 additions & 5 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 08624f7

Please sign in to comment.