1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93:
<?php
namespace Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Parser;
use InvalidArgumentException;
use JonasRudolph\PHPComponents\StringUtility\Implementation\StringUtility;
use Net\Bazzline\Component\ApacheServerStatusParser\DomainModel\Detail;
class DetailLineParser implements LineParserInterface
{
private $stringUtility;
public function __construct(
StringUtility $stringUtility
) {
$this->stringUtility = $stringUtility;
}
public function parse($line)
{
$stringUtility = $this->stringUtility;
$lineAsArray = explode(' ', $line);
$arrayIsInvalid = (count($lineAsArray) < 19);
if ($arrayIsInvalid) {
throw new InvalidArgumentException(
self::class . ' can not parse given line "' . $line . '"'
);
}
$httpMethod = (
$stringUtility->startsWith($lineAsArray[16], '{')
? substr($lineAsArray[16], 1)
: $lineAsArray[16]
);
$pid = filter_var(
$lineAsArray[2],
FILTER_SANITIZE_NUMBER_INT
);
$status = str_replace(
[
'[',
']'
],
'',
$lineAsArray[4]
);
$uriAuthority = str_replace(
[
'[',
']'
],
'',
(isset($lineAsArray[19]) ? $lineAsArray[19] : $lineAsArray[18])
);
$uriPathWithQuery = (
$stringUtility->endsWith($lineAsArray[17], '}')
? substr($lineAsArray[17], 0, -1)
: $lineAsArray[17]
);
return new Detail(
$httpMethod,
$lineAsArray[15],
$pid,
$status,
$uriAuthority,
$uriPathWithQuery
);
}
}