PHP code example of vertigolabs / data-aware

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

    

vertigolabs / data-aware example snippets


use VertigoLabs\DataAware\DataAwareTrait;
use VertigoLabs\DataAware\DataAwareInterface;

class EmployeeProfile implements DataAwareInterface
{
use DataAwareTrait;

    public function __construct(array $data)
    {
        $defaults = [
            'firstName' => 'Not provided',
            'lastName' => 'Not provided',
            'email' => 'Not provided'
        ];

        // Merging incoming data with defaults
        $data = array_merge($defaults, $data);

        $this->setData($data);
    }

    public function displayProfile()
    {
        foreach ($this->getData() as $key => $value) {
            echo ucfirst($key) . ": " . $value . "\n";
        }
    }

}

// Instantiate an EmployeeProfile with some initial data
$employee = new EmployeeProfile([
    'firstName' => 'John',
    'email' => '[email protected]'
]);

// Display the profile
$employee->displayProfile();

// Merge new data into the profile
$employee->mergeData([
    'lastName' => 'Doe',
    'position' => 'Software Developer',
    'address'=>[
        'street'=>'123 Main St',
        'city'=>'Anytown',
        'state'=>'NY','zip'=>'12345'
    ]
]);

// Display the updated profile
$employee->displayProfile();

// Retrieve a specific piece of data
echo "First Name: " . $employee->getData('firstName') . "\n";

// Check for the existence of a specific piece of data
echo "Does the profile have a position? " . ($employee->hasData('position') ? 'Yes' : 'No') . "\n";

// Use dot notation to retieve data from nested arrays
echo "City: " . $employee->getData('address.city') . "\n";

// Retrieve an an array of a subset of data
$name = $employee->getData(['firstName', 'lastName']);

// Display the subset of data
echo "Name: " . $name['firstName'] . " " . $name['lastName'] . "\n";

// Create an array of a subset of data with custom key names
$address = $employee->getData([
    'line1' => 'address.street',
    'locality' => 'address.city',
    'region' => 'address.state',
    'postalCode' => 'address.zip'
]);

// Display the custom subset of data
echo "Address: " . $address['line1'] . "\n";
echo "City: " . $address['locality'] . "\n";
echo "State: " . $address['region'] . "\n";
echo "Zip: " . $address['postalCode'] . "\n";