diff --git a/src/Header/CacheControl.php b/src/Header/CacheControl.php index f01cf0c..4bde251 100644 --- a/src/Header/CacheControl.php +++ b/src/Header/CacheControl.php @@ -39,7 +39,7 @@ abstract class CacheControl * @param string $string * @return static */ - public static function fromString($string) + protected static function createFromString($string) { $cacheControl = new static(); diff --git a/src/Header/RequestCacheControl.php b/src/Header/RequestCacheControl.php index e333cd8..968a111 100644 --- a/src/Header/RequestCacheControl.php +++ b/src/Header/RequestCacheControl.php @@ -20,6 +20,17 @@ class RequestCacheControl extends CacheControl 'only-if-cached' => 'withOnlyIfCached', ]; + /** + * Create a new Request Cache-Control object from a header string. + * + * @param string $string + * @return static + */ + public static function fromString($string) + { + return static::createFromString($string); + } + /** * Set how many seconds a stale representation is acceptable. * diff --git a/src/Header/ResponseCacheControl.php b/src/Header/ResponseCacheControl.php index 98ed680..0a34d46 100644 --- a/src/Header/ResponseCacheControl.php +++ b/src/Header/ResponseCacheControl.php @@ -24,6 +24,17 @@ class ResponseCacheControl extends CacheControl 'proxy-revalidate' => 'withProxyRevalidate', ]; + /** + * Create a new Response Cache-Control object from a header string. + * + * @param string $string + * @return static + */ + public static function fromString($string) + { + return static::createFromString($string); + } + /** * Set whether a response should be cached by shared caches. The method will automatically * remove the private flag if it is set. diff --git a/test/Header/CacheControlStub.php b/test/Header/CacheControlStub.php index 34480a5..305e03a 100644 --- a/test/Header/CacheControlStub.php +++ b/test/Header/CacheControlStub.php @@ -15,6 +15,11 @@ class CacheControlStub extends CacheControl 'custom' => 'withCustom' ]; + public static function createFromString($string) + { + return parent::createFromString($string); + } + public function withCustom($value) { return $value; diff --git a/test/Header/CacheControlTest.php b/test/Header/CacheControlTest.php index 1c3f5a1..645fab3 100644 --- a/test/Header/CacheControlTest.php +++ b/test/Header/CacheControlTest.php @@ -114,61 +114,61 @@ public function testGetDirectiveWithoutValue() } /** - * @covers Micheh\Cache\Header\CacheControl::fromString + * @covers Micheh\Cache\Header\CacheControl::createFromString * @covers Micheh\Cache\Header\CacheControl::getMethod */ public function testFromStringWithFlag() { - $control = CacheControlStub::fromString('no-transform'); + $control = CacheControlStub::createFromString('no-transform'); $this->assertAttributeSame(['no-transform' => true], 'directives', $control); } /** - * @covers Micheh\Cache\Header\CacheControl::fromString + * @covers Micheh\Cache\Header\CacheControl::createFromString * @covers Micheh\Cache\Header\CacheControl::getMethod */ public function testFromStringWithToken() { - $control = CacheControlStub::fromString('max-age=60'); + $control = CacheControlStub::createFromString('max-age=60'); $this->assertAttributeSame(['max-age' => 60], 'directives', $control); } /** - * @covers Micheh\Cache\Header\CacheControl::fromString + * @covers Micheh\Cache\Header\CacheControl::createFromString * @covers Micheh\Cache\Header\CacheControl::getMethod */ public function testFromStringWithMultiple() { - $control = CacheControlStub::fromString('no-transform, max-age=100'); + $control = CacheControlStub::createFromString('no-transform, max-age=100'); $this->assertAttributeSame(['no-transform' => true, 'max-age' => 100], 'directives', $control); } /** - * @covers Micheh\Cache\Header\CacheControl::fromString + * @covers Micheh\Cache\Header\CacheControl::createFromString * @covers Micheh\Cache\Header\CacheControl::getMethod */ public function testFromStringWithOverrideMethod() { - $this->assertSame('123', CacheControlStub::fromString('custom=123')); + $this->assertSame('123', CacheControlStub::createFromString('custom=123')); } /** - * @covers Micheh\Cache\Header\CacheControl::fromString + * @covers Micheh\Cache\Header\CacheControl::createFromString * @covers Micheh\Cache\Header\CacheControl::getMethod */ public function testFromStringWithUnknownDirective() { - $control = CacheControlStub::fromString('foo="bar"'); + $control = CacheControlStub::createFromString('foo="bar"'); $this->assertAttributeSame(['foo' => 'bar'], 'directives', $control); } /** - * @covers Micheh\Cache\Header\CacheControl::fromString + * @covers Micheh\Cache\Header\CacheControl::createFromString * @covers Micheh\Cache\Header\CacheControl::getMethod */ public function testFromStringWithUnknownDirectiveFlag() { - $control = CacheControlStub::fromString('foo'); + $control = CacheControlStub::createFromString('foo'); $this->assertAttributeSame([], 'directives', $control); } diff --git a/test/Header/RequestCacheControlTest.php b/test/Header/RequestCacheControlTest.php index cf71bd0..a0b4dc1 100644 --- a/test/Header/RequestCacheControlTest.php +++ b/test/Header/RequestCacheControlTest.php @@ -2,6 +2,8 @@ namespace MichehTest\Cache\Header; +use Micheh\Cache\Header\RequestCacheControl; + /** * @author Michel Hunziker * @copyright Copyright (c) 2015, Michel Hunziker @@ -14,6 +16,15 @@ class RequestCacheControlTest extends CacheControlTestCase */ protected $controlClass = 'Micheh\Cache\Header\RequestCacheControl'; + /** + * @covers Micheh\Cache\Header\RequestCacheControl::fromString + */ + public function testFromString() + { + $control = RequestCacheControl::fromString('max-age=100'); + $this->assertInstanceOf($this->controlClass, $control); + } + /** * @covers Micheh\Cache\Header\RequestCacheControl::withMaxStale */ diff --git a/test/Header/ResponseCacheControlTest.php b/test/Header/ResponseCacheControlTest.php index e525cae..206b367 100644 --- a/test/Header/ResponseCacheControlTest.php +++ b/test/Header/ResponseCacheControlTest.php @@ -21,6 +21,15 @@ class ResponseCacheControlTest extends CacheControlTestCase */ protected $controlClass = 'Micheh\Cache\Header\ResponseCacheControl'; + /** + * @covers Micheh\Cache\Header\ResponseCacheControl::fromString + */ + public function testFromString() + { + $control = ResponseCacheControl::fromString('max-age=100'); + $this->assertInstanceOf($this->controlClass, $control); + } + /** * @covers Micheh\Cache\Header\ResponseCacheControl::withPublic * @covers Micheh\Cache\Header\ResponseCacheControl::withPublicPrivate