PHP code example of saasformation / magic-field

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

    

saasformation / magic-field example snippets


$data = [
    'name' => 'John',
    'birthdate' => '1990-02-03',
    'height' => 177,
    'alive' => true,
    'highSchoolQualificationsAverage' => 4.56,
    'professions' => [
        'computing engineer',
        'soldier'
    ]
];

$name = (new StandardField($data['name']))->string();
$birthdate = (new StandardField($data['birthdate']))->datetime();
$height = (new StandardField($data['height']))->integer();
$alive = (new StandardField($data['alive']))->boolean();
$highSchoolQualificationsAverage = (new StandardField($data['highSchoolQualificationsAverage']))->float();
$professions = (new StandardField($data['professions']))->array();

abstract class BaseController {
    protected function field(string $path): StandardField {
        return new StandardField($this->getFromBodyRequestByPath($path));
    }
}

class Controller extends BaseController {
    public function doSomething(): Response {
        $this->commandBus->handle(
            new CreatePersonCommand(
                $this->field('data.attributes.name')->string(),
                $this->field('data.attributes.birthdate')->datetime(),
                $this->field('data.attributes.height')->integer(),
                $this->field('data.attributes.alive')->boolean(),
                $this->field('data.attributes.highSchoolQualificationsAverage')->float(),
                $this->field('data.attributes.professions')->array(),
            )
        )
    }
}

trait MyConverterCapable {
    public function addOneToInteger(): integer {
        return (int)$this->value + 1;
    }
}

class MyConverterField extends StandardField
{
    use MyConverterCapable;
}

$addedInteger = (new MyConverterField(1)->addOneToInteger(); // 2
$this->value