PHP code example of thamtech / yii2-yaml

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

    

thamtech / yii2-yaml example snippets



use thamtech\yaml\helpers\Yaml;

$data = Yaml::decode($yaml);
print_r($data);

# Array
# (
#     [people] => Array
#         (
#           [john] => Array
#               (
#                   [id] => 1
#                   [name] => John
#               )
#           [bob] => yii\helpers\ReplaceArrayValue Object
#               (
#                   [value] => Array
#                       (
#                           [id] => 1001
#                           [name] => Bob
#                       )
#               )
#           [jane] => yii\helpers\UnsetArrayValue Object
#               (
#               )
#           [susan] => Symfony\Component\Yaml\Tag\TaggedValue Object
#               (
#                   [tag:Symfony\Component\Yaml\Tag\TaggedValue:private] => lookupIdFromEmployeeNumber
#                   [value:Symfony\Component\Yaml\Tag\TaggedValue:private] => Array
#                       (
#                           [employee_number] => 1234
#                           [name] => Susan
#                       )
#               )
#         )
# )


use thamtech\yaml\helpers\Yaml;

$data = Yaml::decode($yaml, [
    'on lookupIdFromEmployeeNumber' => function ($event) {
        // get the value associated with the `!lookupIdFromEmployeeNumber` tag
        $value = $event->value;
        
        // find the person's id and add it to the value
        $value['id'] = Employee::find()
            ->select(['id'])
            ->where(['employee_number' => $value['employee_number']])
            ->scalar();
        
        // set the updated value in the event; the value set in `value` will
        // replace the `TaggedValue` object in the parsed yaml data as long as we
        // mark that the event was handled
        $event->value = $value;
        $event->handled = true;
        
        // as a shortcut, the following is equivalent to the previous two lines:
        $event->handleValue($value);
    },
]);

print_r($data);
# Array
# (
#     [people] => Array
#         (
#           [john] => Array
#               (
#                   [id] => 1
#                   [name] => John
#               )
#           [bob] => yii\helpers\ReplaceArrayValue Object
#               (
#                   [value] => Array
#                       (
#                           [id] => 1001
#                           [name] => Bob
#                       )
#               )
#           [jane] => yii\helpers\UnsetArrayValue Object
#               (
#               )
#           [susan] => Array
#               (
#                   [employee_number] => 1234
#                   [name] => Susan
#                   [id] => 1004
#               )
#         )
# )


print_r($data);
# Array
# (
#     [people] => Array
#         (
#           [john] => Array
#               (
#                   [id] => 1
#                   [name] => John
#               )
#           [bob] => yii\helpers\ReplaceArrayValue Object
#               (
#                   [value] => Array
#                       (
#                           [id] => 1001
#                           [name] => Bob
#                       )
#               )
#           [jane] => yii\helpers\UnsetArrayValue Object
#               (
#               )
#           [susan] => Some\Package\EmployeeWithoutId Object
#               (
#                   [employee_number:Some\Package\EmployeeWithoutId:private] => 1234
#                   [name:Some\Package\EmployeeWithoutId:private] => Susan
#               )
#         )
# )


use Symfony\Component\Yaml\Yaml;

$yaml = Yaml::dump($data);
echo $yaml;
# people:
#     john:
#         id: 1
#         name: John
#     bob: null
#     jane: null
#     susan: null


use Symfony\Component\Yaml\Yaml;

$yaml = Yaml::dump($data);
echo $yaml;
# people:
#     john:
#         id: 1
#         name: John
#     bob: !yii/helpers/ReplaceArrayValue
#         id: 1001
#         name: Bob
#     jane: !yii/helpers/UnsetArrayValue null
#     susan: null


use thamtech\yaml\helpers\Yaml;
use Symfony\Component\Yaml\Tag\TaggedValue;

$yaml = Yaml::encode($data, [
    'on Some\Package\EmployeeWithoutId' => function ($event) {
        // get the EmployeeWithoutId object
        $value = $event->value;
        
        // decode the object into a TaggedValue object
        $event->value = new TaggedValue('lookupIdFromEmployeeNumber', [
            'employee_number' => $value->getEmployeeNumber(),
            'name' => $value->getName(),
        ]);
        $event->handled = true;
        
        // as a shortcut, the following is equivalent to setting `$event->value`
        // and setting `$event->handled = true`.
        $event->handleValue(
            new TaggedValue('lookupIdFromEmployeeNumber', [
                'employee_number' => $value->getEmployeeNumber(),
                'name' => $value->getName(),
            ])
        );
    },
]);

