<?php
namespace App\Sabre\LineParser\Types; class FloatWithOptionalPercent extends \App\Utils\BaseParser\TextParser\Types\FloatRaw { protected $extractRegexp = "/^(?P<rawData>[P0-9 \.]__LENGHTS__|[0-9 \.]__LENGHTS__)/"; public function __construct($options, $parent = null){ $options['allowSpaces'] = true; parent::__construct($options, $parent); } protected function convert(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret) { $trimmedData = ltrim($ret->extractedData); $trimmedData0 = ltrim($ret->extractedData, ' 0'); if ($this->options['allowSpaces'] && $trimmedData==='') { $this->extractedData = [ 'value' => 0.0, 'type' => null, ]; } elseif ($trimmedData0==='') { $this->extractedData = [ 'value' => 0.0, 'type' => null, ]; } elseif ($trimmedData0!=='') { if ($trimmedData[0]=='P') { $this->extractedData = [ 'value' => (float)substr($trimmedData, 1), 'type' => 'percent', ]; } else { $this->extractedData = [ 'value' => (float)$trimmedData0, 'type' => 'amount', ]; } } else { $this->extractedData = [ 'value' => (float)$trimmedData0, 'type' => null, ]; } $ret->extractedData = $this->extractedData; return $ret; } } 