PHP code example of time2split / time2configure

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

    

time2split / time2configure example snippets


use Time2Split\Config\Configurations;

// Some configuration possibly loaded from an external file
$loadedConfig = [
    'text' => [
        'locale' => 'en_US',
        'encoding' => 'UTF-8'
    ],
    'database' => [
        'driver' => 'mysql',
        'port' => 3306,
        'database' => 'db',
        'username' => 'zuri',
        'password' => 'xxx',
    ],
];
$config = Configurations::ofTree($loadedConfig);

echo $config['text.locale'], "\n";
echo $config['database.driver'], "\n";

$view = $config->subTreeView('database');
print_r($view->toArray());

$config['database.driver'] = 'sqlite';
$config['database'] = 'an internal value';
print_r($config->toArray());

// Get a new configuration instance with an interpolator
$iconfig = Configurations::treeCopyOf($config, Interpolators::recursive());

$iconfig['interpolated'] = '${text.locale} and ${text.encoding}';
echo $iconfig['interpolated'], "\n";

$iconfig['text.locale'] = 'UTF-16';
echo $iconfig['interpolated'], "\n";

$default = [
    'text' => [
        'locale' => 'en_US',
        'encoding' => 'UTF-8'
    ]
];
$default = Configurations::ofTree($default);
/*
    $default is the config to search for
    the unexistant entries of $config.
*/
$config = Configurations::emptyChild($default);

echo $config['text.locale'], "\n";

$config['text.locale'] = 'override';
echo $config['text.locale'], "\n";

$config['text.locale'] = null;
echo $config['text.locale'], "\n";

unset($config['text.locale']);
echo $config['text.locale'], "\n";
 

$tree = [
    'text' => [
        'locale' => 'en_US',
        'encoding' => 'UTF-8'
    ]
];
$config = Configurations::ofTree($tree);
$doEcho = Entries::consumeEntry(function ($key, $val) {
    echo "$key=$val\n";
});
$echoConfig = Configurations::doOnRead($config, $doEcho);

$echoConfig['text.locale'];
iterator_to_array($echoConfig);

$config = Configurations::ofTree();
$config['url.php'] = 'https://www.php.net/support';

// Dereference an url and retrieves the header
$getHeader = Entries::mapValue(function ($value, $key) {

    if (\str_starts_with($key, 'url'))
        return \get_headers($value, true);

    return $value;
});
$deref = Configurations::mapOnRead($config, $getHeader);

print_r($deref['url.php']);
bash
### Output ###
Array
(
    [0] => HTTP/1.1 200 OK
    [Server] => myracloud
    [Date] => Mon, 08 Apr 2024 20:00:43 GMT
    [Content-Type] => text/html; charset=utf-8
    [Transfer-Encoding] => chunked
    [Connection] => close
    [Content-language] => en
    [Permissions-Policy] => interest-cohort=()
    [X-Frame-Options] => SAMEORIGIN
    [Link] => <https://www.php.net/support>; rel=shorturl
    [Expires] => Mon, 08 Apr 2024 20:00:43 GMT
    [Cache-Control] => max-age=0
    [ETag] => "myra-3087d513"
)