PHP code example of macropay-solutions / php-crufd-wizard-decorator

1. Go to this page and download the library: Download macropay-solutions/php-crufd-wizard-decorator 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/ */

    

macropay-solutions / php-crufd-wizard-decorator example snippets


    SomeMiddleware::class . ':list'
    SomeMiddleware::class . ':get'
    SomeMiddleware::class . ':getRelated'
    SomeMiddleware::class . ':update'
    SomeMiddleware::class . ':updateRelated'
    SomeMiddleware::class . ':create'
    SomeMiddleware::class . ':delete'
    SomeMiddleware::class . ':deleteRelated'

    
    
    namespace MacropaySolutions\CrufdWizardDecorator\Models;
    
    use MacropaySolutions\Kernel\Database\Obvious\Model;
    use MacropaySolutions\Kernel\Database\Obvious\Relations\HasOne;
    use MacropaySolutions\CrufdWizard\Models\BaseModel;
    
    class ExampleModel extends BaseModel
    {
        public const RESOURCE_NAME = 'examples';
    
        protected $fillable = [
            'role_id',
            'created_at',
            'updated_at',
        ];
    
        public function roleRelation(): HasOne
        {
            return $this->hasOne(RelationExampleModel::class, 'id', 'role_id');
        }
    }
    
    
    
    namespace MacropaySolutions\CrufdWizardDecorator\Models;
    
    use MacropaySolutions\Kernel\Database\Obvious\Model;
    use MacropaySolutions\Kernel\Database\Obvious\Relations\HasMany;
    use MacropaySolutions\CrufdWizard\Models\BaseModel;
    
    class RelationExampleModel extends BaseModel
    {
        public const RESOURCE_NAME = 'relation-examples';
    
        protected $fillable = [
            'name',
            'color',
            'created_at',
            'updated_at',
        ];
    
        public function exampleModels(): HasMany
        {
            return $this->hasMany(ExampleModel::class, 'role_id', 'id');
        }
    }
    
    
    
    namespace MacropaySolutions\CrufdWizardDecorator\Http\Middleware\Decorators;
    
    use MacropaySolutions\CrufdWizardDecorator\Decorators\ExampleDecorator;
    use MacropaySolutions\CrufdWizardDecorator\Decorators\RelationExampleDecorator;
    
    class ExampleMiddleware extends AbstractDecoratorMiddleware
    {
        protected string $decoratorClass = ExampleDecorator::class;
        protected array $relatedDecoratorClassMap = [
            'roleRelation' => RelationExampleDecorator::class
        ];

        public function setResourceModel(): void
        {
            $this->resourceModel = new ExampleModel();
        }
    }
    
    
    
    namespace MacropaySolutions\CrufdWizardDecorator\Http\Middleware\Decorators;
    
    use MacropaySolutions\CrufdWizardDecorator\Decorators\ExampleDecorator;
    use MacropaySolutions\CrufdWizardDecorator\Decorators\RelationExampleDecorator;
    
    class RelationExampleMiddleware extends AbstractDecoratorMiddleware
    {
        protected string $decoratorClass = RelationExampleDecorator::class;
        protected array $relatedDecoratorClassMap = [
            'exampleModels' => ExampleDecorator::class
        ];

        public function setResourceModel(): void
        {
            $this->resourceModel = new RelationExampleModel();
        }
    }
    
    
    
    namespace MacropaySolutions\CrufdWizardDecorator\Decorators;
    
    class ExampleDecorator extends AbstractResourceDecorator
    {
        public function getResourceMappings(): array
        {
            return [
                'id' => 'ID',
                'updated_at' => 'updatedAt',
                'created_at' => 'createdAt',
            ];
        }
    
        /**
         * @inheritDoc
         */
        public function getRelationMappings(): array
        {
            return [
                'roleRelation' => [
                    'name' => 'roleRelationName',
                    'color' => 'roleRelationColor',
                ],
            ];
        }
    }
    
    
    
    
    namespace MacropaySolutions\CrufdWizardDecorator\Decorators;
    
    class RelationExampleDecorator extends AbstractResourceDecorator
    {
        public array $countRelations = ['exampleModels'];
        public array $existRelations = ['exampleModels'];

        public function getResourceMappings(): array
        {
            return [
                'id' => 'ID',
                'name' => 'roleName',
                'color' => 'roleColor',
                'updated_at' => 'updatedAt',
                'created_at' => 'createdAt',
            ];
        }
    }
bash
php run make:api-resource {resourceName} --decorated