PHP code example of bluepsyduck / common

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

    

bluepsyduck / common example snippets



use BluePsyduck\Common\Data\DataBuilder;

$dataBuilder = new DataBuilder();

// Set some values
$dataBuilder->setInteger('foo', 42)
            ->setString('bar', 'baz');

// Set a variable value unless it is a specific one
$age1 = 21;
$age2 = -1;
$dataBuilder->setInteger('age1', $age1, -1)
            ->setInteger('age2', $age2, -1); // $age2 will get ignored.

// Set a date and time value
$date = new DateTime('2038-01-19 03:14:07');
$dataBuilder->setDateTime('creationTime', $date, 'Y-m-d'); // Will set '2038-01-17' as value.

// Set an array of strings
$dataBuilder->setArray('data', ['foo', 'bar', 42], 'strval'); // Casts all array values to a string.


use BluePsyduck\Common\Data\DataContainer;

$data = [
    'foo' => 'bar',
    'first' => [
        'second' => 42
    ]
];
$dataContainer = new DataContainer($data);

// Accessing the first level
echo $dataContainer->getString('foo'); // 'bar'

// Accessing the second level
echo $dataContainer->getObject('first')->getInteger('second'); // 42
echo $dataContainer->getInteger(['first', 'second']); // 42

// Default values for undefined keys
echo $dataContainer->getFloat('missing', 13.37); // 13.37