PHP code example of peroks / model

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

    

peroks / model example snippets




use Peroks\Model\Model;
use Peroks\Model\PropertyType;

/**
 * The GeoPoint model class.
 *
 * @property float $latitude The geo point latitude.
 * @property float $longitude The geo point longitude.
 */
class GeoPoint extends Model {

    /**
     * @var array An array of model properties.
     */
    protected static array $properties = [
        'latitude'  => [
            'id'       => 'latitude',
            'name'     => 'Latitude',
            'desc'     => 'The geo point latitude',
            'type'     => PropertyType::FLOAT,
            '



use Peroks\Model\PropertyType;

/**
 * The GeoPoint model with altitude. The altitude is optional.
 *
 * @property float $altitude The geo point altitude.
 */
class GeoPointWithAltitude extends GeoPoint {

    /**
     * @var array An array of model properties.
     */
    protected static array $properties = [
        'altitude' => [
            'id'   => 'altitude',
            'name' => 'Altitude',
            'desc' => 'The geo point altitude',
            'type' => PropertyType::NUMBER, // int or float.
        ],
    ];
}

$geo = new GeoPoint();
$geo->latitude  = 70.6646625;
$geo->longitude = 23.6807195;

$geo = new GeoPoint();
$geo['latitude']  = 70.6646625;
$geo['longitude'] = 23.6807195;

$data = [ latitude => 70.6646625, longitude => 23.6807195 ];

// Returns the model instance on success or null on failure.
$geo = GeoPoint:create( $data )->validate(); // Returns the model instance.
$geo = GeoPoint:create()->validate(); // Returns null.
$geo = GeoPoint:create( [ latitude => 70.6646625 ] )->validate(); // Returns null

$data = [ latitude => 70.6646625, longitude => 23.6807195 ];

// Returns the model instance on success or throws a ModelException on failure.
$geo = GeoPoint:create( $data )->validate( true ); // Returns the model instance.
$geo = GeoPoint:create()->validate( true ); // Throws ModelException.
$geo = GeoPoint:create( [ latitude => 70.6646625 ] )->validate( true ); // Throws ModelException.

$geo = GeoPoint:create( [ latitude => 70.6646625, longitude => 23.6807195 ] );
$latitude  = $geo->latitude;
$longitude = $geo['longitude'];

$geo = GeoPoint:create( [ latitude => 70.6646625, longitude => 23.6807195 ] );
$data = $geo->data();

$geo = GeoPoint:create( [ latitude => 70.6646625, longitude => 23.6807195 ] );
a) $json = json_encode( $geo );
b) $json = (string) $geo;



use Peroks\Model\Model;
use Peroks\Model\PropertyType;

/**
 * The Travel model.
 *
 * @property GeoPoint $from Where the travel starts.
 * @property GeoPoint $to Where the travel ends.
 */
class Travel extends Model {

    /**
     * @var array An array of model properties.
     */
    protected static array $properties = [
        'from' => [
            'id'      => 'from',
            'name'    => 'From geo point',
            'type'    => PropertyType::OBJECT,
            'model'   => GeoPoint::class,
            'default' => [ latitude => 70.6646625, longitude => 23.6807195 ],
            '

// Validates the travel model and all sub-models.
$travel = Tarvel::create()->validate( true ); // Returns a valid Travel model.
$from   = $travel->from; // Returns a GeoPont model, already validated.

// Decode, convert and validate external data structures.
$json   = $client->import(); // Json encoded string from an api call.
$travel = Tarvel::create( $json )->validate( true );

abstract class PropertyItem {
    const ID          = 'id';           // string, The property id (      = 'desc';         // string, The property description (default: null).
    const TYPE        = 'type';         // string, The property type (default: PropertyType::MIXED).
    const MODEL       = 'model';        // string, The class name of a model (default: null).
    const OBJECT      = 'object';       // string, The class or interface name to validate an object against (default: null).
    const FOREIGN     = 'foreign';      // string, The property contains an id of the (foreign) model class name (default: null).
    const MATCH       = 'match';        // string, The property of the (foreign) model to match the own id (default: null).
    const DEFAULT     = 'default';      // mixed, The property default value (default: null).
    const REQUIRED    = '';  // array, An enumeration of all valid property values (default: null).
    const MIN         = 'min';          // int|float, The minimum numeric value or string/array length (default: null).
    const MAX         = 'max';          // int|float, The maximum numeric value or string/array length (default: null).
    const VALUE       = 'value';        // mixed, The property value (default: null).
    const PROPERTIES  = 'properties';   // array, An array of model property definitions (default: null).
}

abstract class PropertyType {
    const MIXED    = '';            // Any type, no validation.
    const BOOL     = 'boolean';
    const NUMBER   = 'number';      // Integer or float.
    const INTEGER  = 'integer';
    const FLOAT    = 'double';
    const STRING   = 'string';
    const UUID     = 'uuid';        // A uuid string.
    const URL      = 'url';         // A url.
    const EMAIL    = 'email';       // An email address.
    const DATETIME = 'datetime';    // An ISO 8601 datetime string.
    const DATE     = 'date';        // A date string (Y-m-d).
    const TIME     = 'time';        // A time string (H:i or H:i:s).
    const ARRAY    = 'array';
    const OBJECT   = 'object';
    const FUNCTION = 'function';    // A callable function.
}