PHP code example of italix / contracts

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

    

italix / contracts example snippets


use Italix\Contracts\DataContainer;

// Create with initial data
$user = new DataContainer(['name' => 'Alice', 'age' => 30]);

// Array syntax works naturally
$user['email'] = '[email protected]';
echo $user['name'];      // 'Alice'
isset($user['email']);    // true
unset($user['age']);

// Method syntax with fluent chaining
$user->set('city', 'Rome')
     ->set('role', 'admin')
     ->merge(['active' => true, 'score' => 100]);

$user = new DataContainer(['name' => 'Alice', 'role' => 'admin']);

// get() with default value — never throws on missing keys
$user->get('name');              // 'Alice'
$user->get('missing');           // null
$user->get('missing', 'N/A');   // 'N/A'

// Check existence
$user->has('name');       // true  — key exists and is not null
$user->has('missing');    // false
$user->has_key('name');   // true  — key exists (even if value is null)

$user = new DataContainer([
    'id' => 1,
    'name' => 'Alice',
    'email' => '[email protected]',
    'password' => 'hashed...',
]);

// Get everything as a plain array
$user->to_array();
// ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]', 'password' => 'hashed...']

// Pick specific keys
$user->only(['name', 'email']);
// ['name' => 'Alice', 'email' => '[email protected]']

// Exclude sensitive keys
$user->except(['password']);
// ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]']

// Inspect keys and values
$user->keys();    // ['id', 'name', 'email', 'password']
$user->values();  // [1, 'Alice', '[email protected]', 'hashed...']

$config = new DataContainer(['debug' => false]);

// set() returns $this for chaining
$config->set('debug', true)
       ->set('log_level', 'info');

// merge() overwrites existing keys
$config->merge(['debug' => false, 'cache' => true]);
// Result: ['debug' => false, 'log_level' => 'info', 'cache' => true]

// Remove a key
$config->remove('cache');

// Clear everything
$config->clear();
$config->is_empty(); // true

$data = new DataContainer(['a' => 1, 'b' => 2, 'c' => 3]);

// count()
count($data);  // 3

// foreach
foreach ($data as $key => $value) {
    echo "$key: $value\n";
}

// json_encode()
echo json_encode($data);  // {"a":1,"b":2,"c":3}

// Check state
$data->is_empty();  // false
$data->count();     // 3

use Italix\Contracts\DataContainer;

class Config extends DataContainer
{
    /**
     * Get a nested value using dot notation.
     */
    public function dot_get(string $path, $default = null)
    {
        $keys = explode('.', $path);
        $value = $this->data;

        foreach ($keys as $key) {
            if (!is_array($value) || !array_key_exists($key, $value)) {
                return $default;
            }
            $value = $value[$key];
        }

        return $value;
    }
}

$config = new Config([
    'database' => ['host' => 'localhost', 'port' => 3306],
]);

$config->dot_get('database.host');  // 'localhost'
$config->dot_get('database.name', 'myapp');  // 'myapp' (default)

use Italix\Contracts\DataContainer;

class FormData extends DataContainer
{
    /**
     * Sanitize all string values.
     */
    public function sanitize(): self
    {
        foreach ($this->data as $key => $value) {
            if (is_string($value)) {
                $this->data[$key] = trim(strip_tags($value));
            }
        }
        return $this;
    }
}

$input = new FormData($_POST);
$input->sanitize();
$clean_name = $input->get('name');

use Italix\Contracts\TableMeta;
use Italix\Contracts\ColumnMeta;

class UsersTable implements TableMeta
{
    private array $columns;

    public function describe_columns(): iterable
    {
        return $this->columns;
    }

    public function describe_column(string $name): ?ColumnMeta
    {
        return $this->columns[$name] ?? null;
    }
}

use Italix\Contracts\ColumnMeta;

class VarcharColumn implements ColumnMeta
{
    public function get_name(): string { return $this->name; }
    public function get_type(): string { return 'VARCHAR'; }
    public function is_nullable(): bool { return $this->nullable; }
    public function is_primary_key(): bool { return false; }
    public function get_length(): ?int { return $this->length; }
    public function get_default() { return $this->default; }
    public function has_default(): bool { return $this->hasDefault; }
}

use Italix\Forms\FormMeta;

// Any TableMeta implementation works
$form = new FormMeta($usersTable);

// DelegatedTableMeta enables delegation
$form = new FormMeta($thingsTable);
$form->delegate('Book');  // Merges Thing + Book columns