PHP code example of dandoetech / resource-registry

1. Go to this page and download the library: Download dandoetech/resource-registry 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/ */

    

dandoetech / resource-registry example snippets


use DanDoeTech\ResourceRegistry\Resource;
use DanDoeTech\ResourceRegistry\Builder\ResourceBuilder;
use DanDoeTech\ResourceRegistry\Definition\FieldType;

class ProductResource extends Resource
{
    protected function define(ResourceBuilder $b): void
    {
        $b->key('product')
          ->label('Product')
          ->timestamps()
          ->field('name', FieldType::String, nullable: false, rules: [' ->filterable(['name', 'price', 'category_name'])
          ->sortable(['name', 'price', 'created_at', 'orders_count'])
          ->searchable(['name'])
          ->action('create')
          ->action('update')
          ->action('delete');
    }
}

use DanDoeTech\ResourceRegistry\Registry\ArrayRegistryDriver;
use DanDoeTech\ResourceRegistry\Registry\Registry;

// For standalone/testing use, ArrayRegistryDriver accepts array configs
$driver = new ArrayRegistryDriver([
    'product' => [
        'label' => 'Product',
        'fields' => [
            new FieldDefinition('name', FieldType::String, nullable: false),
            new FieldDefinition('price', FieldType::Float, nullable: false),
        ],
        'actions' => [new ActionDefinition('create')],
    ],
]);
$registry = new Registry($driver);

$product = $registry->getResource('product');
echo $product->getLabel(); // "Product"

foreach ($product->getFields() as $field) {
    echo $field->getName() . ': ' . $field->getType()->value;
}

// Generic — system builds the query via relation metadata
->computed('category_name', FieldType::String, via: 'category.name')
->computed('orders_count', FieldType::Integer, via: 'count:orders')

// Custom — your resolver class handles the query
->computed('revenue', FieldType::Float, resolver: ProductRevenue::class)

use DanDoeTech\ResourceRegistry\Contracts\RegistryDriverInterface;

final class DatabaseRegistryDriver implements RegistryDriverInterface
{
    /** @return list<ResourceDefinitionInterface> */
    public function all(): array { /* ... */ }

    public function find(string $key): ?ResourceDefinitionInterface { /* ... */ }
}