PHP code example of fabiomez / data-extractor

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

    

fabiomez / data-extractor example snippets


/**
 * @extractable
 *   {@start integer}
 *   {@length integer}
 */

/**
 * @extractable
 *   {@index mixed}
 */

/**
 * @extractable
 *   {@pattern string}
 *   {@index integer}
 */


class MyModel
{
    /**
     * @extractable
     *    {@start 0}
     *    {@length 10}
     * @otherTag from prop 1
     */
    public $prop1;

    /**
     * @extractable
     *    {@start 10}
     *    {@length 11}
     * @otherTag from prop 2
     */
    public $prop2;
}

use Fabiomez\DataExtractor\Extractor;
use Fabiomez\DataExtractor\DocBlockParser;
use Fabiomez\DataExtractor\ValueGetters\ArrayValueGetter;
use Fabiomez\DataExtractor\ValueGetters\RegexValueGetter;
use Fabiomez\DataExtractor\ValueGetters\SubstringValueGetter;

//Array extractor
$extractor = new Extractor(
    DocBlockParser::createInstance(),
    new ArrayValueGetter()
);

//Regex extractor
$extractor = new Extractor(
    DocBlockParser::createInstance(),
    new RegexValueGetter()
);

//Substring extractor
$extractor = new Extractor(
    DocBlockParser::createInstance(),
    new SubstringValueGetter()
);

use Fabiomez\DataExtractor\ExtractorFactory;

$factory = new ExtractorFactory();

//Array extractor
$extractor = $factory->createArrayExtractor();

//Regex extractor
$extractor = $factory->createRegexExtractor();

//Substring extractor
$extractor = $factory->createSubstringExtractor();

//By namespace
$extractedModel = $extractor->extract(MyModel::class, 'First dataSecond Data');

//By instance
$extractedModel = $extractor->extract(new MyModel(), 'First dataSecond Data');

echo $extractedModel->prop1; //will give 'First data'
echo $extractedModel->prop2; //will give 'Second data'

$extractedModel = $extractor->extract(
    MyModel::class,
    'First dataSecond Data',
    function ($model, $propertiesSchema) {
        foreach ($propertiesSchema as $property => $schema) {
            $model->{$property} .= $schema['otherTag'];
        }
    }
);

echo $extractedModel->prop1; //will give 'First data from prop 1'
echo $extractedModel->prop2; //will give 'Second data from prop 2'