Completed
Pull Request — master (#51)
by Tom
04:01
created

DefinitionParser::parse()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 19
nc 2
nop 1
1
<?php
2
3
namespace Spatie\SchemaOrg\Generator\Parser;
4
5
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseConstant;
6
use Symfony\Component\DomCrawler\Crawler;
7
use Spatie\SchemaOrg\Generator\Definitions;
8
use Spatie\SchemaOrg\Generator\TypeCollection;
9
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseType;
10
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseProperty;
11
12
class DefinitionParser
13
{
14
    public function parse(Definitions $definitions): TypeCollection
15
    {
16
        $types = array_filter($definitions
17
            ->query('[typeof="rdfs:Class"]')
18
            ->each(function (Crawler $crawler) {
19
                return call_user_func(ParseType::fromCrawler($crawler));
20
            }));
21
22
        $properties = array_filter($definitions
23
            ->query('[typeof="rdf:Property"]')
24
            ->each(function (Crawler $crawler) {
25
                return call_user_func(ParseProperty::fromCrawler($crawler));
26
            }));
27
28
        $constants = [];
29
        foreach($types as $type) {
30
            $constants = array_merge($constants, array_filter($definitions
31
                ->query('[typeof="' . $type->resource . '"]')
32
                ->each(function (Crawler $crawler) use ($type) {
33
                    $constant = call_user_func(ParseConstant::fromCrawler($crawler));
34
                    if (!is_null($constant)) {
35
                        $constant->type = $type->name;
36
                    }
37
                    return $constant;
38
                })));
39
        }
40
41
        return new TypeCollection($types, $properties, $constants);
42
    }
43
}
44