/** * merge their data into one of our array's properties * * @param array &$arr * the array that we want to merge into * @param string $property * the property to merge into * @param mixed $value * the data that we want to merge from * @return void */ public static function ofArray(&$arr, $property, $value) { // robustness! RequireIndexable::check($arr, E4xx_UnsupportedType::class); // easiest case - no clash if (ShouldOverwrite::intoArray($arr, $property, $value)) { $arr[$property] = $value; return; } // special case - we are merging into an object if (is_object($arr[$property])) { MergeIntoAssignable::from($arr[$property], $value); return; } // if we get here, then we must be merging into an array MergeIntoIndexable::from($arr[$property], $value); }
/** * merge a single key into our array * * @param array &$ours * the array to merge into * @param mixed $key * the array key to merge into * @param mixed $value * the data to merge in * @return void */ private static function mergeKeyIntoArray(&$ours, $key, $value) { // general case - overwrite because merging isn't possible if (ShouldOverwrite::intoArray($ours, $key, $value)) { $ours[$key] = $value; return; } // special case - we are merging into an object if (is_object($ours[$key])) { MergeIntoAssignable::from($ours[$key], $value); return; } // at this point, we are merging into an array, using recursion // for which I am going to hell MergeIntoIndexable::from($ours[$key], $value); }
/** * @covers ::intoArray * @dataProvider provideNonIndexable * @expectedException GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType */ public function testThrowsExceptionWhenNonArrayPassedIntoIsArray($data) { // ---------------------------------------------------------------- // setup your test // ---------------------------------------------------------------- // perform the change ShouldOverwrite::intoArray($data, 'doesNotMatter', []); }