From 618feeca893c62f0de1e3b4b7e9cddff83101da6 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 15:44:46 -0600 Subject: [PATCH 1/6] A little documentation --- README.md | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/README.md b/README.md index e69de29..ef94349 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,74 @@ +# flatarray + +## What flatarray does + +Flatarray 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 \FlatArray\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 \FlatArray\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. From 7fa7c6c790626e7cea384d5687f253a4c945492d Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 15:51:32 -0600 Subject: [PATCH 2/6] trimming CI settings --- .gitlab-ci.yml | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8aeb537..da63474 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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: @@ -8,8 +8,6 @@ cache: - vendor/ 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 # Install & enable Xdebug for code coverage reports @@ -19,17 +17,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: From a02f8e2d777b93d626e96139e0ddff6e87f4d902 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 15:54:26 -0600 Subject: [PATCH 3/6] fixing ci --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index da63474..fbbf542 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,7 @@ cache: before_script: # 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 From bf52525a82742e815b61c502b6d4b8a89e3bd34d Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 15:56:09 -0600 Subject: [PATCH 4/6] fixing ci --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fbbf542..1a525f2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,6 +8,8 @@ cache: - vendor/ 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 curl json intl gd xml zip bz2 opcache # Install & enable Xdebug for code coverage reports From 36f787ca8fa403d265be0f5152eb4e42946aedf7 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 16:09:45 -0600 Subject: [PATCH 5/6] renaming, for reasons --- README.md | 10 +++++----- composer.json | 4 ++-- src/Config/Config.php | 8 ++++---- src/Config/ConfigInterface.php | 4 ++-- src/FlatArray.php | 4 ++-- src/FlatArrayInterface.php | 4 ++-- src/SelfReferencingFlatArray.php | 4 ++-- tests/Config/ConfigTest.php | 4 ++-- tests/FlatArrayPushPopTest.php | 4 ++-- tests/FlatArrayTest.php | 4 ++-- tests/SelfReferencingFlatArrayTest.php | 4 ++-- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ef94349..8d2636c 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# flatarray +# Flatrr -## What flatarray does +## What Flatrr does -Flatarray 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. +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. @@ -16,7 +16,7 @@ The main FlatArray class is very simple. It only implements \Array and \Iterator // 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 \FlatArray\FlatArray([ +$f = new \Flatrr\FlatArray([ 'foo' => 'bar', 'bar.baz' => 'a', 'bar.buz' => 'u' @@ -49,7 +49,7 @@ $f['foo']['bar'] = 'baz'; 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 \FlatArray\SelfReferencingFlatArray([ +$f = new \Flatrr\SelfReferencingFlatArray([ 'foo.bar' => 'baz', 'foo.baz' => '${foo.bar}' ]); diff --git a/composer.json b/composer.json index eb3779f..f119a2d 100644 --- a/composer.json +++ b/composer.json @@ -12,12 +12,12 @@ }, "autoload": { "psr-4": { - "FlatArray\\": "src/" + "Flatrr\\": "src/" } }, "autoload-dev": { "psr-4": { - "FlatArray\\": "tests/" + "Flatrr\\": "tests/" } }, "scripts": { diff --git a/src/Config/Config.php b/src/Config/Config.php index 2f0e6dc..6d7ffa4 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -1,10 +1,10 @@ Date: Fri, 17 Aug 2018 16:21:05 -0600 Subject: [PATCH 6/6] renaming --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index f119a2d..3a938e6 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "byjoby/flatarray", + "name": "byjoby/flatrr", "description": "A library for working with multi-dimensional arrays through flattened keys", "type": "library", "license": "MIT", @@ -28,4 +28,4 @@ "require": { "symfony/yaml": "^3.4" } -} +} \ No newline at end of file