PHP code example of xervice / array-handler

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

    

xervice / array-handler example snippets



declare(strict_types=1);

namespace XerviceTest\ArrayHandler\Business\Helper;


use Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface;

class TestFieldHandler implements FieldHandlerPluginInterface
{
    /**
     * @param array $data
     * @param mixed $fieldName
     * @param string $config
     *
     * @return array
     */
    public function handleSimpleConfig(array $data, $fieldName, string $config): array
    {
        $data[$fieldName] = $config;

        return $data;
    }

    /**
     * @param array $data
     * @param mixed $fieldName
     * @param array $config
     *
     * @return array
     */
    public function handleNestedConfig(array $data, $fieldName, array $config): array
    {
        $data[$fieldName] = $config['testvalue'] ?? null;

        return $data;
    }

    /**
     * @param array $data
     * @param array $config
     *
     * @return array
     */
    public function handleArrayConfig(array $data, array $config): array
    {
        $data = $config['testvalue'] ?? null;

        return $data;
    }


    /**
     * @param array $data
     * @param mixed $fieldName
     * @param callable $config
     *
     * @return array
     */
    public function handleCallableConfig(array $data, $fieldName, callable $config): array
    {
        $data[$fieldName] = $config($data[$fieldName]);

        return $data;
    }

}

$data = [
    [
        'keyOne' => 'valueOne',
        'keyTwo' => 'valueTwo',
        'keyThree' => 'valueThree',
        'keyFour' => 'valueFour',
        'keyFive' => 'valueFive',
        'keySix' => [
            'isNested' => [
                'foo' => 'bar'
            ],
            'multi' => 'array'
        ],
        'keySeven' => [
            [
                'data' => 'test'
            ],
            [
                'data' => 'test'
            ],
            [
                'data' => 'test'
            ],
            [
                'data' => 'test'
            ]
        ],
        'keyEight' => [
            'eightsElement' => 'string',
            'nesting' => [
                [
                    'foo' => [
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar']
                    ]
                ],
                [
                    'foo' => [
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar']
                    ]
                ],
                [
                    'foo' => [
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar']
                    ]
                ],
                [
                    'foo' => [
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar']
                    ]
                ],
                [
                    'foo' => [
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar'],
                        ['subfoo' => 'bar']
                    ]
                ]
            ]
        ]
    ]
];

$config = [
  '*' => [
      'keyOne',
      'keyFour',
      [
          'keyFive',
          'keyTwo' => function ($value) {
              return $value . 'DONE';
          },
          'keyThree' => [
              'testvalue' => 'TEST'
          ]
      ],
      [
          'keySix' => [
              'isNested' => function ($value) {
                  return 'multiTest';
              }
          ],
          'keySix.*' => function ($value) {
              return $value . 'NESTED';
          }
      ],
      [
          'keySeven.*' => [
              FieldHandlerPluginInterface::HANDLE_THIS => [
                  'testvalue' => [
                      'data' => 'tested!'
                  ]
              ]
          ],
          'keyEight' => [
              [
                  'nesting.*' => [
                      [
                          'foo.*' => [
                              FieldHandlerPluginInterface::HANDLE_THIS => [
                                  'testvalue' => [
                                      'subfoo' => 'bartested'
                                  ]
                              ]
                          ]
                      ]
                  ]
              ]
          ]
      ]
  ]
];

$result = [
  [
      'keyOne' => 'keyOne',
      'keyTwo' => 'valueTwoDONE',
      'keyThree' => 'TEST',
      'keyFour' => 'keyFour',
      'keyFive' => 'keyFive',
      'keySix' => [
          'multi' => 'arrayNESTED',
          'isNested' => 'multiTestNESTED'
      ],
      'keySeven' => [
          [
              'data' => 'tested!'
          ],
          [
              'data' => 'tested!'
          ],
          [
              'data' => 'tested!'
          ],
          [
              'data' => 'tested!'
          ]
      ],
      'keyEight' => [
          'eightsElement' => 'string',
          'nesting' => [
              [
                  'foo' => [
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested']
                  ]
              ],
              [
                  'foo' => [
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested']
                  ]
              ],
              [
                  'foo' => [
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested']
                  ]
              ],
              [
                  'foo' => [
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested']
                  ]
              ],
              [
                  'foo' => [
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested'],
                      ['subfoo' => 'bartested']
                  ]
              ]
          ]
      ]
  ]
];