Page MenuHomeDevCentral

D637.id1592.diff
No OneTemporary

D637.id1592.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
Tue, Nov 26, 17:44 (16 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2264904
Default Alt Text
D637.id1592.diff (1 KB)

Event Timeline