PHP code example of devium / toml

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

    

devium / toml example snippets


$toml = <<<TOML

# This is a TOML document

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

TOML;

dump(\Devium\Toml\Toml::decode($toml, asArray: true));
dump(\Devium\Toml\Toml::decode($toml, asArray: false));

// or use global helper
dump(toml_decode($toml, asArray: false));
dump(toml_decode($toml, asArray: true));

$data = [
  "title" => "TOML Example",
  "owner" => [
    "name" => "Tom Preston-Werner",
    "dob" => "1979-05-27T15:32:00.000Z",
  ],
];

dump(\Devium\Toml\Toml::encode($data));
// or use global helper
dump(toml_encode($data));

dump(toml_decode($toml, true));

array:9 [
  "offset-date-time-1" => Devium\Toml\TomlDateTime @296638320 {#29
    date: 1979-05-27 07:32:00.0 +00:00
  }
  "offset-date-time-2" => Devium\Toml\TomlDateTime @296638320 {#35
    date: 1979-05-27 00:32:00.0 -07:00
  }
  "offset-date-time-3" => Devium\Toml\TomlDateTime @296638320 {#41
    date: 1979-05-27 00:32:00.999999 -07:00
  }
  "offset-date-time-4" => Devium\Toml\TomlDateTime @296638320 {#47
    date: 1979-05-27 07:32:00.0 +00:00
  }
  "local-date-time-1" => Devium\Toml\TomlLocalDateTime {#55
    +year: 1979
    +month: 5
    +day: 27
    +hour: 7
    +minute: 32
    +second: 0
    +millisecond: 0
  }
  "local-date-time-2" => Devium\Toml\TomlLocalDateTime {#61
    +year: 1979
    +month: 5
    +day: 27
    +hour: 0
    +minute: 32
    +second: 0
    +millisecond: 999
  }
  "local-date-1" => Devium\Toml\TomlLocalDate {#59
    +year: 1979
    +month: 5
    +day: 27
  }
  "local-time-1" => Devium\Toml\TomlLocalTime {#70
    +millisecond: 0
    +hour: 7
    +minute: 32
    +second: 0
  }
  "local-time-2" => Devium\Toml\TomlLocalTime {#77
    +millisecond: 999
    +hour: 0
    +minute: 32
    +second: 0
  }
]

$data = [
    'DateTimeInterface' => new DateTimeImmutable('1979-05-27T07:32:00Z'),
    'TomlDateTimeInterface' => new TomlDateTime('1979-05-27T07:32:00Z'),
];

toml_decode($toml, asFloat: true);
// or
\Devium\Toml\Toml::decode($toml, asFloat: true);
toml
DateTimeInterface = 1979-05-27T07:32:00.000Z
TomlDateTimeInterface = 1979-05-27T07:32:00.000Z