Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Nov 17, 2015
2 parents 7ac29f5 + 6152538 commit b965b36
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 957 deletions.
37 changes: 37 additions & 0 deletions .travis.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env php
<?php
// Update composer to the latest version ...
passthru('composer self-update --no-interaction');

// Build a composer config that uses the GitHub OAuth token if it is available ...
$config = array(
'config' => array(
'notify-on-install' => false
)
);

if ($token = getenv('ARCHER_TOKEN')) {
$config['config']['github-oauth'] = array(
'github.com' => $token
);
$composerFlags = '--prefer-dist';
} else {
$composerFlags = '--prefer-source';
}

$file = '~/.composer/config.json';
$dir = dirname($file);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
file_put_contents($file, json_encode($config));

// Display some information about GitHub rate limiting ...
if ($token) {
passthru('curl -s -i -H "Authorization: token $ARCHER_TOKEN" https://api.github.com | grep "^X-RateLimit"');
}

// Install composer dependencies ...
$exitCode = 0;
passthru('composer install --dev --no-progress --no-interaction --ansi ' . $composerFlags, $exitCode);
exit($exitCode);
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php

php: ["5.5", "5.6", "7.0", "hhvm", "hhvm-nightly"]

matrix:
allow_failures: [{"php": "hhvm"}, {"php": "hhvm-nightly"}]
fast_finish: true

env:
global:
- ARCHER_PUBLISH_VERSION=5.6


install:
- ./.travis.install

script:
- ./vendor/bin/archer travis:build
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Flip Changelog

### 0.1.0 (2015-11-17)

* Initial Release
107 changes: 70 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flip

[![The most recent stable version is 0.0.0][version-image]][semantic versioning]
[![The most recent stable version is 0.1.0][version-image]][semantic versioning]
[![Current build status image][build-image]][current build status]
[![Current coverage status image][coverage-image]][current coverage status]

Expand All @@ -9,10 +9,9 @@
[coverage-image]: https://img.shields.io/codecov/c/github/IcecaveStudios/flip/develop.svg?style=flat-square "Current test coverage for the develop branch"
[current coverage status]: https://coveralls.io/r/IcecaveStudios/flip
[semantic versioning]: http://semver.org/
[version-image]: http://img.shields.io/:semver-0.0.0-red.svg?style=flat-square "This project uses semantic versioning"
[version-image]: http://img.shields.io/:semver-0.1.0-yellow.svg?style=flat-square "This project uses semantic versioning"

**Flip** is a simple PHP library for accepting sets of boolean options as input
to functions.
**Flip** is a tiny PHP library for working with strict sets of boolean values.

- Install via [Composer] package [icecave/flip]
- Read the [API documentation]
Expand All @@ -21,62 +20,96 @@ to functions.
[composer]: http://getcomposer.org/
[icecave/flip]: https://packagist.org/packages/icecave/flip

## Example
## Defining an option-set

First the valid options are defined by declaring a class that extends from
`AbstractOptions`.
An option-set describes the available options of a given type. Option-sets are
defined by declaring a class that uses the `OptionSetTrait` trait.

Each property in the class defines a named option that can be set to `true` or
`false`. All properties must be private and have a default boolean value.

```php
use Icecave\Flip\AbstractOptions;
use Icecave\Flip\OptionSetTrait;

final class MyOptions extends AbstractOptions
final class ExampleOptions
{
const FOO = 'foo';
const BAR = 'bar';
use OptionSetTrait;

private $foo = true;
private $bar = false;
private $baz = false;
}
```

Functions that accept the options use the `build()` method to create an
`OptionCollection` that holds the state of the passed options.
## Creating an option-set

The option-set trait provides the following static methods for quickly creating
common sets:

* `defaults()` - creates an option-set where all options are set to the default values
* `all()` - creates an option-set where all options are set to `true`
* `none()` - creates an option-set where all options are set to `false`

Option-sets can also be created and modified using a fluent interface. The
example below creates an option-set with only the `bar` and `baz` properties set
to `true`.

```php
function printOptions(array $options)
{
$options = MyOptions::build($options);
$options = ExampleOptions::none()
->bar(true)
->baz(true);
```

Omitting the initial call to `defaults()`, `all()` or `none()` is short-hand
for using the defaults. This means that the following two examples are equivalent:

```php
$options = ExampleOptions::defaults()
->foo(false)
->bar(true);
```

```php
$options = ExampleOptions
::foo(false)
->bar(true);
```

Option-sets are immutable, each call to the fluent interface returns a new
instance with the updated option value.

if ($options[MyOptions::FOO()]) {
Options can not be named "defaults", "all" or "none".

## Using an option-set

**Functions that accept option-sets as parameters can use a type-hint.** Options are
read using the regular PHP property notation. Option values are guaranteed to
be a boolean.

```php
function dumpOptions(ExampleOptions $options)
{
if ($options->foo) {
echo 'Foo is enabled!';
} else {
echo 'Foo is disabled!';
}

if ($options[MyOptions::BAR()]) {
if ($options->bar) {
echo 'Bar is enabled!';
} else {
echo 'Bar is disabled!';
}
}
```

When calling the function, options are passed as an array mapping the option
value to a boolean.

```php
$options = [
MyOptions::FOO => true,
];

printOptions($options);
```

Which will output:
```console
Foo is enabled!
Bar is disabled!
if ($options->baz) {
echo 'Baz is enabled!';
} else {
echo 'Baz is disabled!';
}
}
```

An `InvalidArgumentException` is thrown if the options array passed to `build()`
contains keys that are not defined in the `MyOptions` class.
It is not possible to set options using the property notation.

## Contact us

Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "icecave/flip",
"description": "Simple, strict, explicit boolean options.",
"description": "A tiny library for working with strict sets of boolean values.",
"keywords": [
"flag",
"option",
"bit",
"boolean"
"boolean",
"set",
"enum",
"field"
],
"homepage": "https://github.com/IcecaveStudios/flip",
"license": "MIT",
Expand All @@ -17,13 +20,10 @@
}
],
"require": {
"php": ">=5.6",
"icecave/isolator": "~2",
"eloquent/enumeration": "^5"
"php": ">=5.5"
},
"require-dev": {
"icecave/archer": "~1",
"eloquent/phony": "^0.3"
"icecave/archer": "~1"
},
"autoload": {
"psr-4": {
Expand All @@ -32,7 +32,7 @@
},
"extra": {
"branch-alias": {
"dev-develop": "0.1.x-dev"
"dev-develop": "0.2.x-dev"
}
}
}
Loading

0 comments on commit b965b36

Please sign in to comment.