Browscap\Parser\IniParser::parse PHP Method

parse() public method

public parse ( ) : array
return array
    public function parse()
    {
        $fileLines = $this->getFileLines();
        $data = [];
        $currentSection = '';
        $currentDivision = '';
        for ($line = 0, $count = count($fileLines); $line < $count; ++$line) {
            $currentLine = $fileLines[$line];
            $currentLineLength = strlen($currentLine);
            if ($currentLineLength === 0) {
                continue;
            }
            if (substr($currentLine, 0, 40) === ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;') {
                $currentDivision = trim(substr($currentLine, 41));
                continue;
            }
            // We only skip comments that *start* with semicolon
            if ($currentLine[0] === ';') {
                continue;
            }
            if ($currentLine[0] === '[') {
                $currentSection = substr($currentLine, 1, $currentLineLength - 2);
                continue;
            }
            $bits = explode('=', $currentLine);
            if (count($bits) > 2) {
                throw new \RuntimeException("Too many equals in line: {$currentLine}, in Division: {$currentDivision}");
            }
            if (count($bits) < 2) {
                $bits[1] = '';
            }
            $data[$currentSection][$bits[0]] = $bits[1];
            $data[$currentSection]['Division'] = $currentDivision;
        }
        if ($this->shouldSort()) {
            $data = $this->sortArrayAndChildArrays($data);
        }
        $this->data = $data;
        return $data;
    }

Usage Example

Example #1
0
 /**
  * tests throwing an exception if more than one eual sign is present in a line
  *
  * @group parser
  * @group sourcetest
  */
 public function testParseThrowsExceptionWhenInvalidFormatting()
 {
     $lines = ['double=equals=here'];
     $parser = new IniParser('');
     $parser->setFileLines($lines);
     $this->setExpectedException('\\RuntimeException', 'Too many equals in line: double=equals=here');
     $parser->parse();
 }