Tobscure\JsonApi\Util::parseRelationshipPaths PHP Метод

parseRelationshipPaths() публичный статический Метод

Given a flat array of relationship paths like: ['user', 'user.employer', 'user.employer.country', 'comments'] create a nested array of relationship paths one-level deep that can be passed on to other serializers: ['user' => ['employer', 'employer.country'], 'comments' => []]
public static parseRelationshipPaths ( array $paths ) : array
$paths array
Результат array
    public static function parseRelationshipPaths(array $paths)
    {
        $tree = [];
        foreach ($paths as $path) {
            list($primary, $nested) = array_pad(explode('.', $path, 2), 2, null);
            if (!isset($tree[$primary])) {
                $tree[$primary] = [];
            }
            if ($nested) {
                $tree[$primary][] = $nested;
            }
        }
        return $tree;
    }

Usage Example

Пример #1
0
 public function testParseRelationshipPaths()
 {
     $this->assertEquals(['user' => ['employer', 'employer.country'], 'comments' => []], Util::parseRelationshipPaths(['user', 'user.employer', 'user.employer.country', 'comments']));
     $this->assertEquals(['user' => ['employer.country']], Util::parseRelationshipPaths(['user.employer.country']));
 }