Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Wip * Wip * Wip * Wip * Wip * Apply fixes from StyleCI * Wip * Remove async * Wip * Wip * Wip * Apply fixes from StyleCI
- Loading branch information
1 parent
4d90ddd
commit 0c32ab8
Showing
14 changed files
with
278 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Spatie\SchemaOrg\Generator; | ||
|
||
use RuntimeError; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
class Definitions | ||
{ | ||
/** @var array */ | ||
protected $sources; | ||
|
||
/** @var string */ | ||
protected $tempDir = __DIR__.'/temp'; | ||
|
||
public function __construct(array $sources) | ||
{ | ||
$this->sources = $sources; | ||
} | ||
|
||
public function preload() | ||
{ | ||
foreach ($this->sources as $sourceId => $sourcePath) { | ||
$this->loadSource($sourceId, false); | ||
} | ||
} | ||
|
||
public function query(string $selector): Crawler | ||
{ | ||
return (new Crawler($this->loadSource('core')))->filter($selector); | ||
} | ||
|
||
protected function loadSource(string $sourceId, bool $fromCache = true): string | ||
{ | ||
if (! isset($this->sources[$sourceId])) { | ||
throw new RuntimeError("Source `{$sourceId}` doesn't exist"); | ||
} | ||
|
||
$cachePath = $this->tempDir.'/'.$sourceId.'.rdfa'; | ||
|
||
if ($fromCache && file_exists($cachePath)) { | ||
return file_get_contents($cachePath); | ||
} | ||
|
||
$rdfa = file_get_contents($this->sources[$sourceId]); | ||
|
||
file_put_contents($cachePath, $rdfa); | ||
|
||
return $rdfa; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Spatie\SchemaOrg\Generator\Parser; | ||
|
||
use Spatie\SchemaOrg\Generator\Property; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Spatie\SchemaOrg\Generator\Definitions; | ||
use Spatie\SchemaOrg\Generator\TypeCollection; | ||
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseType; | ||
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseProperty; | ||
|
||
class DefinitionParser | ||
{ | ||
public function parse(Definitions $definitions): TypeCollection | ||
{ | ||
$types = $definitions | ||
->query('[typeof="rdfs:Class"]') | ||
->each(function (Crawler $crawler) { | ||
return call_user_func(ParseType::fromCrawler($crawler)); | ||
}); | ||
|
||
$properties = $definitions | ||
->query('[typeof="rdf:Property"]') | ||
->each(function (Crawler $crawler) { | ||
return call_user_func(ParseProperty::fromCrawler($crawler)); | ||
}); | ||
|
||
return new TypeCollection( | ||
array_filter($types), array_filter($properties) | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Spatie\SchemaOrg\Generator\Parser\Tasks; | ||
|
||
use Spatie\SchemaOrg\Generator\Property; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
class ParseProperty extends Task | ||
{ | ||
public function __invoke(): ?Property | ||
{ | ||
$node = new Crawler($this->definition); | ||
|
||
$property = new Property(); | ||
|
||
$property->name = $this->getText($node, '[property="rdfs:label"]'); | ||
|
||
if (empty($property->name)) { | ||
return null; | ||
} | ||
|
||
$property->description = $this->getText($node, '[property="rdfs:comment"]'); | ||
|
||
$property->resource = $this->getAttribute($node, 'resource'); | ||
|
||
$node | ||
->filter('[property="http://schema.org/domainIncludes"]') | ||
->each(function (Crawler $domain) use ($property) { | ||
$property->addType($this->getText($domain)); | ||
}); | ||
|
||
$node | ||
->filter('[property="http://schema.org/rangeIncludes"]') | ||
->each(function (Crawler $range) use ($property) { | ||
$property->addRanges( | ||
$this->castRangesToTypes($this->getText($range)) | ||
); | ||
}); | ||
|
||
return $property; | ||
} | ||
|
||
private function castRangesToTypes(string $range): array | ||
{ | ||
switch ($range) { | ||
case 'Boolean': | ||
return ['bool']; | ||
case 'False': | ||
return ['false']; | ||
case 'True': | ||
return ['true']; | ||
case 'Date': | ||
case 'Time': | ||
case 'DateTime': | ||
return ['\DateTimeInterface']; | ||
case 'Text': | ||
case 'URL': | ||
return ['string']; | ||
case 'Number': | ||
return ['float', 'int']; | ||
case 'Float': | ||
return ['float']; | ||
case 'Integer': | ||
return ['int']; | ||
default: | ||
return [$range]; | ||
} | ||
} | ||
} |
Oops, something went wrong.