fresh/src/ImageReference.php

61 lines
1.3 KiB
PHP

<?php
namespace NoccyLabs\FreshDocker;
class ImageReference
{
private $image;
private $tag;
private $registry;
private $scheme;
public function __construct(string $image)
{
// if (!preg_match('/^http[s]:\/\//i', $image)) {
$parts = explode("/", $image);
if (count($parts) < 3) {
$image = "https://registry.hub.docker.com/{$image}";
} else {
$image = "https://{$image}";
}
$parts = parse_url($image);
$image = ltrim($parts['path'],'/');
if (str_contains($image,':')) {
[$image, $tag] = explode(":", $image, 2);
} else {
$tag = 'latest';
}
if (!str_contains($image,'/')) {
$image = "library/{$image}";
}
$this->image = $image;
$this->tag = $tag;
$this->registry = $parts['host'];
$this->scheme = $parts['scheme'];
}
public function getRegistry(): string
{
return $this->registry;
}
public function getImage(): string
{
return $this->image;
}
public function getTag(): string
{
return $this->tag;
}
public function __toString()
{
return sprintf("%s/%s:%s", $this->registry, $this->image, $this->tag);
}
}