From 8cff6933879a3c6eb0a777ac8de5a0106832e9ac Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 17 Aug 2018 15:02:50 -0600 Subject: [PATCH] initial commit --- .gitignore | 1 + .gitlab-ci.yml | 37 + composer.json | 31 + composer.lock | 1541 ++++++++++++++++++++++++ phpunit.xml | 7 + src/Config/Config.php | 74 ++ src/Config/ConfigInterface.php | 12 + src/FlatArray.php | 220 ++++ src/FlatArrayInterface.php | 17 + src/SelfReferencingFlatArray.php | 90 ++ tests/Config/ConfigTest.php | 55 + tests/Config/configtest.json | 6 + tests/Config/configtest.yaml | 3 + tests/FlatArrayPushPopTest.php | 33 + tests/FlatArrayTest.php | 177 +++ tests/SelfReferencingFlatArrayTest.php | 53 + 16 files changed, 2357 insertions(+) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 phpunit.xml create mode 100644 src/Config/Config.php create mode 100644 src/Config/ConfigInterface.php create mode 100644 src/FlatArray.php create mode 100644 src/FlatArrayInterface.php create mode 100644 src/SelfReferencingFlatArray.php create mode 100644 tests/Config/ConfigTest.php create mode 100644 tests/Config/configtest.json create mode 100644 tests/Config/configtest.yaml create mode 100644 tests/FlatArrayPushPopTest.php create mode 100644 tests/FlatArrayTest.php create mode 100644 tests/SelfReferencingFlatArrayTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..8aeb537 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,37 @@ +# 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 + +# Select what we should cache between builds +cache: + paths: + - 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 +- pecl install xdebug +- docker-php-ext-enable xdebug +# Install and run Composer +- 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: + script: + - vendor/bin/phpunit --configuration phpunit.xml --coverage-text --colors=never diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..eb3779f --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "byjoby/flatarray", + "description": "A library for working with multi-dimensional arrays through flattened keys", + "type": "library", + "license": "MIT", + "authors": [{ + "name": "Joby Elliott", + "email": "joby@byjoby.com" + }], + "require-dev": { + "phpunit/phpunit": "^7.3" + }, + "autoload": { + "psr-4": { + "FlatArray\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "FlatArray\\": "tests/" + } + }, + "scripts": { + "test": [ + "phpunit" + ] + }, + "require": { + "symfony/yaml": "^3.4" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..7ac1fb1 --- /dev/null +++ b/composer.lock @@ -0,0 +1,1541 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "b0ee3fcf8b5d85e383dd8be989bad9ca", + "packages": [ + { + "name": "symfony/polyfill-ctype", + "version": "v1.9.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "e3d826245268269cd66f8326bd8bc066687b4a19" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e3d826245268269cd66f8326bd8bc066687b4a19", + "reference": "e3d826245268269cd66f8326bd8bc066687b4a19", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.9-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + }, + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "time": "2018-08-06T14:22:27+00:00" + }, + { + "name": "symfony/yaml", + "version": "v3.4.14", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "810af2d35fc72b6cf5c01116806d2b65ccaaf2e2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/810af2d35fc72b6cf5c01116806d2b65ccaaf2e2", + "reference": "810af2d35fc72b6cf5c01116806d2b65ccaaf2e2", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-ctype": "~1.8" + }, + "conflict": { + "symfony/console": "<3.4" + }, + "require-dev": { + "symfony/console": "~3.4|~4.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2018-07-26T11:19:56+00:00" + } + ], + "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", + "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "^6.2.3", + "squizlabs/php_codesniffer": "^3.0.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2017-07-22T11:58:36+00:00" + }, + { + "name": "myclabs/deep-copy", + "version": "1.8.1", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8", + "reference": "3e01bdad3e18354c3dce54466b7fbe33a9f9f7f8", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "replace": { + "myclabs/deep-copy": "self.version" + }, + "require-dev": { + "doctrine/collections": "^1.0", + "doctrine/common": "^2.6", + "phpunit/phpunit": "^7.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + }, + "files": [ + "src/DeepCopy/deep_copy.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "time": "2018-06-11T23:09:50+00:00" + }, + { + "name": "phar-io/manifest", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/phar-io/manifest.git", + "reference": "7761fcacf03b4d4f16e7ccb606d4879ca431fcf4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/7761fcacf03b4d4f16e7ccb606d4879ca431fcf4", + "reference": "7761fcacf03b4d4f16e7ccb606d4879ca431fcf4", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-phar": "*", + "phar-io/version": "^2.0", + "php": "^5.6 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", + "time": "2018-07-08T19:23:20+00:00" + }, + { + "name": "phar-io/version", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/version.git", + "reference": "45a2ec53a73c70ce41d55cedef9063630abaf1b6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/version/zipball/45a2ec53a73c70ce41d55cedef9063630abaf1b6", + "reference": "45a2ec53a73c70ce41d55cedef9063630abaf1b6", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Library for handling version information and constraints", + "time": "2018-07-08T19:19:57+00:00" + }, + { + "name": "phpdocumentor/reflection-common", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2017-09-11T18:02:19+00:00" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "4.3.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "94fd0001232e47129dd3504189fa1c7225010d08" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/94fd0001232e47129dd3504189fa1c7225010d08", + "reference": "94fd0001232e47129dd3504189fa1c7225010d08", + "shasum": "" + }, + "require": { + "php": "^7.0", + "phpdocumentor/reflection-common": "^1.0.0", + "phpdocumentor/type-resolver": "^0.4.0", + "webmozart/assert": "^1.0" + }, + "require-dev": { + "doctrine/instantiator": "~1.0.5", + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^6.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2017-11-30T07:14:17+00:00" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/9c977708995954784726e25d0cd1dddf4e65b0f7", + "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7", + "shasum": "" + }, + "require": { + "php": "^5.5 || ^7.0", + "phpdocumentor/reflection-common": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.2||^4.8.24" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "time": "2017-07-14T14:27:02+00:00" + }, + { + "name": "phpspec/prophecy", + "version": "1.8.0", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "4ba436b55987b4bf311cb7c6ba82aa528aac0a06" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/4ba436b55987b4bf311cb7c6ba82aa528aac0a06", + "reference": "4ba436b55987b4bf311cb7c6ba82aa528aac0a06", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0", + "sebastian/comparator": "^1.1|^2.0|^3.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0" + }, + "require-dev": { + "phpspec/phpspec": "^2.5|^3.2", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5 || ^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2018-08-05T17:53:17+00:00" + }, + { + "name": "phpunit/php-code-coverage", + "version": "6.0.7", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "865662550c384bc1db7e51d29aeda1c2c161d69a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/865662550c384bc1db7e51d29aeda1c2c161d69a", + "reference": "865662550c384bc1db7e51d29aeda1c2c161d69a", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-xmlwriter": "*", + "php": "^7.1", + "phpunit/php-file-iterator": "^2.0", + "phpunit/php-text-template": "^1.2.1", + "phpunit/php-token-stream": "^3.0", + "sebastian/code-unit-reverse-lookup": "^1.0.1", + "sebastian/environment": "^3.1", + "sebastian/version": "^2.0.1", + "theseer/tokenizer": "^1.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "suggest": { + "ext-xdebug": "^2.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2018-06-01T07:51:50+00:00" + }, + { + "name": "phpunit/php-file-iterator", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "cecbc684605bb0cc288828eb5d65d93d5c676d3c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/cecbc684605bb0cc288828eb5d65d93d5c676d3c", + "reference": "cecbc684605bb0cc288828eb5d65d93d5c676d3c", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2018-06-11T11:44:00+00:00" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21T13:50:34+00:00" + }, + { + "name": "phpunit/php-timer", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "8b8454ea6958c3dee38453d3bd571e023108c91f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/8b8454ea6958c3dee38453d3bd571e023108c91f", + "reference": "8b8454ea6958c3dee38453d3bd571e023108c91f", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2018-02-01T13:07:23+00:00" + }, + { + "name": "phpunit/php-token-stream", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "21ad88bbba7c3d93530d93994e0a33cd45f02ace" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/21ad88bbba7c3d93530d93994e0a33cd45f02ace", + "reference": "21ad88bbba7c3d93530d93994e0a33cd45f02ace", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2018-02-01T13:16:43+00:00" + }, + { + "name": "phpunit/phpunit", + "version": "7.3.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "f9b14c17860eccb440a0352a117a81eb754cff5a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f9b14c17860eccb440a0352a117a81eb754cff5a", + "reference": "f9b14c17860eccb440a0352a117a81eb754cff5a", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.1", + "ext-dom": "*", + "ext-json": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-xml": "*", + "myclabs/deep-copy": "^1.7", + "phar-io/manifest": "^1.0.2", + "phar-io/version": "^2.0", + "php": "^7.1", + "phpspec/prophecy": "^1.7", + "phpunit/php-code-coverage": "^6.0.7", + "phpunit/php-file-iterator": "^2.0.1", + "phpunit/php-text-template": "^1.2.1", + "phpunit/php-timer": "^2.0", + "sebastian/comparator": "^3.0", + "sebastian/diff": "^3.0", + "sebastian/environment": "^3.1", + "sebastian/exporter": "^3.1", + "sebastian/global-state": "^2.0", + "sebastian/object-enumerator": "^3.0.3", + "sebastian/resource-operations": "^1.0", + "sebastian/version": "^2.0.1" + }, + "conflict": { + "phpunit/phpunit-mock-objects": "*" + }, + "require-dev": { + "ext-pdo": "*" + }, + "suggest": { + "ext-soap": "*", + "ext-xdebug": "*", + "phpunit/php-invoker": "^2.0" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "7.3-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2018-08-07T06:44:28+00:00" + }, + { + "name": "sebastian/code-unit-reverse-lookup", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Looks up which function or method a line of code belongs to", + "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "time": "2017-03-04T06:30:41+00:00" + }, + { + "name": "sebastian/comparator", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "5de4fc177adf9bce8df98d8d141a7559d7ccf6da" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/5de4fc177adf9bce8df98d8d141a7559d7ccf6da", + "reference": "5de4fc177adf9bce8df98d8d141a7559d7ccf6da", + "shasum": "" + }, + "require": { + "php": "^7.1", + "sebastian/diff": "^3.0", + "sebastian/exporter": "^3.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "https://github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2018-07-12T15:12:46+00:00" + }, + { + "name": "sebastian/diff", + "version": "3.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "366541b989927187c4ca70490a35615d3fef2dce" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/366541b989927187c4ca70490a35615d3fef2dce", + "reference": "366541b989927187c4ca70490a35615d3fef2dce", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0", + "symfony/process": "^2 || ^3.3 || ^4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff", + "udiff", + "unidiff", + "unified diff" + ], + "time": "2018-06-10T07:54:39+00:00" + }, + { + "name": "sebastian/environment", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "cd0871b3975fb7fc44d11314fd1ee20925fce4f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/cd0871b3975fb7fc44d11314fd1ee20925fce4f5", + "reference": "cd0871b3975fb7fc44d11314fd1ee20925fce4f5", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2017-07-01T08:51:00+00:00" + }, + { + "name": "sebastian/exporter", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/234199f4528de6d12aaa58b612e98f7d36adb937", + "reference": "234199f4528de6d12aaa58b612e98f7d36adb937", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "ext-mbstring": "*", + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2017-04-03T13:19:02+00:00" + }, + { + "name": "sebastian/global-state", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2017-04-27T15:39:26+00:00" + }, + { + "name": "sebastian/object-enumerator", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-enumerator.git", + "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/7cfd9e65d11ffb5af41198476395774d4c8a84c5", + "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/object-reflector": "^1.1.1", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Traverses array structures and object graphs to enumerate all referenced objects", + "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "time": "2017-08-03T12:35:26+00:00" + }, + { + "name": "sebastian/object-reflector", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-reflector.git", + "reference": "773f97c67f28de00d397be301821b06708fca0be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/773f97c67f28de00d397be301821b06708fca0be", + "reference": "773f97c67f28de00d397be301821b06708fca0be", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Allows reflection of object attributes, including inherited and non-public ones", + "homepage": "https://github.com/sebastianbergmann/object-reflector/", + "time": "2017-03-29T09:07:27+00:00" + }, + { + "name": "sebastian/recursion-context", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", + "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2017-03-03T06:23:57+00:00" + }, + { + "name": "sebastian/resource-operations", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/resource-operations.git", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides a list of PHP built-in functions that operate on resources", + "homepage": "https://www.github.com/sebastianbergmann/resource-operations", + "time": "2015-07-28T20:34:47+00:00" + }, + { + "name": "sebastian/version", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2016-10-03T07:35:21+00:00" + }, + { + "name": "theseer/tokenizer", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/theseer/tokenizer.git", + "reference": "cb2f008f3f05af2893a87208fe6a6c4985483f8b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/cb2f008f3f05af2893a87208fe6a6c4985483f8b", + "reference": "cb2f008f3f05af2893a87208fe6a6c4985483f8b", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": "^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + } + ], + "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", + "time": "2017-04-07T12:08:54+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "0df1908962e7a3071564e857d86874dad1ef204a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/0df1908962e7a3071564e857d86874dad1ef204a", + "reference": "0df1908962e7a3071564e857d86874dad1ef204a", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2018-01-29T19:49:41+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..89c010e --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,7 @@ + + + + tests + + + diff --git a/src/Config/Config.php b/src/Config/Config.php new file mode 100644 index 0000000..2f0e6dc --- /dev/null +++ b/src/Config/Config.php @@ -0,0 +1,74 @@ +$fn($input)) { + return $out; + } + return array(); + } + + protected function parse_yaml($input) + { + return Yaml::parse($input); + } + + public function json($raw = false) : string + { + return json_encode($this->get(null, $raw), JSON_PRETTY_PRINT); + } + + public function yaml($raw = false) : string + { + return Yaml::dump($this->get(null, $raw), 10); + } + + protected function read_ini($filename) + { + return parse_ini_file($filename, true); + } + + protected function read_json($filename) + { + return json_decode(file_get_contents($filename), true); + } + + protected function read_yaml($filename) + { + return Yaml::parse(file_get_contents($filename)); + } + + protected function read_yml($filename) + { + return $this->read_yaml($filename); + } + + public function readFile($filename, string $name = null, bool $overwrite = false) + { + if (!is_file($filename) || !is_readable($filename)) { + throw new \Exception("Couldn't read config file \"$filename\""); + } + $format = strtolower(preg_replace('/.+\./', '', $filename)); + $fn = 'read_'.$format; + if (!method_exists($this, $fn)) { + throw new \Exception("Don't know how to read the format \"$format\""); + } + $data = $this->$fn($filename); + if (!$data) { + throw new \Exception("Error reading \"".$filename."\""); + } + $this->merge($data, $name, $overwrite); + } +} diff --git a/src/Config/ConfigInterface.php b/src/Config/ConfigInterface.php new file mode 100644 index 0000000..af81b50 --- /dev/null +++ b/src/Config/ConfigInterface.php @@ -0,0 +1,12 @@ +merge($data); + } + + public function push(?string $name, $value) + { + $arr = $this->get($name); + if ($arr !== null && !is_array($arr)) { + return; + } + $arr[] = $value; + $this->set($name, $arr); + } + + public function pop(?string $name) + { + $arr = $this->get($name); + if ($arr !== null && !is_array($arr)) { + return; + } + $out = array_pop($arr); + $this->set($name, $arr); + return $out; + } + + public function shift(?string $name, $value) + { + $arr = $this->get($name); + if ($arr !== null && !is_array($arr)) { + return; + } + array_unshift($arr, $value); + $this->set($name, $arr); + } + + public function unshift(?string $name) + { + $arr = $this->get($name); + if ($arr !== null && !is_array($arr)) { + return; + } + $out = array_shift($arr); + $this->set($name, $arr); + return $out; + } + + public function set(?string $name, $value) + { + return $this->flattenSearch($name, $value); + } + + public function get(?string $name = null) + { + return $this->flattenSearch($name); + } + + public function unset(?string $name) + { + $this->flattenSearch($name, null, true); + } + + public function offsetSet($name, $value) + { + return $this->set($name, $value); + } + + public function offsetGet($name) + { + return $this->get($name); + } + + public function offsetExists($name) + { + return $this->flattenSearch($name) !== null; + } + + public function offsetUnset($name) + { + return $this->unset($name); + } + + public function rewind() + { + return reset($this->_arrayData); + } + + public function current() + { + return current($this->_arrayData); + } + + public function next() + { + return next($this->_arrayData); + } + + public function key() + { + return key($this->_arrayData); + } + + public function valid() + { + return isset($this->_arrayData[$this->key()]); + } + + /** + * Recursively set a value, with control over whether existing values or new + * values take precedence + */ + public function merge($value, string $name = null, bool $overwrite = false) + { + if (!isset($this[$name])) { + //easiest possible outcome, old value doesn't exist, so we can just write the value + $this->set($name, $value); + return; + } elseif (is_array($value) && is_array($this->get($name))) { + //both new and old values are arrays + foreach ($value as $k => $v) { + if ($name) { + $k = $name.'.'.$k; + } + $this->merge($v, $k, $overwrite); + } + return; + } else { + //old and new values exist, and one or both are not arrays, $overwrite rules the day + if ($overwrite) { + $this->set($name, $value); + } + return; + } + } + + /** + * Recursively builds a reference down into $this->config from a config key + * string. It sets it if $value exists, otherwise it returns the value if it + * exists. + */ + protected function flattenSearch(?string $name, $value = null, $unset = false) + { + //normalize key names in $value, and also $name + $value = $this->normalizeValue($value); + $name = preg_replace('/[^a-z0-9\-\_\.]/', '', strtolower($name)); + //check for home strings + if ($name == '' || $name === null) { + if ($unset) { + $this->_arrayData = []; + } elseif ($value !== null) { + $this->_arrayData = $value; + } + return $this->_arrayData; + } + //validate + if (substr($name, -1, 1) == '.' || substr($name, 0, 1) == '..' || strpos($name, '..') !== false) { + return null; + } + //build a reference to where this name should be + $parent = &$this->_arrayData; + $name = explode('.', $name); + $key = array_pop($name); + foreach ($name as $part) { + if (!isset($parent[$part])) { + if ($value !== null) { + $parent[$part] = array(); + } else { + return null; + } + } + $parent = &$parent[$part]; + } + //now we have a ref, see if we can unset or set it + if ($unset) { + unset($parent[$key]); + return null; + } elseif ($value !== null) { + if (is_array(@$parent[$key]) && is_array($value)) { + //both value and destination are arrays, merge them + $parent[$key] = array_replace_recursive($parent[$key], $value); + } else { + //set the hard way + if (!is_array($parent)) { + $parent = array(); + } + $parent[$key] = $value; + } + return null; + } + //return value + if (!is_array($parent)) { + return null; + } + return @$parent[$key]; + } + + protected function normalizeValue($value) + { + if (!is_array($value)) { + return $value; + } + $norm = []; + foreach ($value as $key => $value) { + $nKey = preg_replace('/[^a-z0-9\-\_]/', '', strtolower($key)); + if ($nKey == '') { + throw new \Exception("Key \"$key\" can't be used inside a FlatArray"); + } + $norm[$nKey] = $this->normalizeValue($value); + } + return $norm; + } +} diff --git a/src/FlatArrayInterface.php b/src/FlatArrayInterface.php new file mode 100644 index 0000000..e09ca9f --- /dev/null +++ b/src/FlatArrayInterface.php @@ -0,0 +1,17 @@ +filter($out); + if ($unescape) { + $out = $this->unescape($out); + } + return $out; + } + + protected function unescape($value) + { + if ($value === null) { + return null; + } + //search/replace on string values + if (is_string($value)) { + //unescape references + $value = preg_replace( + '/\$\\\?\{([^\}\\\]+)\\\?\}/', + '\${$1}', + $value + ); + //return + return $value; + } + //map this function onto array values + if (is_array($value)) { + return array_map( + function ($i) { + return $this->filter($i); + }, + $value + ); + } + //fall back to just returning value, it's some other datatype + return $value; + } + + /** + * Recursively replace ${var/name} type strings in string values with + */ + protected function filter($value) + { + if ($value === null) { + return null; + } + //search/replace on string values + if (is_string($value)) { + //search for valid replacements + $value = preg_replace_callback( + //search for things like ${var/name}, escape with \ before last brace + '/\$\{([^\}]*[^\.\\\])\}/', + //replace match with value from $this if it exists + function ($matches) { + if (null !== $value = $this->get($matches[1], false, false)) { + if (!is_array($value)) { + return $value; + } + } + return $matches[0]; + }, + //applied to $value + $value + ); + //return + return $value; + } + //map this function onto array values + if (is_array($value)) { + return array_map( + function ($i) { + return $this->filter($i); + }, + $value + ); + } + //fall back to just returning value, it's some other datatype + return $value; + } +} diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php new file mode 100644 index 0000000..d2673eb --- /dev/null +++ b/tests/Config/ConfigTest.php @@ -0,0 +1,55 @@ +assertEquals('a', $c['c']); + //variable referencing another variable + $c['d'] = '${c}'; + $this->assertEquals('a', $c['d']); + } + + public function testReading() + { + $data = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e' + ] + ]; + //json + $a = new Config(); + $a->readFile(__DIR__.'/configtest.json'); + $this->assertEquals($data, $a->get()); + //yaml + $a = new Config(); + $a->readFile(__DIR__.'/configtest.yaml'); + $this->assertEquals($data, $a->get()); + } + + public function testSerializing() + { + $data = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e' + ] + ]; + $c = new Config($data); + //json + $this->assertEquals($data, json_decode($c->json(), true)); + //yaml + $this->assertEquals($data, Yaml::parse($c->yaml())); + } +} diff --git a/tests/Config/configtest.json b/tests/Config/configtest.json new file mode 100644 index 0000000..4f46098 --- /dev/null +++ b/tests/Config/configtest.json @@ -0,0 +1,6 @@ +{ + "a": "b", + "c": { + "d": "e" + } +} \ No newline at end of file diff --git a/tests/Config/configtest.yaml b/tests/Config/configtest.yaml new file mode 100644 index 0000000..2bda9a4 --- /dev/null +++ b/tests/Config/configtest.yaml @@ -0,0 +1,3 @@ +a: b +c: + d: e diff --git a/tests/FlatArrayPushPopTest.php b/tests/FlatArrayPushPopTest.php new file mode 100644 index 0000000..830d13d --- /dev/null +++ b/tests/FlatArrayPushPopTest.php @@ -0,0 +1,33 @@ +push(null, 'foo'); + $f->push(null, 'bar'); + $this->assertEquals(['foo','bar'], $f->get()); + $this->assertEquals('bar', $f->pop(null)); + $this->assertEquals(['foo'], $f->get()); + $this->assertEquals('foo', $f->pop(null)); + $this->assertEquals([], $f->get()); + } + + public function testShiftUnshift() + { + $f = new FlatArray(); + $f->shift(null, 'foo'); + $f->shift(null, 'bar'); + $this->assertEquals(['bar','foo'], $f->get()); + $this->assertEquals('bar', $f->unshift(null)); + $this->assertEquals(['foo'], $f->get()); + $this->assertEquals('foo', $f->unshift(null)); + $this->assertEquals([], $f->get()); + } +} diff --git a/tests/FlatArrayTest.php b/tests/FlatArrayTest.php new file mode 100644 index 0000000..8dc470a --- /dev/null +++ b/tests/FlatArrayTest.php @@ -0,0 +1,177 @@ + 'A', + 'b' => ['c'=>'C'] + ]; + $a = new FlatArray($data); + //first level + $this->assertEquals('A', $a['a']); + $this->assertEquals('A', $a->get('a')); + //nested + $this->assertEquals('C', $a['b.c']); + $this->assertEquals('C', $a->get('b.c')); + //returning array + $this->assertEquals(['c'=>'C'], $a['b']); + $this->assertEquals(['c'=>'C'], $a->get('b')); + //returning entire array by requesting null or empty string + $this->assertEquals($data, $a[null]); + $this->assertEquals($data, $a->get()); + $this->assertEquals($data, $a['']); + $this->assertEquals($data, $a->get('')); + //requesting invalid keys should return null + $this->assertNull($a->get('nonexistent')); + $this->assertNull($a->get('b.nonexistent')); + $this->assertNull($a->get('..')); + $this->assertNull($a->get('.')); + //double dots + $this->assertNull($a->get('..a')); + $this->assertNull($a->get('a..')); + $this->assertNull($a->get('..a..')); + $this->assertNull($a->get('..a..')); + $this->assertNull($a->get('b..c')); + $this->assertNull($a->get('b..c..')); + $this->assertNull($a->get('..b..c')); + $this->assertNull($a->get('..b..c..')); + $this->assertNull($a->get('b.c..')); + $this->assertNull($a->get('..b.c')); + $this->assertNull($a->get('..b.c..')); + //single dots + $this->assertNull($a->get('.a')); + $this->assertNull($a->get('a.')); + $this->assertNull($a->get('.a.')); + $this->assertNull($a->get('.a.')); + $this->assertNull($a->get('b.c.')); + $this->assertNull($a->get('.b.c')); + $this->assertNull($a->get('.b.c.')); + $this->assertNull($a->get('b.c.')); + $this->assertNull($a->get('.b.c')); + $this->assertNull($a->get('.b.c.')); + } + + public function testSetting() + { + $data = [ + 'a' => 'A', + 'b' => ['c'=>'C'] + ]; + $a = new FlatArray($data); + //setting on first layer + $a['a'] = 'B'; + $this->assertEquals('B', $a['a']); + $a['new'] = 'NEW'; + $this->assertEquals('NEW', $a['new']); + //setting nested + $a['b.c'] = 'D'; + $this->assertEquals('D', $a['b.c']); + $a['b.new'] = 'NEW'; + $this->assertEquals('NEW', $a['b.new']); + //final state + $this->assertEquals( + [ + 'a' => 'B', + 'b' => [ + 'c' => 'D', + 'new' => 'NEW' + ], + 'new' => 'NEW' + ], + $a->get() + ); + } + + public function testCaseNormalizing() + { + $h = new FlatArray([ + 'ABC'=>['ABC'=>'ABC'] + ]); + $this->assertEquals('ABC', $h['abc.abc']); + $this->assertEquals('ABC', $h['Abc.aBC']); + $h['Abc.aBC'] = 'abc'; + $this->assertEquals('abc', $h['abc.abc']); + $this->assertEquals('abc', $h['Abc.aBC']); + $h['ABC'] = ['ABC'=>'ABC']; + $this->assertEquals('ABC', $h['abc.abc']); + $this->assertEquals('ABC', $h['Abc.aBC']); + } + + public function testAccidentalSubstrings() + { + $h = new FlatArray(['foo'=>'bar']); + $this->assertNull($h['foo.baz']); + } + + public function testMerge() + { + $data = [ + 'a' => 'b', + 'c' => [ + 'd' => 'e' + ] + ]; + //overwrite false, original values should be preserved + $c = new FlatArray($data); + $c->merge([ + 'a' => 'B', + 'c' => [ + 'd' => 'E', + 'f' => 'g' + ], + 'h' => 'i' + ]); + $this->assertEquals('b', $c['a']); + $this->assertEquals('e', $c['c.d']); + $this->assertEquals('i', $c['h']); + $this->assertEquals('g', $c['c.f']); + //overwrite true, original values should be overwritten + $c = new FlatArray($data); + $c->merge([ + 'a' => 'B', + 'c' => [ + 'd' => 'E', + 'f' => 'g' + ], + 'h' => 'i' + ], null, true); + $this->assertEquals('B', $c['a']); + $this->assertEquals('E', $c['c.d']); + $this->assertEquals('i', $c['h']); + $this->assertEquals('g', $c['c.f']); + //overwrite false with mismatched array-ness + $c = new FlatArray($data); + $c->merge([ + 'a' => ['b'=>'c'], + 'c' => 'd' + ]); + $this->assertEquals('b', $c['a']); + $this->assertEquals('e', $c['c.d']); + //overwrite true with mismatched array-ness + $c = new FlatArray($data); + $c->merge([ + 'a' => ['b'=>'c'], + 'c' => 'd' + ], null, true); + $this->assertEquals('c', $c['a.b']); + $this->assertEquals('d', $c['c']); + } + + public function testConstructionUnflattening() + { + $arr = new FlatArray([ + 'foo.bar' => 'baz' + ]); + $this->assertEquals( + ['foo'=>['bar'=>'baz']], + $arr->get() + ); + } +} diff --git a/tests/SelfReferencingFlatArrayTest.php b/tests/SelfReferencingFlatArrayTest.php new file mode 100644 index 0000000..2baa9db --- /dev/null +++ b/tests/SelfReferencingFlatArrayTest.php @@ -0,0 +1,53 @@ + 'bar', + 'bar' => ['baz'=>'qux'], + 'test' => [ + 'foo' => '${foo}', + 'bar' => '${bar.baz}', + 'none' => '${foo.none}', + 'escaped' => [ + 'full' => '$\\{foo\\}', + 'left' => '$\\{foo}', + 'right' => '${foo\\}' + ], + ], + 'nested' => [ + 'foo' => '${test.foo}', + 'bar' => '${test.bar}', + 'none' => '${test.none}', + 'escaped' => [ + 'full' => '${test.escaped.full}', + 'left' => '${test.escaped.left}', + 'right' => '${test.escaped.right}' + ] + ] + ]); + //basic references + $this->assertEquals('bar', $f['test.foo']); + $this->assertEquals('qux', $f['test.bar']); + $this->assertEquals('${foo.none}', $f['test.none']); + //nested references + $this->assertEquals('bar', $f['nested.foo']); + $this->assertEquals('qux', $f['nested.bar']); + $this->assertEquals('${foo.none}', $f['nested.none']); + //escaped + $this->assertEquals('${foo}', $f['test.escaped.full']); + $this->assertEquals('${foo}', $f['test.escaped.left']); + $this->assertEquals('${foo}', $f['test.escaped.right']); + //nested and escaped + $this->assertEquals('${foo}', $f['nested.escaped.full']); + $this->assertEquals('${foo}', $f['nested.escaped.left']); + $this->assertEquals('${foo}', $f['nested.escaped.right']); + } +}