PHP code example of minvws / icore-php-typearray

1. Go to this page and download the library: Download minvws/icore-php-typearray 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/ */

    

minvws / icore-php-typearray example snippets




use MinVWS\TypeArray\TypeArray;

[
        'name' => 'Ada',
        'age' => 37,
        'active' => true,
        'address' => [
            'city' => 'Amsterdam',
        ],
        'preferences' => [
            'locale' => 'nl',
        ],
    ],
]);

$name = $data->getString('[user][name]');
$age = $data->getInt('[user][age]');
$active = $data->getBool('[user][active]');
$locale = $data
    ->getTypeArray('[user][preferences]')
    ->getString('[locale]');

$city = $data->getString('[user][address][city]');

$config = new TypeArray([
    'description' => null,
]);

$environment = $config->getString('[environment]', 'production');
$description = $config->getString('[description]', 'No description');

$name = $config->getStringOrNull('[name]');
$retries = $config->getIntOrNull('[retries]');
$ratio = $config->getFloatOrNull('[ratio]');
$options = $config->getTypeArrayOrNull('[options]');

$order = new TypeArray([
    'shipping' => [
        'country' => 'NL',
    ],
    'items' => [
        ['sku' => 'ABC-123', 'quantity' => 2],
        ['sku' => 'XYZ-987', 'quantity' => 1],
    ],
]);

$country = $order
    ->getTypeArray('[shipping]')
    ->getString('[country]');

foreach ($order->getIterable('[items]') as $item) {
    if ($item instanceof TypeArray) {
        $sku = $item->getString('[sku]');
        $quantity = $item->getInt('[quantity]');
    }
}

$payload = TypeArray::fromJson('{"id":42,"status":"open"}');

$id = $payload->getInt('[id]');
$status = $payload->getString('[status]');

$array = $payload->toArray();
$json = json_encode($payload, JSON_THROW_ON_ERROR);

$data->exists('[user][name]');
$data->isNullOrNotExists('[user][middle_name]');
$data->isTypeArray('[user][preferences]');
$data->isEmpty();

$empty = TypeArray::empty();

use MinVWS\TypeArray\Exception\IncorrectDataTypeException;
use MinVWS\TypeArray\Exception\InvalidIndexException;

$data = new TypeArray([
    'count' => '42',
]);

try {
    $data->getInt('[missing]');
} catch (InvalidIndexException $exception) {
    echo $exception->getMessage();
}

try {
    $data->getInt('[count]');
} catch (IncorrectDataTypeException $exception) {
    echo $exception->getMessage();
}

use Doctrine\DBAL\Types\Type;
use MinVWS\TypeArray\Doctrine\DBAL\Types\TypeArrayType;

if (!Type::hasType(TypeArrayType::TYPE)) {
    Type::addType(TypeArrayType::TYPE, TypeArrayType::class);
}
shell
composer