/** * Returns a nested tree representation of `'embed'` option. * * @return array The corresponding nested tree representation. */ public function treeify($embed) { if (!$embed) { return []; } $embed = Set::expand(array_fill_keys(array_keys(Set::normalize((array) $embed)), null)); $result = []; foreach ($embed as $relName => $value) { if (!isset($this->_relations[$relName])) { continue; } if ($this->_relations[$relName]['relation'] === 'hasManyThrough') { $rel = $this->relation($relName); $result[$rel->through()] = [$rel->using() => $value]; } $result[$relName] = $value; } return $result; }
/** * Applies the has conditions. */ protected function _applyHas() { $tree = Set::expand(array_fill_keys(array_keys($this->has()), false)); $this->_applyJoins($this->schema(), $tree, '', $this->alias()); foreach ($this->has() as $path => $conditions) { $this->where($conditions, $this->alias($path)); } }
it("slices two arrays", function () { $data = ['key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3']; list($kept, $removed) = Set::slice($data, ['key3']); expect($removed)->toBe(['key3' => 'val3']); expect($kept)->toBe(['key1' => 'val1', 'key2' => 'val2']); }); }); describe("::flatten()", function () { $this->expanded = [['Post' => ['id' => '1', 'author_id' => '1', 'title' => 'First Post'], 'Author' => ['id' => '1', 'user' => 'nate', 'password' => 'foo']], ['Post' => ['id' => '2', 'author_id' => '5', 'title' => 'Second Post'], 'Author' => ['id' => '5', 'user' => 'jeff', 'password' => null]]]; $this->flattened = ['0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1', '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '5', '1.Post.title' => 'Second Post', '1.Author.id' => '5', '1.Author.user' => 'jeff', '1.Author.password' => null]; it("flattens", function () { $result = Set::flatten($this->expanded); expect($result)->toBe($this->flattened); }); it("expands", function () { $result = Set::expand($this->flattened); expect($result)->toBe($this->expanded); }); }); describe("::normalize()", function () { it("normalizes arrays", function () { $result = Set::normalize(['one', 'two', 'three']); $expected = ['one' => null, 'two' => null, 'three' => null]; expect($result)->toBe($expected); $result = Set::normalize(['one' => ['a', 'b', 'c' => 'd'], 'two' => 2, 'three']); $expected = ['one' => ['a', 'b', 'c' => 'd'], 'two' => 2, 'three' => null]; expect($result)->toBe($expected); }); it("throws with non-normalizable array", function () { $closure = function () { Set::normalize([['a', 'b', 'c' => 'd'], 'two' => 2, 'three']);