Skip to content

Commit

Permalink
Update library for PHP 7.2 and implement strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Dec 31, 2018
1 parent 318c61e commit fa6b349
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 134 deletions.
4 changes: 3 additions & 1 deletion src/Exception/HttpRangeException.php
Expand Up @@ -9,10 +9,12 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range\Exception;

/**
* A general exception for ramsey/http-range
* A general exception for ramsey/http-range.
*/
class HttpRangeException extends \RuntimeException
{
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/InvalidRangeSetException.php
Expand Up @@ -9,10 +9,12 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range\Exception;

/**
* Indicates a problem with the range-set property
* Thrown to indicate a problem with the range-set property.
*/
class InvalidRangeSetException extends HttpRangeException
{
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/InvalidRangeUnitException.php
Expand Up @@ -9,10 +9,12 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range\Exception;

/**
* Indicates a problem with the range-unit property
* Thrown to indicate a problem with the range-unit property.
*/
class InvalidRangeUnitException extends HttpRangeException
{
Expand Down
6 changes: 4 additions & 2 deletions src/Exception/NoRangeException.php
Expand Up @@ -9,15 +9,17 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range\Exception;

/**
* Indicates a range request is not present in the current request
* Thrown to indicate a range request is not present in the current request.
*/
class NoRangeException extends HttpRangeException
{
/**
* @var string
*/
protected $message = 'The Range header is not present on this request or has no value';
protected $message = 'The Range header is not present on this request or has no value.';
}
36 changes: 20 additions & 16 deletions src/Exception/NotSatisfiableException.php
Expand Up @@ -9,58 +9,62 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range\Exception;

use Exception;

/**
* Indicates the range given cannot be satisfied
* Thrown to indicate the range given cannot be satisfied.
*/
class NotSatisfiableException extends HttpRangeException
{
/**
* The range string that couldn't be satisfied
*
* @var string
*/
private $range;

/**
* The total size of the entity being requested
*
* @var mixed
*/
private $totalSize;

/**
* Constructs a NotSatisfiableException
* Constructs a NotSatisfiableException.
*
* @param string $message
* @param string $range
* @param mixed $totalSize
* @param int $code
* @param Exception $previous
* @param string $message The exception message.
* @param string $range The range value parsed from the request.
* @param mixed $totalSize The total size of the entity for which the range
* is requested.
* @param int $code A custom error code, if applicable.
* @param Exception $previous A previous exception, if applicable.
*/
public function __construct($message, $range, $totalSize, $code = 0, Exception $previous = null)
{
public function __construct(
string $message,
string $range,
$totalSize,
int $code = 0,
Exception $previous = null
) {
$this->range = $range;
$this->totalSize = $totalSize;

parent::__construct($message, $code, $previous);
}

/**
* Returns the range that couldn't be satisfied
* Returns the range that couldn't be satisfied.
*
* @return string
*/
public function getRange()
public function getRange(): string
{
return $this->range;
}

/**
* Returns the total size of the entity being requested
* Returns the total size of the entity being requested.
*
* @return mixed
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/ParseException.php
Expand Up @@ -9,10 +9,12 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range\Exception;

/**
* Indicates a problem that occurred when attempting to parse a range
* Thrown to indicate a problem occurred when attempting to parse a range.
*/
class ParseException extends HttpRangeException
{
Expand Down
32 changes: 19 additions & 13 deletions src/Range.php
Expand Up @@ -9,14 +9,16 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range;

use Psr\Http\Message\RequestInterface;
use Ramsey\Http\Range\Exception\NoRangeException;
use Ramsey\Http\Range\Unit\UnitInterface;

/**
* Represents an HTTP Range request header
* `Range` represents an HTTP Range request header.
*
* @link https://tools.ietf.org/html/rfc7233 RFC 7233: HTTP Range Requests
*/
Expand All @@ -38,9 +40,13 @@ class Range
private $unitFactory;

/**
* @param RequestInterface $request
* @param mixed $totalSize The total size of the entity for which a range is requested
* @param UnitFactoryInterface $unitFactory
* Constructs an HTTP Range request header.
*
* @param RequestInterface $request A PSR-7-compatible HTTP request.
* @param mixed $totalSize The total size of the entity for which a range is
* requested (this may be in bytes, items, etc.).
* @param UnitFactoryInterface $unitFactory An optional factory to use for
* parsing range units.
*/
public function __construct(
RequestInterface $request,
Expand All @@ -58,17 +64,17 @@ public function __construct(
}

/**
* Returns the HTTP request object
* Returns the PSR-7 HTTP request object.
*
* @return RequestInterface
*/
public function getRequest()
public function getRequest(): RequestInterface
{
return $this->request;
}

/**
* Returns the total size of the entity for which the range is requested
* Returns the total size of the entity for which the range is requested.
*
* @return mixed
*/
Expand All @@ -78,23 +84,23 @@ public function getTotalSize()
}

/**
* Returns the unit factory used by this range
* Returns the unit factory used by this range.
*
* @return UnitFactoryInterface
*/
public function getUnitFactory()
public function getUnitFactory(): UnitFactoryInterface
{
return $this->unitFactory;
}

/**
* Returns the unit parsed for this range request
*
* @throws NoRangeException if a range request is not present in the current request
* Returns the unit parsed for this range request.
*
* @return UnitInterface
*
* @throws NoRangeException if a range request header could not be found.
*/
public function getUnit()
public function getUnit(): UnitInterface
{
$rangeHeader = $this->getRequest()->getHeader('Range');

Expand Down
41 changes: 23 additions & 18 deletions src/Unit/AbstractUnit.php
Expand Up @@ -9,10 +9,12 @@
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Http\Range\Unit;

/**
* An abstract unit to handle common unit functionality
* `AbstractUnit` provides a basic implementation for HTTP range units.
*/
abstract class AbstractUnit implements UnitInterface
{
Expand All @@ -27,65 +29,68 @@ abstract class AbstractUnit implements UnitInterface
private $totalSize;

/**
* Returns a new collection for this range unit
* Returns a new collection for this range unit.
*
* @return UnitRangesCollection
*/
abstract public function newCollection();
abstract public function newCollection(): UnitRangesCollection;

/**
* Returns a new unit range for this range unit
* Returns a new unit range for this range unit.
*
* @param string $range A single range (i.e. 500-999, 500-, -500).
* @param mixed $totalSize The total size of the entity the range describes.
*
* @param string $range A single range (i.e. 500-999, 500-, -500)
* @param mixed $totalSize The total size of the entity the range describes
* @return UnitRangeInterface
*/
abstract public function newRange($range, $totalSize);
abstract public function newRange(string $range, $totalSize): UnitRangeInterface;

/**
* Constructs a new unit
* Constructs a new unit.
*
* @param string $rangeSet A set of ranges for this unit (i.e. 500-999,500-,-500)
* @param mixed $totalSize The total size of the entity the unit describes
* @param string $rangeSet A set of ranges for this unit (i.e. 500-999,500-,-500).
* @param mixed $totalSize The total size of the entity the unit describes.
*/
public function __construct($rangeSet, $totalSize)
public function __construct(string $rangeSet, $totalSize)
{
$this->rangeSet = $rangeSet;
$this->totalSize = $totalSize;
}

/**
* Returns the raw range set defined for this unit
* Returns the raw range set defined for this unit.
*
* other-range-set = 1*VCHAR
*
* @link https://tools.ietf.org/html/rfc7233#section-3.1 RFC 7233 § 3.1
*
* @return string
*/
public function getRangeSet()
public function getRangeSet(): string
{
return $this->rangeSet;
}

/**
* Returns the raw ranges specifier defined for this unit
* Returns the raw ranges specifier defined for this unit.
*
* other-ranges-specifier = other-range-unit "=" other-range-set
*
* @link https://tools.ietf.org/html/rfc7233#section-3.1 RFC 7233 § 3.1
*
* @return string
*/
public function getRangesSpecifier()
public function getRangesSpecifier(): string
{
return $this->getRangeUnit() . '=' . $this->getRangeSet();
}

/**
* Returns an iterable collection of unit ranges
* Returns an iterable collection of unit ranges.
*
* @return UnitRangesCollection
*/
public function getRanges()
public function getRanges(): UnitRangesCollection
{
$ranges = explode(',', $this->getRangeSet());
$totalSize = $this->getTotalSize();
Expand All @@ -99,7 +104,7 @@ public function getRanges()
}

/**
* Returns the total size of the entity this unit describes
* Returns the total size of the entity this unit describes.
*
* For example, if this unit describes the bytes in a file, then this
* returns the total bytes of the file.
Expand Down

0 comments on commit fa6b349

Please sign in to comment.