Skip to content

Commit

Permalink
Added O::pathOr() and tweaked O::assocPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Aug 5, 2021
1 parent 4fd8b20 commit 6005fb0
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions src/O.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class O
// O::isObject(new stdClass()) -> true
public static function isObject($data): bool
{
return is_object($data);
return $data !== null && is_object($data);
}

// O::toPairs(['a' => 1, 'b' => 2]) -> [['a', 1], ['b', 2]]
Expand Down Expand Up @@ -58,16 +58,50 @@ public static function assoc(string $key, $value, $data)
return $data;
}

public static function pathOr($path, $defaultValue, $data)
{
$tmp = $data;

for ($i = 0; $i < count($path); $i++) {
$key = $path[$i];
if (!self::isObject($tmp) && !A::isArray($tmp)) {
return $defaultValue;
}

if (self::isObject($tmp) || A::isAssoc($tmp)) {
if (!self::has($key, $tmp)) {
return $defaultValue;
} else {
$tmp = self::prop($key, $tmp);
}
} else {
if (!isset($tmp[$key])) {
return $defaultValue;
} else {
$tmp = $tmp[$key];
}
}
}

return $tmp;
}

// O::assocPath(['foo', 'bar'], 12, {}) -> {foo: {bar: 12}}
function assocPath($path, $value, $data) {
public static function assocPath($path, $value, &$data)
{
$tmp = &$data;

for ($i = 0; $i < count($path); $i++) {
$tmp = &$tmp[$path[$i]];
$key = $path[$i];
if (self::isObject($tmp)) {
$tmp = &$tmp->{$key};
} elseif (A::isArray($tmp)) {
$tmp = &$tmp[$key];
}
}

$tmp = $value;

return $data;
}

Expand All @@ -86,7 +120,8 @@ public static function dissoc(string $key, $data)
}

// O::has('x', {x:10, y:20}) -> true
public static function has(string $key, $data): bool {
public static function has(string $key, $data): bool
{
if (self::isObject($data)) {
return property_exists($data, $key);
}
Expand All @@ -99,7 +134,8 @@ public static function has(string $key, $data): bool {
}

// O::keys(['a' => 1, 'b' => 2]) -> ['a', 'b']
public static function keys($data): array {
public static function keys($data): array
{
if (self::isObject($data)) {
return A::keys(get_object_vars($data));
}
Expand All @@ -110,7 +146,8 @@ public static function keys($data): array {
}

// O::values(['a' => 1, 'b' => 2]) -> [1, 2]
public static function values($data): array {
public static function values($data): array
{
if (self::isObject($data)) {
return A::values(get_object_vars($data));
}
Expand All @@ -121,7 +158,8 @@ public static function values($data): array {
}

// O::prop('x', ['x' => 10]) -> 10
public static function prop($key, $data) {
public static function prop($key, $data)
{
if (self::isObject($data)) {
return $data->{$key} ?? null;
}
Expand Down

0 comments on commit 6005fb0

Please sign in to comment.