Ejemplo n.º 1
0
 /**
  * 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;
 }
Ejemplo n.º 2
0
 /**
  * Sets the relations to retrieve.
  *
  * @param  array  $embed The relations to load with the query.
  * @return object        Returns `$this`.
  */
 public function embed($embed = null, $conditions = [])
 {
     if (!$embed) {
         return $this->_embed;
     }
     if (!is_array($embed)) {
         $embed = [$embed => $conditions];
     }
     $embed = Set::normalize($embed);
     $this->_embed = Set::merge($this->_embed, $embed);
     return $this;
 }
Ejemplo n.º 3
0
        });
    });
    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']);
            };
            expect($closure)->toThrow(new Exception("Invalid array format, a value can't be normalized"));
        });
    });
});