-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from xp-forge/feature/s3-key
Add S3Key implementation to construct S3 paths
- Loading branch information
Showing
8 changed files
with
184 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php namespace com\amazon\aws; | ||
|
||
use lang\Value; | ||
|
||
/** | ||
* S3 Keys help construct paths from components | ||
* | ||
* @test com.amazon.aws.unittest.S3KeyTest | ||
*/ | ||
class S3Key implements Value { | ||
private $path; | ||
|
||
/** Creates a new S3 key from given components */ | ||
public function __construct(string... $components) { | ||
$this->path= ltrim(implode('/', $components), '/'); | ||
} | ||
|
||
/** Returns the path */ | ||
public function path(string $base= ''): string { | ||
return rtrim($base, '/').'/'.$this->path; | ||
} | ||
|
||
/** @return string */ | ||
public function __toString() { return '/'.$this->path; } | ||
|
||
/** @return string */ | ||
public function hashCode() { return 'S3'.md5($this->path); } | ||
|
||
/** @return string */ | ||
public function toString() { return nameof($this).'(/'.$this->path.')'; } | ||
|
||
/** | ||
* Comparison | ||
* | ||
* @param var $value | ||
* @return int | ||
*/ | ||
public function compareTo($value) { | ||
return $value instanceof self ? $this->path <=> $value->path : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php namespace com\amazon\aws\unittest; | ||
|
||
use com\amazon\aws\S3Key; | ||
use test\{Assert, Test, Values}; | ||
|
||
class S3KeyTest { | ||
|
||
#[Test] | ||
public function empty() { | ||
Assert::equals('/', (new S3Key())->path()); | ||
} | ||
|
||
#[Test] | ||
public function single() { | ||
Assert::equals('/test', (new S3Key('test'))->path()); | ||
} | ||
|
||
#[Test] | ||
public function composed() { | ||
Assert::equals('/target/test', (new S3Key('target', 'test'))->path()); | ||
} | ||
|
||
#[Test, Values(['/base', '/base/'])] | ||
public function based($base) { | ||
Assert::equals('/base/test', (new S3Key('test'))->path($base)); | ||
} | ||
|
||
#[Test] | ||
public function string_cast() { | ||
Assert::equals('/test', (string)new S3Key('test')); | ||
} | ||
|
||
#[Test] | ||
public function string_representation() { | ||
Assert::equals('com.amazon.aws.S3Key(/target/test)', (new S3Key('target', 'test'))->toString()); | ||
} | ||
|
||
#[Test] | ||
public function comparison() { | ||
$fixture= new S3Key('b-test'); | ||
|
||
Assert::equals(0, $fixture->compareTo(new S3Key('b-test'))); | ||
Assert::equals(1, $fixture->compareTo(new S3Key('a-test'))); | ||
Assert::equals(-1, $fixture->compareTo(new S3Key('c-test'))); | ||
} | ||
} |
Oops, something went wrong.