echo $yaml;
# people:
#     john:
#         id: 1
#         name: John
#     bob: !yii/helpers/ReplaceArrayValue/
#         id: 1001
#         name: Bob
#     jane: !yii/helpers/UnsetArrayValue null
#     susan: !lookupIdFromEmployeeNumber
#         employee_number: 1234
#         name: Susan


use Yii;

Yii::$container->set('thamtech\yaml\Parser');
Yii::$container->set('thamtech\yaml\Dumper');

// alternatively, in your application configuration:
[
    'container' => [
        'definitions' => [
            'thamtech\yaml\Parser' => [],
            'thamtech\yaml\Dumper' => [],
        ],
    ],
];

use Yii;
use thamtech\yaml\helpers\Yaml;

Yii::$container->setDefinitions([
    'thamtech\yaml\Parser' => Yaml::getParserDefinition([
        // example: we are calling getParserDefinition() to use `Yaml`'s default
        // definitions as a base, but these lines shows how we can alter those
        // default definitions. In this case, we remove the 'ReplaceArrayValue'
        // and 'UnsetArrayValue' handlers by unsetting their array keys:
        'on yii/helpers/ReplaceArrayValue' => new \yii\helpers\UnsetArrayValue(),
        'on yii/helpers/UnsetArrayValue' => new \yii\helpers\UnsetArrayValue(),
        
        // example: adding your own handler
        'on lookupIdFromEmployeeNumber' => function ($event) {
            // get the value associated with the `!lookupIdFromEmployeeNumber` tag
            $value = $event->value;
            
            // find the person's id and add it to the value
            $value['id'] = Employee::find()
                ->select(['id'])
                ->where(['employee_number' => $value['employee_number']])
                ->scalar();
            
            // set the updated value in the event; the value set in `value` will
            // replace the `TaggedValue` object in the parsed yaml data as long as we
            // mark that the event was handled
            $event->value = $value;
            $event->handled = true;
            
            // as a shortcut, the following is equivalent to the previous two lines:
            $event->handleValue($value);
        },
    ]),
    'thamtech\yaml\Dumper' => Yaml::getDumperDefinition([
        // example: we are calling getDumperDefinition() to use `Yaml`'s default
        // definitions as a base, but these lines shows how we can alter those
        // default definitions. In this case, we remove the 'ReplaceArrayValue'
        // and 'UnsetArrayValue' handlers by unsetting their array keys:
        'on yii/helpers/ReplaceArrayValue' => new \yii\helpers\UnsetArrayValue(),
        'on yii/helpers/UnsetArrayValue' => new \yii\helpers\UnsetArrayValue(),
        
        // example: adding your own handler
        'on Some\Package\EmployeeWithoutId' => function ($event) {
            // get the EmployeeWithoutId object
            $value = $event->value;
            
            // decode the object into a TaggedValue object
            $event->value = new TaggedValue('lookupIdFromEmployeeNumber', [
                'employee_number' => $value->getEmployeeNumber(),
                'name' => $value->getName(),
            ]);
            $event->handled = true;
            
            // as a shortcut, the following is equivalent to setting `$event->value`
            // and setting `$event->handled = true`.
            $event->handleValue(
                new TaggedValue('lookupIdFromEmployeeNumber', [
                    'employee_number' => $value->getEmployeeNumber(),
                    'name' => $value->getName(),
                ])
            );
        },
    ]),
]);


// alternatively, in your application configuration:
[
    'container' => [
        'definitions' => [
            'thamtech\yaml\Parser' => Yaml::getParserDefinition([
                // ...
            ]),
            'thamtech\yaml\Dumper' => Yaml::getDumperDefinition([
                // ...
            ]),
        ],
    ],
];


// ... config ...
return [
    'response' => [
        'formatters' => [
            'yaml' => [
                'class' => 'thamtech\yaml\web\YamlResponseFormatter',
                
                // you can define your own dumper config like the earlier
                // examples:
                'dumper' => [
                    // `'class' => 'thamtech\yaml\Dumper'` is assumed, but can
                    // be overridden if you extend 'thamtech\yaml\Dumper'
                    
                    'on Some\Package\EmployeeWithoutId' => function ($event) {
                        // ... handle event, see earlier example ...
                    },
                ],
            ],
        ],
    ],
];