PHP code example of ylsideas / forecaster

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

    

ylsideas / forecaster example snippets


$result = forecast([
    'a-string-int' => '10',
    'a-string-float' => '1.5',
    'an-int' => 10,
    'another-int' => 1,
    'do-not-touch' => '11'
])
    ->cast('a-string-int', 'anInt', 'int')
    ->cast('a-string-float', 'aFloat', 'float')
    ->cast('an-int', 'aString', 'string')
    ->cast('another-int', 'aBoolean', 'bool')
    ->cast('do-not-touch', 'doNotTouch')
    ->get();

// $results to

[
    'anInt' => 10,
    'aFloat' => 1.5,
    'aString' => '10',
    'aBoolean' => true,
    'doNotTouch' => '11',
]   

$result = forecast([
    'onions' => [
        'have' => [
            'layers' => true,
        ]
    ]
])
    ->cast('onions.have.layers', 'ogres.do.to')
    ->get();
    
// results to

[
    'orgres' => [
        'do' => [
            'to' => true,
        ]
    ]
]               

$result = forecast([
    'anArrayOfStrings' => [
        '10', '100', '1000'
    ],
    'anArrayOfArrays' => [
        ['value' => '20'],
        ['value' => '200'],
        ['value' => '2000'],
    ]
])
    ->castAll('anArrayOfStrings', 'an-array-of-ints')
    ->castAll('anArrayOfArrays.*.value', 'an-array-of-all-values')
    ->get();
    
// results to

[
    'an-array-of-ints' => [
        10, 100, 1000
    ],
    'an-array-of-all-values' => [
        20, 200, 2000
    ],
]               

$object = new stdClass();
$object->objField = [
    'arrField' => '10',
];

$result = forecast($object)
    ->cast('objField.arrField', 'my_field', 'int')
    ->get();
    
// results to

[
    'my_field' => 10,
]               

$result = forecast([
    'an-array' => [
        ['attr' => '10'],
        ['attr' => '100'],
        ['attr' => '1000'],
    ]
])
    ->castItems('an-array', 'anArray', function (Forecaster $forecaster) {
        $forecaster->cast('attr', 'Attribute', 'int');
    })
    ->get();
    
// results to

[
    'anArray' => [
        ['Attribute' => 10],
        ['Attribute' => 100],
        ['Attribute' => 1000],
    ]
]    

Forecaster::transformer('csv', function ($value) {
    return str_getcsv($value);
});

$results = forecast([
    'test' => '1, 2, 3',
])
    ->cast('test', 'output', 'csv')
    ->get();
    
// results to

[
    'output' => ['1', '2', '3']
]

$results = forecast([
    'test' => '1, 2, 3',
])
    ->cast('test', 'output', function ($value) {
        return str_getcsv($value);
    })
    ->get();
    
// results to

[
    'output' => ['1', '2', '3']
]

public class CsvTransformer implements CastingTransformer
{
    public function cast(string $in, string $out, array $item, array $processed)
    {
        return str_getcsv($item[$in]);
    }
}

$results = forecast([
    'test' => '1, 2, 3',
])
    ->cast('test', 'output', new CsvTransformer())
    ->get();
    
// results to

[
    'output' => ['1', '2', '3']
]

$processed = Forecaster::make([
    'test' => '10',
])
    ->when(true, function (Forecaster $caster) {
        $caster->cast('test', 'output', 'int');
    })
    ->get();
    
// results to

[
    'output' => 10, 
]    

$processed = Forecaster::make([
    'test' => '10',
])
    ->when(
        function ($item) {
            return $item['test'] > 1;
        }, 
        function (Forecaster $caster) {
            $caster->cast('test', 'output', 'int');
        }
    )
    ->get();
    
// results to

[
    'output' => 10, 
]    

$results = forecast([
    'test' => '10',
])
    ->cast('test', 'output')
    ->get(SomeClass::class);

$object = forecast([
    'test' => '10',
])
    ->cast('test', 'output')
    ->get('object');

$results = forecast([
    'test' => '10',
])
    ->cast('test', 'output')
    ->get(function ($processed) {
        return new SomeClass($processed['output']);
    });

$collection = collect([
    ['test' => '123.456'],
    ['test' => '789.101112']
])
    ->forecast(function (Forecaster $forecast) {
        $forecast->cast('test', 'output', 'float');
    })
    ->toArray();
    
// results to

[
    ['output' => 123.456],
    ['output' => 789.101112],
]

$collection = collect([
    ['test' => '123.456'],
    ['test' => '789.101112']
])
    ->forecast(
        function (Forecaster $forecast) {
            $forecast->cast('test', 'output', 'float');
        }, 
        'object'
    );