-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
UnitFactory.php
59 lines (53 loc) · 1.72 KB
/
UnitFactory.php
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
<?php
/**
* This file is part of the ramsey/http-range library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Ramsey\Http\Range;
use Ramsey\Http\Range\Exception\InvalidRangeSetException;
use Ramsey\Http\Range\Exception\InvalidRangeUnitException;
use Ramsey\Http\Range\Unit\BytesUnit;
use Ramsey\Http\Range\Unit\GenericUnit;
use Ramsey\Http\Range\Unit\UnitInterface;
/**
* A default factory for creating bytes range units
*/
class UnitFactory implements UnitFactoryInterface
{
/**
* Returns a parsed unit for the HTTP Range header
*
* @param string $rangesSpecifier The original value of the HTTP Range header
* @param mixed $totalSize The total size of the entity described by this unit
*
* @throws InvalidRangeUnitException
* @throws InvalidRangeSetException
*
* @return UnitInterface
*/
public function getUnit($rangesSpecifier, $totalSize)
{
$unitSet = explode('=', $rangesSpecifier);
if (empty($unitSet[0])) {
throw new InvalidRangeUnitException(
'No range-unit provided in $rangesSpecifier'
);
}
if (empty($unitSet[1])) {
throw new InvalidRangeSetException(
'No range-set provided in $rangesSpecifier'
);
}
switch (strtolower($unitSet[0])) {
case 'bytes':
return new BytesUnit($unitSet[1], $totalSize);
default:
return new GenericUnit($unitSet[0], $unitSet[1], $totalSize);
}
}
}