-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
19 changed files
with
650 additions
and
5 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
composer.phar | ||
/vendor/ | ||
|
||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock | ||
composer.lock | ||
/build/logs | ||
/bin |
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 @@ | ||
<?php | ||
|
||
$finder = Symfony\CS\Finder\DefaultFinder::create() | ||
->in('src') | ||
; | ||
|
||
$header = <<<EOT | ||
This file is part of the tmilos-value package. | ||
(c) Milos Tomic <[email protected]> | ||
This source file is subject to the MIT license that is bundled | ||
with this source code in the file LICENSE. | ||
EOT; | ||
|
||
Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header); | ||
|
||
return Symfony\CS\Config\Config::create() | ||
->setUsingCache(false) | ||
->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL) | ||
->fixers(array('-empty_return', '-phpdoc_no_empty_return', 'header_comment')) | ||
->finder($finder) | ||
; |
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,2 @@ | ||
tools: | ||
external_code_coverage: false |
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,36 @@ | ||
language: php | ||
|
||
sudo: false | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- hhvm | ||
|
||
matrix: | ||
fast_finish: true | ||
allow_failures: | ||
- php: hhvm | ||
include: | ||
- php: 5.5 | ||
env: COMPOSER_FLAGS="--prefer-lowest" | ||
|
||
before_install: | ||
- composer self-update | ||
- composer --version | ||
- wget http://get.sensiolabs.org/php-cs-fixer.phar -O php-cs-fixer.phar | ||
|
||
install: | ||
- composer update $COMPOSER_FLAGS | ||
|
||
script: | ||
- php php-cs-fixer.phar fix --dry-run -v | ||
- bin/phpunit --coverage-clover build/logs/clover.xml | ||
|
||
after_script: | ||
- php bin/coveralls -v |
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 |
---|---|---|
@@ -1,2 +1,52 @@ | ||
# value | ||
# Value object PHP lib | ||
|
||
Abstract value and enum objects | ||
|
||
## Value objects | ||
|
||
```php | ||
class IntValue extends AbstractValue | ||
{ | ||
public static function isValid($value) | ||
{ | ||
return is_int($value); | ||
} | ||
} | ||
|
||
$x = new IntValue(10); // ok | ||
print $x->getValue(); // 10 | ||
$y = new IntValue(10); | ||
var_dump($x->equal($y)); // true | ||
|
||
$z = new IntValue('20'); // throws \UnexpectedValueException | ||
``` | ||
|
||
## Enum value object | ||
|
||
All class defined constants are valid values, and magic method with same constant name can be called to instantiate Enum with that value. | ||
|
||
```php | ||
class Gender extends AbstractEnum | ||
{ | ||
const MALE = 'male'; | ||
const FEMALE = 'female'; | ||
|
||
private static $titles = [ | ||
self::MALE => 'gender.male', | ||
self::FEMALE => 'gender.female', | ||
]; | ||
|
||
public function getTitle() | ||
{ | ||
return self::$titles[$this->getValue()]; | ||
} | ||
} | ||
|
||
var_dump(Gender::all()); // ['male' => Gender() => 'female' => Gender() ] | ||
var_dump(Gender::values()); // [ 0 => 'male', 1 => 'female' ] | ||
$m = Gender::MALE(); | ||
print $m->getValue(); // male | ||
print $m->getTitle(); // female | ||
var_dump(Gender::isValid('male')); // true | ||
var_dump(Gender::isValid('something')); // false | ||
``` |
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,8 @@ | ||
<?php | ||
|
||
// if library is in dev environement with its own vendor, include its autoload | ||
if(file_exists(__DIR__ . '/vendor')) | ||
require_once __DIR__ . '/vendor/autoload.php'; | ||
// if library is in vendor of another project, include the global autolaod | ||
else | ||
require_once __DIR__ . '/../../autoload.php'; |
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,33 @@ | ||
{ | ||
"name": "tmilos/value", | ||
"license": "MIT", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Milos Tomic", | ||
"email": "[email protected]", | ||
"homepage": "https://github.com/tmilos/", | ||
"role": "Developer" | ||
} | ||
], | ||
"autoload": { | ||
"psr-0": { | ||
"Tmilos\\Value\\Tests\\": "tests/", | ||
"Tmilos\\Value\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=5.5.1" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~4.8", | ||
"satooshi/php-coveralls": "~0.6", | ||
"ramsey/uuid": "^3.3", | ||
"moontoast/math": "~1.1" | ||
}, | ||
"config": { | ||
"bin-dir": "bin" | ||
}, | ||
"prefer-stable": true, | ||
"minimum-stability": "stable" | ||
} |
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,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html --> | ||
<phpunit | ||
backupGlobals = "false" | ||
backupStaticAttributes = "false" | ||
colors = "false" | ||
convertErrorsToExceptions = "true" | ||
convertNoticesToExceptions = "true" | ||
convertWarningsToExceptions = "true" | ||
processIsolation = "false" | ||
stopOnFailure = "false" | ||
syntaxCheck = "false" | ||
bootstrap = "autoload.php" | ||
> | ||
|
||
<logging> | ||
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/> | ||
</logging> | ||
|
||
<testsuites> | ||
<testsuite name="Project Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<!-- | ||
<php> | ||
<server name="KERNEL_DIR" value="/path/to/your/app/" /> | ||
</php> | ||
--> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
|
||
</phpunit> |
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,115 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the tmilos-value package. | ||
* | ||
* (c) Milos Tomic <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Tmilos\Value; | ||
|
||
abstract class AbstractEnum extends AbstractValue implements Enum | ||
{ | ||
/** @var array */ | ||
private static $constantsCache = []; | ||
|
||
/** @var array */ | ||
private static $reflectionClassCache = []; | ||
|
||
/** | ||
* Returns array of all Enum instances index by their values. | ||
* | ||
* @return Enum[] Constant name => Enum | ||
*/ | ||
public static function all() | ||
{ | ||
$result = []; | ||
foreach (self::getConstants() as $k => $v) { | ||
/** @var Value $object */ | ||
$object = new static($v); | ||
$result[$object->getValue()] = $object; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Returns array of constant values. | ||
* | ||
* @return array | ||
*/ | ||
public static function values() | ||
{ | ||
return array_values(self::getConstants()); | ||
} | ||
|
||
/** | ||
* Check if is valid enum value. | ||
* | ||
* @param $value | ||
* | ||
* @return bool | ||
*/ | ||
public static function isValid($value) | ||
{ | ||
return in_array($value, self::getConstants(), true); | ||
} | ||
|
||
/** | ||
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant. | ||
* | ||
* @param string $name | ||
* @param array $arguments | ||
* | ||
* @return static | ||
* | ||
* @throws \BadMethodCallException | ||
*/ | ||
public static function __callStatic($name, array $arguments = []) | ||
{ | ||
$array = self::getConstants(); | ||
if (array_key_exists($name, $array)) { | ||
$reflectionClass = self::getReflectionClass(); | ||
array_unshift($arguments, $array[$name]); | ||
|
||
return $reflectionClass->newInstanceArgs($arguments); | ||
} | ||
|
||
throw new \BadMethodCallException(sprintf('No static method or enum constant "%s" in class "%s"', $name, get_called_class())); | ||
} | ||
|
||
/** | ||
* @return \ReflectionClass | ||
*/ | ||
private static function getReflectionClass() | ||
{ | ||
$class = get_called_class(); | ||
if (!array_key_exists($class, self::$reflectionClassCache)) { | ||
self::inspect($class); | ||
} | ||
|
||
return self::$reflectionClassCache[$class]; | ||
} | ||
|
||
private static function getConstants() | ||
{ | ||
$class = get_called_class(); | ||
if (!array_key_exists($class, self::$constantsCache)) { | ||
self::inspect($class); | ||
} | ||
|
||
return self::$constantsCache[$class]; | ||
} | ||
|
||
private static function inspect($class) | ||
{ | ||
if (!array_key_exists($class, self::$reflectionClassCache)) { | ||
$reflection = new \ReflectionClass($class); | ||
self::$reflectionClassCache[$class] = $reflection; | ||
self::$constantsCache[$class] = $reflection->getConstants(); | ||
} | ||
} | ||
} |
Oops, something went wrong.