ExportDocBlock.php

<?php

namespace Tlf\Scrawl\FileExt;

/**
 * Export docblock content above `@export(key)`
 * @featured
 */
class ExportDocBlock {

    use \BetterRegTrait;
    use \BetterRegShorthandsTrait;

    protected $regData = [
        'key' => '([^\)]*)',
        'export.key' => '/\@export\(([^\)]*)\)/',
        // see `matchExports()`
        'Exports' => '/((?:.|\n)*) *(\@export.*)/',
    ];

    protected $extensions = [];

    public function __construct(){} 

    /**
     * get an array of docblocks
     * @return array of docblocks, like `[0=>['raw'=>'/*...', 'clean'=>'...'], 1=>...]`
     */
    public function get_docblocks(string $str): array{
        $blocks = \Tlf\Scrawl\Utility\DocBlock::extractBlocks($str);
        return $blocks;
    }

    /**
     * get an array of exported text
     * @param $docblocks Array of docblocks
     */
    public function get_exports(array $docblocks){
        $exports = [];
        foreach ($docblocks as $db){
            $exports = array_merge($exports,
               $this->match('Exports', $db->clean)[0]
            );
        }

        return $exports;
    }

    /**
     *
     * get array of exports
     *
     * @param $info array of match info as generated by BetterRegex 
     * @param $comment The full text being exported
     * @param $export The `@export(key) whatever text` line
     * @return array like `['key'=>'exported docblock text']`
     *
     * @todo improve the `$info` array from BetterRegex, maybe?
     * @example `$info` is like ... kinda like `[key=>0, string=>$original, func=>matchExports, match=>[array returned by preg_match], 'allMatches'=>[array returned by preg_match?] ]`
     */
    public function matchExports(array $info, string $comment, string $export): array{

        $comment = \Tlf\Scrawl\Utility\Main::trimTextBlock($comment);
        $key = $this->match('export.key',$export);

        if (!is_array($key))return [];

        return [$key[0]=>$comment];
    }

    /**
     *
     * Get the key from `@export(key) whatever`
     *
     * @param $info the array of match info from BetterRegex
     * @param $key an array containing the `key` portion of `@export(key) whatever)`. 
     * @return the string key
     */
    public function matchExport_Key(array $info, string $key): string{
        return $key;
    }

}