├── .gitattributes ├── composer.json ├── LICENSE.txt ├── README.md └── src └── ArrayReader.php /.gitattributes: -------------------------------------------------------------------------------- 1 | /tests export-ignore 2 | .coveralls.yml export-ignore 3 | .docheader export-ignore 4 | .gitignore export-ignore 5 | .php_cs export-ignore 6 | .travis.yml export-ignore 7 | phpunit.xml.dist export-ignore -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "codeliner/array-reader", 3 | "description": "PHP ArrayReader", 4 | "license": "BSD-3-Clause", 5 | "homepage": "https://github.com/codeliner/array-reader", 6 | "authors": [ 7 | { 8 | "name": "Alexander Miertsch", 9 | "email": "kontakt@codeliner.ws", 10 | "homepage": "http://www.codeliner.ws/" 11 | } 12 | ], 13 | "keywords": [ 14 | "php", 15 | "ArrayReader", 16 | "util" 17 | ], 18 | "require": { 19 | "php": ">=7.1" 20 | }, 21 | "require-dev" : { 22 | "phpunit/phpunit": ">=7.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Codeliner\\ArrayReader\\": "src" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "Codeliner\\ArrayReaderTest\\": "tests" 32 | } 33 | }, 34 | "scripts": { 35 | "test": "vendor/bin/phpunit -vvv" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Alexander Miertsch kontakt@codeliner.ws 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of Alexander Miertsch nor the names of its 15 | contributors may be used to endorse or promote products derived from this 16 | software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | array-reader 2 | ============ 3 | 4 | PHP ArrayReader 5 | 6 | [![Build Status](https://travis-ci.org/codeliner/array-reader.png?branch=master)](https://travis-ci.org/codeliner/array-reader) 7 | 8 | 9 | ## Installation 10 | 11 | Installation of codeliner\array-reader uses composer. For composer documentation, please refer to 12 | [getcomposer.org](http://getcomposer.org/). 13 | Add following requirement to your composer.json 14 | 15 | ```sh 16 | "codeliner/array-reader" : "~2.0" 17 | ``` 18 | 19 | ## Usage 20 | 21 | You can use the ArrayReader to read single values from a multidimensional array by passing the path to one 22 | of the `{type}Value()` methods. Each `{type}Value()` method takes a default value as second argument If the path can 23 | not be found in the original array, the default is used as return value. 24 | 25 | ## Example 26 | 27 | ```php 28 | $arrayReader = new ArrayReader( 29 | array( 30 | 'hash' => array( 31 | 'with' => array( 32 | 'nested' => 'value' 33 | ) 34 | ) 35 | ) 36 | ); 37 | 38 | echo $arrayReader->stringValue('hash.with.nested')); 39 | 40 | //Output: value 41 | 42 | $arrayReader = new ArrayReader( 43 | array( 44 | 'hash' => array( 45 | 'with' => array( 46 | 'nested' => 'value' 47 | ) 48 | ) 49 | ) 50 | ); 51 | 52 | echo $arrayReader->stringValue('hash.not.existing.path', 'defaultString')); 53 | 54 | //Output: defaultString 55 | 56 | 57 | //If a key in your array contains a dot you escape it in the path with a backslash 58 | 59 | $arrayReader = new ArrayReader( 60 | array( 61 | 'hash' => array( 62 | 'with.dot.key' => array( 63 | 'nested' => 'value' 64 | ) 65 | ) 66 | ) 67 | ); 68 | 69 | echo $arrayReader->stringValue('hash.with\.dot\.key.nested')); 70 | 71 | //Output: value 72 | 73 | //If you need to differentiate between a NULL value and a not existing path, you can explicity check if the path exists: 74 | 75 | $arrayReader = new ArrayReader( 76 | array( 77 | 'hash' => array( 78 | 'with' => array( 79 | 'nested' => null 80 | ) 81 | ) 82 | ) 83 | ); 84 | 85 | if($arrayReader->pathExists('hash.with.nested')) { 86 | echo "path exists"; 87 | } 88 | 89 | //Output: path exists 90 | ``` 91 | 92 | 93 | -------------------------------------------------------------------------------- /src/ArrayReader.php: -------------------------------------------------------------------------------- 1 | 5 | * 6 | * For the full copyright and license information, please view the LICENSE 7 | * file that was distributed with this source code. 8 | * 9 | * Date: 08.03.14 - 21:28 10 | */ 11 | 12 | namespace Codeliner\ArrayReader; 13 | 14 | /** 15 | * Class ArrayReader 16 | * 17 | * @package Codeliner\ArrayReader 18 | * @author Alexander Miertsch 19 | */ 20 | class ArrayReader 21 | { 22 | /** 23 | * @var array 24 | */ 25 | private $originalArray = []; 26 | 27 | /** 28 | * @param array $anArray 29 | */ 30 | public function __construct(array $anArray) 31 | { 32 | $this->originalArray = $anArray; 33 | } 34 | 35 | public function integerValue(string $aPath, int $default = 0): int 36 | { 37 | $value = $this->getValueFromPath($aPath); 38 | 39 | if ($value === null) { 40 | return $default; 41 | } 42 | 43 | return (int)$value; 44 | } 45 | 46 | public function floatValue(string $aPath, float $default = 0.0): float 47 | { 48 | $value = $this->getValueFromPath($aPath); 49 | 50 | if ($value === null) { 51 | return $default; 52 | } 53 | 54 | return (float)$value; 55 | } 56 | 57 | public function booleanValue(string $aPath, bool $default = false): bool 58 | { 59 | $value = $this->getValueFromPath($aPath); 60 | 61 | if ($value === null) { 62 | return $default; 63 | } 64 | 65 | return (bool)$value; 66 | } 67 | 68 | public function stringValue(string $aPath, string $default = ''): string 69 | { 70 | $value = $this->getValueFromPath($aPath); 71 | 72 | if ($value === null) { 73 | return $default; 74 | } 75 | 76 | return (string)$value; 77 | } 78 | 79 | public function arrayValue(string $aPath, array $default = []): array 80 | { 81 | $value = $this->getValueFromPath($aPath); 82 | 83 | if ($value === null) { 84 | return $default; 85 | } 86 | 87 | if (is_scalar($value)) { 88 | return [$value]; 89 | } 90 | 91 | if (is_object($value)) { 92 | $value = json_decode(json_encode($value), true); 93 | } 94 | 95 | return $value; 96 | } 97 | 98 | /** 99 | * @param string $aPath 100 | * @param mixed $default 101 | * @return mixed 102 | */ 103 | public function mixedValue(string $aPath, $default = null) 104 | { 105 | $value = $this->getValueFromPath($aPath); 106 | 107 | if ($value === null) { 108 | return $default; 109 | } 110 | 111 | return $value; 112 | } 113 | 114 | public function pathExists(string $aPath): bool 115 | { 116 | $pathKeys = $this->toPathKeys($aPath); 117 | 118 | $arrayCopyOrValue = $this->originalArray; 119 | 120 | foreach ($pathKeys as $pathKey) { 121 | 122 | if (!array_key_exists($pathKey, $arrayCopyOrValue)) { 123 | return false; 124 | } 125 | 126 | $arrayCopyOrValue = $arrayCopyOrValue[$pathKey]; 127 | } 128 | 129 | return true; 130 | } 131 | 132 | public function toArray(): array 133 | { 134 | return $this->originalArray; 135 | } 136 | 137 | protected function toPathKeys(string $aPath): array 138 | { 139 | $aPath = str_replace('\.', '___IamADot___', $aPath); 140 | $parts = explode('.', $aPath); 141 | 142 | return array_map(function ($part) { 143 | return str_replace('___IamADot___', '.', $part); 144 | }, $parts); 145 | } 146 | 147 | /** 148 | * @param string $aPath 149 | * @return mixed 150 | */ 151 | protected function getValueFromPath(string $aPath) 152 | { 153 | $pathKeys = $this->toPathKeys($aPath); 154 | 155 | $arrayCopyOrValue = $this->originalArray; 156 | 157 | foreach ($pathKeys as $pathKey) { 158 | 159 | if (!isset($arrayCopyOrValue[$pathKey])) { 160 | return null; 161 | } 162 | 163 | $arrayCopyOrValue = $arrayCopyOrValue[$pathKey]; 164 | } 165 | 166 | return $arrayCopyOrValue; 167 | } 168 | } 169 | --------------------------------------------------------------------------------