GlobalXtreme 解析器模式

2.0.0 2024-05-11 02:40 UTC

This package is auto-updated.

Last update: 2024-09-11 03:22:00 UTC


README

Version Total Downloads License

使用composer安装

要使用Composer安装,只需要求此包的最新版本。

composer require globalxtreme/parser

您可以使用命令安装解析器类。

php artisan make:gx-parser {{ class }}

使用

  • 安装解析器类。
    php artisan make:gx-parser Custom\CustomParser 
  • 您可以在解析器中添加自定义函数
    use GlobalXtreme\Parser\BaseParser;
    
    class CustomParser extends BaseParser
    {
        use HasParser;
        
        public function tests($collections)
        {
            if (!$collections || count($collections) == 0) {
                return null;
            }
    
            $data = [];
            foreach ($collections as $collection) {
                $data[] = $collection->toArray();
            }
    
            return $data;
        } 
    }
  • 将解析器注册到模型中。
    use App\Packages\Parser\Custom\CustomParser;
    use GlobalXtreme\Parser\Trait\HasParser;
    
    class Custom extends Model
    {
        use HasParser;
        
        public $parserClass = CustomParser::class;
    }
  • 使用控制器使用解析器。
    use App\Http\Controllers\Controller;
    use App\Models\Custom;
    use App\Packages\Parser\Custom\CustomParser;
    use GlobalXtreme\Parser\Parser;
    
    class CustomController extends Controller
    {
        public function testing() 
        {
            // Get more than one data
            $customs = Custom::get();
            
            // Display data using CustomParser and your custom function 
            $results = CustomParser::get($customs);
            $results = CustomParser::tests($customs);
    
            // Display data using parser class from package 
            // with default function or custom function from CustomParser
            $results = Parser::get($customs);
            $results = Parser::tests($customs);
    
    
            // Get one data
            $custom = Custom::first();
            
            // Display data using custom parser
            $result = CustomParser::first($custom);
    
            // Display data using parser class from package
            $result = Parser::first($custom);
        }
    }