PHP code example of oi-lab / oi-laravel-ts

1. Go to this page and download the library: Download oi-lab/oi-laravel-ts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oi-lab / oi-laravel-ts example snippets


return [
    // Output path for generated TypeScript file
    'output_path' => resource_path('js/types/interfaces.ts'),

    // Include _count fields for relationships
    'with_counts' => true,

    // Enable JSON-LD support
    'with_json_ld' => false,

    // Follow relationships to generate interfaces for referenced models
    // (incl. models attached through traits, e.g. spatie/laravel-permission)
    'discover_related_models' => true,

    // Add every Eloquent model of these namespaces to the schema, as if in app/Models
    'Name} interfaces
    'extended_namespaces' => [],

    // Save intermediate schema.json for debugging
    'save_schema' => false,

    // Namespaces holding spatie/laravel-data style DTOs to emit as I{ClassName}
    'data_namespaces' => [],

    // Give a DTO a distinct interface name when two share a short class name
    'data_aliases' => [],

    // Emit a DTO as a discriminated union (see "Discriminated Unions")
    'data_discriminators' => [],

    // 'null': `?` means "key may be absent", `| null` means "value may be null"
    // 'optional': legacy — nullable or defaulted properties all render as `?`
    'data_nullable_style' => 'null',

    // When true, a model mapped to a DTO no longer emits its own Eloquent interface
    'data_replaces_model' => false,

    // Explicit model => DTO map (otherwise inferred from the DTO's fromModel() factory)
    'data_for_model' => [],

    // Define specific types for model properties
    'props_with_types' => [],

    // Add custom properties to models
    'custom_props' => [
        'Organization' => [
            'uuid' => 'string',
        ],
    ],
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = ['name', 'email', 'bio'];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }
}

// config/oi-laravel-ts.php
'custom_props' => [
    'User' => [
        'full_name' => 'string',
        'avatar_url' => 'string',
    ],
],

// Your model
class Page extends Model
{
    protected $casts = [
        'metadata' => MetadataCast::class,
    ];
}

// Your Cast
class MetadataCast implements CastsAttributes
{
    public function get($model, string $key, $value, array $attributes): Metadata
    {
        return Metadata::fromArray(json_decode($value, true));
    }
}

// Generated TypeScript
export interface IMetadata {
    title: string;
    description?: string;
}

export interface IPage {
    id: number;
    metadata?: IMetadata | null;
}

'data_namespaces' => [
    'App\\Data',
],

// App\Data\Knowledge\KnowledgeData
class KnowledgeData extends \Spatie\LaravelData\Data
{
    public function __construct(
        public readonly string $id,
        public readonly KnowledgeState $state,   // backed enum
        public readonly ?KnowledgeSourceData $source,   // nested DTO
        /** @var KnowledgeTagData[]|null */
        public readonly ?array $tags,
        public readonly string|Optional $slug,   // may be absent from the payload
        public readonly bool $isActive = true,
    ) {}

    public static function fromModel(Knowledge $knowledge): self { /* ... */ }
}

'data_replaces_model' => true,
'data_for_model' => [
    App\Models\Knowledge::class => App\Data\Knowledge\KnowledgeData::class,
],

'data_discriminators' => [
    App\Data\BlockData::class => [
        'discriminant' => 'template_key',
        'property' => 'props',
        // A literal map, or a callable returning one at generation time,
        // e.g. [BlockRegistry::class, 'propsClasses'].
        'map' => [
            'hero' => App\Data\Blocks\HeroData::class,
            'grid' => App\Data\Blocks\GridData::class,
        ],
    ],
],

'excluded_namespaces' => [
    'OiLab\\Prestashop\\Models',
],

'extended_namespaces' => [
    'OiLab\\Prestashop\\Extended\\Models',
],

'custom_props' => [
    'User' => [
        'settings' => '@/types/settings|UserSettings',
    ],
],
typescript
export interface IBlockDataBase { id: number; name: string | null; }

export type IBlockDataPropsMap = { 'hero': IHeroData; 'grid': IGridData; };

export type IBlockData = IBlockDataBase & (
    | { template_key: 'hero'; props: IHeroData }
    | { template_key: 'grid'; props: IGridData }
);