Page MenuHomeDevCentral

D637.diff
No OneTemporary

D637.diff

diff --git a/src/DockerImage.php b/src/DockerImage.php
--- a/src/DockerImage.php
+++ b/src/DockerImage.php
@@ -2,6 +2,8 @@
namespace Keruald\DockerHub;
+use InvalidArgumentException;
+
abstract class DockerImage {
///
@@ -24,7 +26,7 @@
public $image;
///
- /// Constructor
+ /// Constructors
///
/**
@@ -38,6 +40,24 @@
$this->image = $image;
}
+ /**
+ * Initializes a new instance of a DockerImage object
+ * from the slash notation.
+ *
+ * @param string $image the full user image name (e.g. "acme/foo")
+ * @return DockerImage
+ *
+ * @throws InvalidArgumentException when image name doesn't contain a slash.
+ */
+ public static function loadFromSlashNotation ($image) {
+ if (strpos($image, '/') === false) {
+ throw new InvalidArgumentException("Image name doesn't contain a slash (/).");
+ }
+
+ $imageFragments = explode('/', $image, 2);
+ return new static(...$imageFragments);
+ }
+
///
/// Helper methods
///
diff --git a/tests/DockerHubImageTest.php b/tests/DockerHubImageTest.php
--- a/tests/DockerHubImageTest.php
+++ b/tests/DockerHubImageTest.php
@@ -16,6 +16,18 @@
$this->image = new DockerHubImage("acme", "foo");
}
+ public function testLoadFromSlashNotation () {
+ $slashNotationImage = DockerHubImage::loadFromSlashNotation("acme/foo");
+ $this->assertEquals($slashNotationImage, $this->image);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testLoadFromSlashNotationWithInvalidArgument () {
+ DockerHubImage::loadFromSlashNotation("foo");
+ }
+
public function testGetRegistryUrl () {
$this->assertSame(
"https://registry.hub.docker.com/u/acme/foo",

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 16:09 (18 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258376
Default Alt Text
D637.diff (1 KB)

Event Timeline