/** * @param Data|Reference $item * @param array $target * @return array */ private static function merge($item, $target) { if ($item instanceof Reference) { $target = $item; } else { $value = $item->getValue(); if (is_array($value)) { $target = array_merge($target, $value); } else { $target[] = $value; } } return $target; }
public function testCombineMultiple() { /* Given */ $data = [Data::build(['foo', 1], ['foo' => 'bar']), Data::build(['foo', 1], ['name' => 'bar2']), Data::build(['foo', 2], ['foo2' => 'bar']), Data::build(['foo', 2], ['name2' => 'bar2'])]; /* When */ $result = DataCombiner::combine($data); /* Then */ assertThat(count($result), is(2)); assertThat($result[0]->getValue(), is(['foo' => 'bar', 'name' => 'bar2'])); assertThat($result[1]->getValue(), is(['foo2' => 'bar', 'name2' => 'bar2'])); }
public function testMultipleReferencesShouldCallOnce() { /* Given */ $dug = \Mockery::mock(Dug::class); $source = Source::build(['users', '/[0-9]+/'], function () { }); $dug->shouldReceive('sourceForPath')->once()->andReturn($source); $user1 = Data::build(['users', 1], ['id' => 1, 'name' => 'Joris']); $user2 = Data::build(['users', 2], ['id' => 2, 'name' => 'Jisca']); $dug->shouldReceive('data')->once()->andReturn([$user1, $user2]); $input = [Data::build(['foo', 1], ['user' => new Reference(['users', 1])]), Data::build(['foo', 2], ['user' => new Reference(['users', 2])])]; /* When */ $referenceResolver = new ReferenceResolver($dug); $result = $referenceResolver->process($input); /* Then */ assertThat(count($result), is(2)); assertThat($result[0]->getValue()['user'], is([$user1])); assertThat($result[1]->getValue()['user'], is([$user2])); }
public function testReferenceListToMultiple() { /* Given */ $dug = new Dug(); $source = Source::build(['pets', '/[0-9]+/'], function (array $path) { return [Data::build(['pets', 1], ['name' => 'Fluffy']), Data::build(['pets', 2], ['name' => 'Loesje'])]; }); $dug->register($source); $source = Source::build(['users', '/[0-9]+/'], function (array $path) { return [Data::build(['users', 123], ['id' => 123, 'pets' => new Reference(['pets', [1, 2]])])]; }); $dug->register($source); /* When */ $result = $dug->fetch(['users', [123]]); /* Then */ assertThat($result, is([['id' => 123, 'pets' => [['name' => 'Fluffy'], ['name' => 'Loesje']]]])); }
/** * @param array $path * @return array */ public function handle(array $path) : array { return [Data::build(['users', 1], ['id' => 1, 'source' => 'UserProvider'])]; }