PHP code example of uni-method / json-to-php-class

1. Go to this page and download the library: Download uni-method/json-to-php-class 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/ */

    

uni-method / json-to-php-class example snippets




namespace App\Model;

class Root
{
    protected int $count;
    /**
     * @var Same[]
     */
    protected array $same;
    public function getCount() : int
    {
        return $this->count;
    }
    public function setCount(int $count) : void
    {
        $this->count = $count;
    }
    /**
     * @return Same[]
     */
    public function getSame() : array
    {
        return $this->same;
    }
    /**
     * @param Same[] $same
     */
    public function setSame(array $same) : void
    {
        $this->same = $same;
    }
}



namespace App\Model;

class Same
{
    protected float $length;
    protected Tag $tag;
    public function getLength() : float
    {
        return $this->length;
    }
    public function setLength(float $length) : void
    {
        $this->length = $length;
    }
    public function getTag() : Tag
    {
        return $this->tag;
    }
    public function setTag(Tag $tag) : void
    {
        $this->tag = $tag;
    }
}



namespace App\Model;

class Tag
{
    protected string $name;
    public function getName() : string
    {
        return $this->name;
    }
    public function setName(string $name) : void
    {
        $this->name = $name;
    }
}

use Symfony\Component\Serializer\Annotation\SerializedName;

/**
 * @SerializedName("reply_to_message")
 */
protected ReplyToMessage $replyToMessage;

 declare(strict_types=1);

use PhpParser\PrettyPrinter;
use UniMethod\JsonToPhpClass\{Builder\AstBuilder, Converter\Converter};

;
$scenarios->attributesOnDifferentNames = [
    'Symfony\Component\Serializer\Annotation\SerializedName' => [['SerializedName', ['{{ originalName }}']]]
];
$scenarios->attributesForNullAndUndefined = [
    false => [
        false => [
            'Symfony\Component\Validator\Constraints as Assert' => [['Assert\NotNull']]
        ],
        true => [],
    ],
    true => [
        false => [],
        true => [],
    ],
];

$converter = new Converter();
$prettyPrinter = new PrettyPrinter\Standard();
$ast = new AstBuilder();
$classes = $converter->convert($json);

foreach ($classes as $class) {
    $fullPath = $path . '/' . $class->name . '.php';
    file_put_contents($fullPath, $prettyPrinter->prettyPrintFile($ast->build($class)));
}
shell
php script.php /some/local/path/input.json
shell
php script.php /some/local/path/input.json /put/generated/files/here
shell
php script.php /some/local/path/input.json /put/generated/files/here "App\Dto"
docker build -t php-debug .