errorcode418/php
0
1<?php2/**3 * ua-parser4 *5 * Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com6 *7 * Released under the MIT license8 */9namespace UAParser\Util\Logfile;10 11use UAParser\Exception\ReaderException;12 13abstract class AbstractReader implements ReaderInterface14{15 /** @var ReaderInterface[] */16 private static $readers = array();17 18 /**19 * @param string $line20 * @return ReaderInterface21 */22 public static function factory($line)23 {24 foreach (static::getReaders() as $reader) {25 if ($reader->test($line)) {26 return $reader;27 }28 }29 }30 31 private static function getReaders()32 {33 if (static::$readers) {34 return static::$readers;35 }36 37 static::$readers[] = new ApacheCommonLogFormatReader();38 39 return static::$readers;40 }41 42 public function test($line)43 {44 $matches = $this->match($line);45 46 return isset($matches['userAgentString']);47 }48 49 public function read($line)50 {51 $matches = $this->match($line);52 53 if (!isset($matches['userAgentString'])) {54 throw ReaderException::userAgentParserError($line);55 }56 57 return $matches['userAgentString'];58 }59 60 protected function match($line)61 {62 if (preg_match($this->getRegex(), $line, $matches)) {63 return $matches;64 }65 66 return array();67 }68 69 abstract protected function getRegex();70}71 