コード例 #1
0
 public function parse()
 {
     if ($this->parse_called) {
         return $this->getParsed();
     }
     if ($this->options['debug']) {
         echo "Trying to match value '{$this->value}' \n";
     }
     // parse 1 кВт / 1 кВт / 2 кВт, select the max value
     $exploded_values = explode('/', $this->value);
     if ($this->options['debug']) {
         echo "Exploded values are " . var_export($exploded_values, true) . " \n";
     }
     $result = array();
     foreach ($exploded_values as $value) {
         $parser = new PowerSpecParser(trim($value));
         if ($this->options['debug']) {
             $parser->setOption('debug', true);
         }
         $parsed = $parser->parse();
         if ($parsed) {
             $result[$parsed[0]['value']] = $parsed;
         }
     }
     if (!empty($result)) {
         $max_key = max(array_keys($result));
         $this->parsed = $result[$max_key];
         $this->parsed = $this->validateParsed($this->parsed);
         $this->matched_regex = '';
         $this->parse_called = true;
         if ($this->options['debug']) {
             echo "Parse result is " . var_export($this->parsed, true) . " \n";
         }
         return $this->parsed;
     }
     if ($this->options['debug']) {
         echo "Parse result is empty \n";
     }
     $this->parsed = false;
     $this->parse_called = true;
     $this->matched_regex = '';
     return false;
 }
コード例 #2
0
 public function parse()
 {
     if ($this->parse_called) {
         return $this->getParsed();
     }
     if ($this->options['debug']) {
         echo "Trying to match value '{$this->value}' \n";
     }
     $measures = $this->measures;
     $measures_list = implode('|', $this->getMeasureAliases($measures));
     $parse_exploded = function ($exploded_values, &$this, $pattern) {
         if ($this->options['debug']) {
             echo "Exploded values are " . var_export($exploded_values, true) . " \n";
         }
         $result = array();
         foreach ($exploded_values as $value) {
             $parser = new PowerSpecParser(trim($value));
             if ($this->options['debug']) {
                 $parser->setOption('debug', true);
             }
             $parsed = $parser->parse();
             if ($parsed) {
                 $result = array_merge($result, $parsed);
             }
         }
         if (!empty($result)) {
             $this->parsed = $result;
             $this->parsed = $this->validateParsed($this->parsed);
             $this->matched_regex = $pattern;
             $this->parse_called = true;
             if ($this->options['debug']) {
                 echo "Parse result is " . var_export($this->parsed, true) . " \n";
             }
             return $this->parsed;
         } else {
             if ($this->options['debug']) {
                 echo "Parse result is empty \n";
             }
             $this->parsed = false;
             $this->parse_called = true;
             $this->matched_regex = '';
             return false;
         }
     };
     // First, try to process special cases.
     // parse 40 Вт (номинальная), 140 Вт (максимальная)
     $pattern = '/^(.+)\\s(' . $measures_list . ')\\s\\(номинальная\\),\\s(.+)\\s(' . $measures_list . ')\\s\\(максимальная\\)$/mi';
     if (preg_match($pattern, $this->value)) {
         $exploded_values = explode(',', $this->value);
         if (!empty($exploded_values) && count($exploded_values) == 2) {
             return $parse_exploded($exploded_values, $this, $pattern);
         }
     }
     // 30 л.с., 22000 Вт
     $pattern = '/^(.+)\\s(' . implode('|', $measures['л.с.']) . '),\\s(.+)\\s(' . $measures_list . ')$/mi';
     if (preg_match($pattern, $this->value)) {
         $exploded_values = explode(',', $this->value);
         if (!empty($exploded_values) && count($exploded_values) == 2) {
             return $parse_exploded(array($exploded_values[0]), $this, $pattern);
         }
     }
     // 1600 Вт / 2.1 л. с.
     $pattern = '/^(.+)\\s(' . $measures_list . ')\\s\\/\\s(.+)\\s(' . implode('|', $measures['л.с.']) . ')$/mi';
     if (preg_match($pattern, $this->value)) {
         $exploded_values = explode('/', $this->value);
         if (!empty($exploded_values) && count($exploded_values) == 2) {
             return $parse_exploded(array($exploded_values[1]), $this, $pattern);
         }
     }
     // 1600 Вт (макс.|пиковая 2000 Вт)
     $electric_measures = array_diff_key($measures, array('л.с.' => ''));
     $electric_measures = $this->getMeasureAliases($electric_measures);
     $electric_measures = implode('|', $electric_measures);
     $pattern = '/^((.+)\\s(' . $electric_measures . '))\\s\\((макс\\.|пиковая)\\s((.+)\\s(' . $electric_measures . '))\\)$/mi';
     $matches = array();
     if (preg_match($pattern, $this->value, $matches)) {
         $result = array();
         $parser_nominal = new PowerSpecParser(trim($matches[1]));
         $parser_nominal->parse();
         $parser_max = new PowerSpecParser(trim($matches[5]));
         $parser_max->parse();
         if ($parser_nominal->getParsed()) {
             $result[] = array('value' => $parser_nominal->getParsed()[0]['value'], 'measure' => $parser_nominal->getParsed()[0]['measure'], 'key' => 'Мощность номинальная');
         }
         if ($parser_max->getParsed()) {
             $result[] = array('value' => $parser_max->getParsed()[0]['value'], 'measure' => $parser_max->getParsed()[0]['measure'], 'key' => 'Мощность максимальная');
         }
         if (!empty($result)) {
             $this->parsed = $result;
             $this->parsed = $this->validateParsed($this->parsed);
             $this->matched_regex = $pattern;
             $this->parse_called = true;
             if ($this->options['debug']) {
                 echo "Parse result is " . var_export($this->parsed, true) . " \n";
             }
             return $this->parsed;
         } else {
             if ($this->options['debug']) {
                 echo "Parse result is empty \n";
             }
             $this->parsed = false;
             $this->parse_called = true;
             $this->matched_regex = '';
             return false;
         }
     }
     // If the parsed value should not be processed by code above let it to be processed by standard method with patterns declared in $this->patterns.
     return parent::parse();
 }