This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4c8b1a
commit 57f93f7
Showing
6 changed files
with
118 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?hh // strict | ||
|
||
namespace Zynga\Framework\Type\V1\FileType; | ||
|
||
use Zynga\Framework\Type\V1\StringBox; | ||
use Zynga\Framework\Type\V1\FileType\BoxFactory as FileTypeBoxFactory; | ||
use Zynga\Framework\Type\V1\FileType\Exception\UnknownFileTypeException; | ||
|
||
/** | ||
* Representation of a file type | ||
*/ | ||
class Box extends StringBox { | ||
|
||
<<__Override>> | ||
protected function importFromString(string $value): bool { | ||
if (FileTypeBoxFactory::isValidValue($value)) { | ||
return parent::importFromString($value); | ||
} | ||
|
||
throw new UnknownFileTypeException('value='.$value); | ||
} | ||
|
||
} |
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,29 @@ | ||
<?hh // strict | ||
|
||
namespace Zynga\Framework\Type\V1\FileType; | ||
|
||
use Zynga\Framework\Type\V1\FileType\Box as FileTypeBox; | ||
use Zynga\Framework\Type\V1\FileType\Enum\FileType as FileTypeEnum; | ||
|
||
/** | ||
* Generates and validates file types | ||
*/ | ||
class BoxFactory { | ||
|
||
public static function json(): FileTypeBox { | ||
$value = new FileTypeBox(); | ||
$value->set(FileTypeEnum::JSON); | ||
return $value; | ||
} | ||
|
||
public static function csv(): FileTypeBox { | ||
$value = new FileTypeBox(); | ||
$value->set(FileTypeEnum::CSV); | ||
return $value; | ||
} | ||
|
||
public static function isValidValue(string $value): bool { | ||
return FileTypeEnum::coerce($value) !== null; | ||
} | ||
|
||
} |
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,28 @@ | ||
<?hh // strict | ||
|
||
namespace Zynga\Framework\Type\V1\FileType; | ||
|
||
use Zynga\Framework\Testing\TestCase\V2\Base as ZyngaTestCase; | ||
use Zynga\Framework\Type\V1\FileType\BoxFactory; | ||
use Zynga\Framework\Type\V1\FileType\Enum\FileType as FileTypeEnum; | ||
|
||
class BoxFactoryTest extends ZyngaTestCase { | ||
|
||
public function testJsonReturnsValidType(): void { | ||
$this->assertEquals((string)FileTypeEnum::JSON, BoxFactory::json()->get()); | ||
} | ||
|
||
public function testCsvReturnsValidType(): void { | ||
$this->assertEquals((string)FileTypeEnum::CSV, BoxFactory::csv()->get()); | ||
} | ||
|
||
public function testIsValidValueReturnsFalseCorrectly(): void { | ||
$this->assertFalse(BoxFactory::isValidValue('pumpernickel')); | ||
} | ||
|
||
public function testIsValidValueReturnsTrueCorrectly(): void { | ||
$this->assertTrue(BoxFactory::isValidValue((string)FileTypeEnum::JSON)); | ||
$this->assertTrue(BoxFactory::isValidValue((string)FileTypeEnum::CSV)); | ||
} | ||
|
||
} |
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,18 @@ | ||
<?hh // strict | ||
|
||
namespace Zynga\Framework\Type\V1\FileType; | ||
|
||
use Zynga\Framework\Testing\TestCase\V2\Base as ZyngaTestCase; | ||
use Zynga\Framework\Type\V1\FileType\BoxFactory; | ||
use Zynga\Framework\Type\V1\FileType\Box as FileTypeBox; | ||
use Zynga\Framework\Type\V1\FileType\Exception\UnknownFileTypeException; | ||
|
||
class BoxTest extends ZyngaTestCase { | ||
|
||
public function testInvalidSetThrowsUnknownFileTypeException(): void { | ||
$fileType = new FileTypeBox(); | ||
$this->expectException(UnknownFileTypeException::class); | ||
$fileType->set('pumpernickel'); | ||
} | ||
|
||
} |
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,10 @@ | ||
<?hh // strict | ||
|
||
namespace Zynga\Framework\Type\V1\FileType\Enum; | ||
|
||
enum FileType : string as string { | ||
// @codeCoverageIgnoreStart | ||
JSON = 'json'; | ||
CSV = 'csv'; | ||
// @codeCoverageIgnoreEnd | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Zynga/Framework/Type/V1/FileType/Exception/UnknownFileTypeException.hh
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,10 @@ | ||
<?hh // strict | ||
|
||
namespace Zynga\Framework\Type\V1\FileType\Exception; | ||
|
||
use Zynga\Framework\Exception\V1\Exception as BaseException; | ||
|
||
/** | ||
* Representation that an unknown file type was attempted to be used | ||
*/ | ||
class UnknownFileTypeException extends BaseException {} |