PHP code example of simplecomplex / validate

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

    

simplecomplex / validate example snippets


// We wanna see some bicycles...
class Bicycle
{
    public $wheels = 0;
    public $saddle;
    public $sound = '';
    public $accessories = [];
    public $various;

    public function __construct($wheels, $saddle, $sound, $accessories, $various)
    {
        $this->wheels = $wheels;
        $this->saddle = $saddle;
        $this->sound = $sound;
        $this->accessories = $accessories;
        $this->various = $various;
    }
}

// Not just any kind of bicycles. They must comply to this rule set:
$rule_set = [
    'class' => [
        'Bicycle'
    ],
    'tableElements' => [
        //'exclusive' => true,
        //'whitelist' => ['unspecified_1'],
        //'blacklist' => ['saddle', 'accessories'],
        'rulesByElements' => [
            'wheels' => [
                'integer',
                'range' => [
                    1,
                    3
                ]
            ],
            'saddle' => [
                'integer',
                'alternativeEnum' => [
                    // Wrongly defined as nested; confused by enum which
                    // formally          'numeric',
                            'optional'
                        ]
                    ]
                ]
            ],
            'various' => [
                'array',
                'optional',
                // allowNull and alternativeEnum with null
                // are two ways of allowing null.
                'allowNull' => true,
                'alternativeEnum' => [
                    null,
                ],
                'listItems' => [
                    'maxOccur' => 5,
                    'itemRules' => [
                        'string',
                        'alternativeEnum' => [
                            // Correctly defined as un-nested array.
                            true,
                            false,
                        ]
                    ]
                ]
            ]
        ]
    ]
];