GlobalXtreme Parser Pattern

2.0.0 2024-05-11 02:40 UTC

This package is auto-updated.

Last update: 2024-11-11 03:42:49 UTC


README

Version Total Downloads License

Install with composer

To install with Composer, simply require the latest version of this package.

composer require globalxtreme/parser

You can install parser class with command.

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

Using

  • Install parser class.
    php artisan make:gx-parser Custom\CustomParser 
  • You can add custom function in your parser
    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;
        } 
    }
  • Register your parser to model.
    use App\Packages\Parser\Custom\CustomParser;
    use GlobalXtreme\Parser\Trait\HasParser;
    
    class Custom extends Model
    {
        use HasParser;
        
        public $parserClass = CustomParser::class;
    }
  • Using parser with controller.
    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);
        }
    }