PHP code example of danilovl / parameter-bundle

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

    

danilovl / parameter-bundle example snippets



// config/bundles.php

return [
    // ...
    Danilovl\ParameterBundle\ParameterBundle::class => ['all' => true]
];

 declare(strict_types=1);

namespace Danilovl\ParameterBundle\Interfaces;

interface ParameterServiceInterface
{
    public function get(
        string $key,
        string $delimiter = null,
        bool $ignoreNotFound = false
    ): array|bool|string|int|float|UnitEnum|null;

    public function getString(string $key, string $delimiter = null): string;
    public function getStringOrNull(string $key, string $delimiter = null): ?string;

    public function getInt(string $key, string $delimiter = null): int;
    public function getIntOrNull(string $key, string $delimiter = null): ?int;

    public function getFloat(string $key, string $delimiter = null): float;
    public function getFloatOrNull(string $key, string $delimiter = null): ?float;

    public function getBoolean(string $key, string $delimiter = null): bool;
    public function getBooleanOrNull(string $key, string $delimiter = null): ?bool;

    public function getArray(string $key, string $delimiter = null): array;
    public function getArrayOrNull(string $key, string $delimiter = null): ?array;

    public function getUnitEnum(string $key, string $delimiter = null): UnitEnum;
    public function getUnitEnumOrNull(string $key, string $delimiter = null): ?UnitEnum;

    public function has(string $key, string $delimiter = null): bool;
}


 declare(strict_types=1);

namespace App\Controller;

use Danilovl\ParameterBundle\Interfaces\ParameterServiceInterface;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

class BaseController extends AbstractController
{
    protected function createPagination(
        Request $request,
        $query,
        int $page = null,
        int $limit = null,
        array $options = null
    ): PaginationInterface {
        $page = $page ?? $this->get(ParameterServiceInterface::class)
                ->getInt('pagination::default::page', '::');

        $limit = $limit ?? $this->get(ParameterServiceInterface::class)
                ->getInt('pagination.default.limit');

        $pagination = $this->get('knp_paginator');
        if ($options !== null) {
            $pagination->setDefaultPaginatorOptions($options);
        }

        return $pagination->paginate(
            $query,
            $request->query->getInt('page', $page),
            $request->query->getInt('limit', $limit)
        );
    }
}

 declare(strict_types=1);

namespace App\Service;

use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Danilovl\ParameterBundle\Interfaces\ParameterServiceInterface;

class UserService
{
    public function __construct(private ParameterServiceInterface $parameterService)
    {
    }
    
    public function getUserRoles(): array
    {
        return $this->parameterService->getArray('user.roles');
    }
}

 declare(strict_types=1);

namespace App\Service;

use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Danilovl\ParameterBundle\Interfaces\ParameterServiceInterface;

class WidgetService
{
    public function __construct(private ParameterServiceInterface $parameterService)
    {
    }
    
    public function getWidgetName(): string
    {
        return $this->parameterService->get(key: 'widget.name', ignoreNotFound: true) ?? 'default widget name';
    }
}
yaml
# config/services.yaml

parameters:
  locale: 'en'
  debug: false
  price: 200.00
  volume: 0.00
  project_namespace: 'App'
  pagination:
    default:
      page: 1
      limit: 25
  google:
    api_key: 'AzT6Ga0A46K3pUAdQKLwr-zT6Ga0A46K3pUAdQKLwr'
    analytics_code: 'UA-X000000'
twig
parameter_get
parameter_get_string
parameter_get_string_or_null
parameter_get_int
parameter_get_int_or_null
parameter_get_float
parameter_get_float_or_null
parameter_get_boolean
parameter_get_boolean_or_null
parameter_get_array
parameter_get_array_or_null
parameter_get_unit_enum
parameter_get_unit_enum_or_null
parameter_has