Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ Every time you call `Yard\Data\PostData::from($post)` you receive an instance of

If you choose to create a new data class for your custom post type, you can have this class be returned for all instances of that post type.

Just use the config file and map all custom posts and it's classes:
To do this, you need to add the Fully Qualified Class Name (FQCN) of your custom data class to the `supports` array when registering your custom post type:

```php
register_post_type('vacancy', [
'supports' => [
'data-class' => ['classFQN' => App\Data\KnowledgebaseData::class],
],
]);
````

Another option is to create a mapping in the `config/yard-data.php` file. The mapping in the project config takes precedence over the register_post_type `supports` args.

```php
'post_types' => [
Expand Down
19 changes: 17 additions & 2 deletions src/PostData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Corcel\Model\Post;
use ReflectionClass;
use ReflectionNamedType;
use RuntimeException;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\WithCastable;
use Spatie\LaravelData\Data;
Expand Down Expand Up @@ -86,7 +87,7 @@ public static function fromCorcel(Post $post): PostData
}

/**
* @return class-string<self>
* @return class-string<PostData>
*/
private static function dataClass(string $postType): string
{
Expand All @@ -96,7 +97,21 @@ private static function dataClass(string $postType): string
return $classes[$postType];
}

return static::class;
$classFQN = get_all_post_type_supports($postType)['data-class'][0]['classFQN'] ?? null;

if (null === $classFQN) {
return static::class;
}

if (! class_exists($classFQN)) {
throw new RuntimeException(sprintf('The class "%s" does not exist or is not autoloaded.', $classFQN));
}

if (! is_a($classFQN, PostData::class, true)) {
throw new RuntimeException(sprintf('The class "%s" must extend %s.', $classFQN, PostData::class));
}

return $classFQN;
}

private function metaPrefix(): string
Expand Down