Merge branch 'v0.1.0' into 'master'

V0.1.0

See merge request byjoby/flatrr!1
This commit is contained in:
Joby Elliott 2018-08-18 15:38:01 +00:00
commit 03113a23aa
12 changed files with 99 additions and 36 deletions

View file

@ -1,6 +1,6 @@
# This file is a template, and might need editing before it works on your project.
# Select image from https://hub.docker.com/_/php/
image: php:7.1.1
image: php:7.1.1
# Select what we should cache between builds
cache:
@ -11,7 +11,7 @@ before_script:
- apt-get update -yqq
- apt-get install -yqq git libmcrypt-dev libpq-dev libcurl4-gnutls-dev libicu-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev
# Install PHP extensions
- docker-php-ext-install mbstring mcrypt pdo_pgsql curl json intl gd xml zip bz2 opcache
- docker-php-ext-install mbstring curl json intl gd xml zip bz2 opcache
# Install & enable Xdebug for code coverage reports
- pecl install xdebug
- docker-php-ext-enable xdebug
@ -19,17 +19,6 @@ before_script:
- curl -sS https://getcomposer.org/installer | php
- php composer.phar install
# Bring in any services we need http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service
# See http://docs.gitlab.com/ce/ci/services/README.html for examples.
services:
- mysql:5.7
# Set any variables we need
variables:
# Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
MYSQL_DATABASE: mysql_database
MYSQL_ROOT_PASSWORD: mysql_strong_password
# Run our tests
# If Xdebug was installed you can generate a coverage report and see code coverage metrics.
test:

View file

@ -0,0 +1,74 @@
# Flatrr
## What Flatrr does
Flatrr is a utility library for accessing arrays via flattened key names. So, for example, rather than using `$arr['foo']['bar']` you could use `$arr['foo.bar']`. Mostly this is useful if you want to use string building to make the keys you're going to use to access an array.
It should be noted that because of the way arrays and references work, this is not going to work *exactly* the same way as a native array in all cases. There are actually countless tiny caveats, and with that in mind you should generally stick to using this library as it is documented. Using undocumented features is exceptionally unpredictable due to the nature of this tool, and things may work radically different under the hood in the future.
## FlatArray
The main FlatArray class is very simple. It only implements \Array and \Iterator. With it you can make arrays, merge values into them, and retrieve parts of the array via flattened keys.
### Constructing and getting values
```php
// Instantiating FlatArrays can be done by passing them an array
// Keys in the initial array will be unflattened, for example the following
// yields a FlatArray containing ['foo'=>'bar','bar'=>['baz'=>'a','buz'=>'u']]
$f = new \Flatrr\FlatArray([
'foo' => 'bar',
'bar.baz' => 'a',
'bar.buz' => 'u'
]);
```
You can then access the values of the array like a normal array, through the `get()` method, or by flattened keys.
```php
// All of the following are equal to 'a'
$f['bar']['baz'];
$f['bar.baz'];
$f->get('bar.baz');
```
### Setting values
Values can be set through either flattened keys or the `set()` method. Setting values like a normal multi-dimensional array won't work beyond the first layer, and it shouldn't be done.
```php
// Both of these work
$f['foo.bar'] = 'baz';
$f->set('foo.bar','baz');
// This does NOT work
$f['foo']['bar'] = 'baz';
```
## SelfReferencingFlatArray
SelfReferencingFlatArray is a class that does everything FlatArray does, but also allows strings within the array to reference other fields within the array, and include them as strings. For example:
```php
$f = new \Flatrr\SelfReferencingFlatArray([
'foo.bar' => 'baz',
'foo.baz' => '${foo.bar}'
]);
// foo.baz will now always return the value of foo.bar
// echoes 'baz'
echo $f['foo.baz'];
// You can also get the "raw" value of a field using get()
// echoes '${foo.bar}'
echo $f->get('foo.baz');
// Variables can also be used as part of other strings
$f['a'] = 'foo.bar is: ${foo.bar}';
// echoes 'foo.bar is: baz'
echo $f['a'];
// Variables are resolved recursively
$f['b'] = 'foo.baz is: ${foo.baz}';
// echoes 'foo.baz is: baz'
echo $f['b'];
```
## Config
Config does the same things as SelfReferencingFlatArray, but adds methods for reading INI, JSON, and YAML files. It also provides methods for extracting its contents as JSON and YAML.

View file

@ -12,12 +12,12 @@
},
"autoload": {
"psr-4": {
"FlatArray\\": "src/"
"Flatrr\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"FlatArray\\": "tests/"
"Flatrr\\": "tests/"
}
},
"scripts": {
@ -28,4 +28,4 @@
"require": {
"symfony/yaml": "^3.4"
}
}
}

View file

@ -1,10 +1,10 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
namespace FlatArray\Config;
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
namespace Flatrr\Config;
use Symfony\Component\Yaml\Yaml;
use FlatArray\SelfReferencingFlatArray;
use FlatArray\FlatArray;
use Flatrr\SelfReferencingFlatArray;
use Flatrr\FlatArray;
class Config extends SelfReferencingFlatArray implements ConfigInterface
{

View file

@ -1,6 +1,6 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
namespace FlatArray\Config;
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
namespace Flatrr\Config;
interface ConfigInterface extends \ArrayAccess
{

View file

@ -1,6 +1,6 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
namespace FlatArray;
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
namespace Flatrr;
class FlatArray implements FlatArrayInterface
{

View file

@ -1,6 +1,6 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
namespace FlatArray;
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
namespace Flatrr;
interface FlatArrayInterface extends \ArrayAccess, \Iterator
{

View file

@ -1,6 +1,6 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
namespace FlatArray;
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
namespace Flatrr;
class SelfReferencingFlatArray extends FlatArray
{

View file

@ -1,7 +1,7 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
declare(strict_types=1);
namespace FlatArray\Config;
namespace Flatrr\Config;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;

View file

@ -1,7 +1,7 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
declare(strict_types=1);
namespace FlatArray;
namespace Flatrr;
use PHPUnit\Framework\TestCase;

View file

@ -1,7 +1,7 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
declare(strict_types=1);
namespace FlatArray;
namespace Flatrr;
use PHPUnit\Framework\TestCase;

View file

@ -1,7 +1,7 @@
<?php
/* FlatArray | https://gitlab.com/byjoby/flatarray | MIT License */
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
declare(strict_types=1);
namespace FlatArray;
namespace Flatrr;
use PHPUnit\Framework\TestCase;