/** * merge their data into our array, using dot.notation.support to * find the point where the merge starts * * @param array &$arr * the array that we want to merge into * @param string $path * the dot.notation.support path to where the merge should start * @param mixed $value * the data that we want to merge from * @param array|callable|string|null * if $path goes beyond what exists in $ours, how do we want to * extend $ours? * @return void */ public static function intoArray(&$arr, $path, $value, $extendingItem = null) { // robustness! RequireIndexable::check($arr, E4xx_UnsupportedType::class); RequireDotNotationPath::check($path); // find the point where we want to merge list($firstPart, $finalPart) = self::splitPathInTwo($path); if ($firstPart !== null) { $leaf =& DescendDotNotationPath::intoArray($arr, $firstPart, $extendingItem); } else { $leaf =& $arr; } // merge it MergeIntoProperty::of($leaf, $finalPart, $value); }
/** * @covers ::intoArray * @covers ::getPathFromRoot * @covers ::getChildFromPart * @expectedException GanbaroDigital\DataContainers\Exceptions\E4xx_NoSuchProperty */ public function testThrowsExceptionWhenChildObjectOfArrayDoesNotMatchPath() { // ---------------------------------------------------------------- // setup your test $data = ["one" => (object) ["two" => 2]]; // ---------------------------------------------------------------- // perform the change DescendDotNotationPath::intoArray($data, "one.three"); }
/** * does $path point to a valid piece of data inside $container? * * @param array $container * the container to look inside * @param string $path * the dot.notation.support path to walk * @return boolean * TRUE if $path points to a vlaid piece of data * FALSE otherwise */ public static function inArray($container, $path) { // defensive programming! RequireIndexable::check($container, E4xx_UnsupportedType::class); RequireAnyOneOf::check([new IsDotNotationPath(), new IsStringy()], [$path], E4xx_NotDotNotationPath::class); try { DescendDotNotationPath::intoArray($container, $path); return true; } catch (E4xx_NoSuchIndex $e) { return false; } catch (E4xx_NoSuchProperty $e) { return false; } }
/** * extract a value from an array, using dot.notation.support * * @param array $arr * the array to extract from * @param string $index * the dot.notation.support path to walk * @return mixed * whatever we find when we walk the path */ public static function fromArray($arr, $index) { // robustness! RequireIndexable::check($arr, E4xx_UnsupportedType::class); return DescendDotNotationPath::intoArray($arr, $index); }