PHP code example of cpoint-eu / settings-bundle

1. Go to this page and download the library: Download cpoint-eu/settings-bundle 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/ */

    

cpoint-eu / settings-bundle example snippets


// config/bundles.php

return [
    // ...
    CreativePoint\SettingsBundle\CreativePointSettingsBundle::class => ['all' => true],
];

//...
use CreativePoint\SettingsBundle\Model\SettingsDtoInterface;

class MySettingsDto implements SettingsDtoInterface
{
    private const SETTINGS_ID = 'mySettings';

    public function __construct(
        public ?string $someValue = 'default value',
        public ?int $someNumber = 254,
    ) {
    }

    public static function getSettingsId(): string
    {
        return self::SETTINGS_ID;
    }
}

// ...
use CreativePoint\SettingsBundle\Factory\SettingFactoryInterface;

// ...

// Save settings to the database
public function saveSettings(SettingFactoryInterface $factory)
{
    // Set data from array
    $factory->setSettingsData('mySettings', [
        'someValue' => 'new value',
        'someNumber' => 123,
    ]);
    
    // Set data from DTO
    $dto = new MySettingsDto(
        'new value',
        123,
    );
    
    $factory->setSettingsDataFromDto($dto);
}


// ...
use CreativePoint\SettingsBundle\Provider\SettingsProvider;

// ...

// Save settings to the database
public function loadSettings(SettingsProviderInterface $provider)
{
    // Load data from DB by DTO::SETTINGS_ID and return DTO
    $settings = $provider->loadSettingsDto('mySettings');
    // ...or load SettingsEntity itself
    $settings = $provider->getSettingsEntity('mySettings');
    
    // You can also load your DTO from array data
    $settings = $provider->loadSettingsDtoFromArray('mySettings', [
        'someValue' => 'new value',
        'someNumber' => 123,
    ]);
}