Source of file ArrayProxyTrait.php
Size: 4,944 Bytes - Last Modified: 2021-01-12T22:04:13+00:00
C:/Users/MAKS/Code/_PROJECTS/amqp-agent/src/Helper/ArrayProxyTrait.php
| 1234567891011121314151617181920212223242526272829303132 
                                Covered by 4 test(s):
                            33 
 
                                Covered by 1 test(s):
                            343536 
 
                                Covered by 4 test(s):
                            3738 
 
                                Covered by 4 test(s):
                            39 
 
                                Covered by 4 test(s):
                            4041 
 
                                Covered by 4 test(s):
                            42 
 
                                Covered by 4 test(s):
                            43 
 
                                Covered by 2 test(s):
                            444546 
 
                                Covered by 4 test(s):
                            474849 
 
                                Covered by 4 test(s):
                            505152 
 
                                Covered by 4 test(s):
                            535455565758596061626364 
 
                                Covered by 2 test(s):
                            65 
 
                                Covered by 1 test(s):
                            666768 
 
                                Covered by 2 test(s):
                            69 
 
                                Covered by 2 test(s):
                            7071 
 
                                Covered by 2 test(s):
                            7273 
 
                                Covered by 2 test(s):
                            74 
 
                                Covered by 2 test(s):
                            75 
 
                                Covered by 2 test(s):
                            76 
 
                                Covered by 2 test(s):
                            777879 
 
                                Covered by 2 test(s):
                            80818283 
 
                                Covered by 2 test(s):
                            8485 
 
                                Covered by 2 test(s):
                            86878889909192939495 
 
                                Covered by 4 test(s):
                            9697 
 
                                Covered by 4 test(s):
                            9899 
 
                                Covered by 2 test(s):
                            100 
 
                                Covered by 1 test(s):
                            101 
 
                                Covered by 1 test(s):
                            102 
 
                                Covered by 2 test(s):
                            103 
 
                                Covered by 1 test(s):
                            104 
 
                                Covered by 1 test(s):
                            105 
 
                                Covered by 2 test(s):
                            106 
 
                                Covered by 2 test(s):
                            107 
 
                                Covered by 2 test(s):
                            108 
 
                                Covered by 1 test(s):
                            109 
 
                                Covered by 1 test(s):
                            110 
 
                                Covered by 1 test(s):
                            111 
 
                                Covered by 1 test(s):
                            112 
 
                                Covered by 1 test(s):
                            113 
 
                                Covered by 1 test(s):
                            114115 
 
                                Covered by 1 test(s):
                            116117118119 
 
                                Covered by 4 test(s):
                            120121122123124125126127128129130 
 
                                Covered by 2 test(s):
                            131 
 
                                Covered by 1 test(s):
                            132133134 
 
                                Covered by 1 test(s):
                            135136 
 
                                Covered by 1 test(s):
                            137 
 
                                Covered by 1 test(s):
                            138 
 
                                Covered by 1 test(s):
                            139 
 
                                Covered by 1 test(s):
                            140141142 
 
                                Covered by 1 test(s):
                            143144145146147148149150151152153 
 
                                Covered by 2 test(s):
                            154 
 
                                Covered by 1 test(s):
                            155156157 
 
                                Covered by 1 test(s):
                            158159 
 
                                Covered by 1 test(s):
                            160 
 
                                Covered by 1 test(s):
                            161 
 
                                Covered by 1 test(s):
                            162 
 
                                Covered by 1 test(s):
                            163 
 
                                Covered by 1 test(s):
                            164165166 
 
                                Covered by 1 test(s):
                            167168169 
 | <?php/** * @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com> * @copyright Marwan Al-Soltany 2020 * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */declare(strict_types=1); namespace MAKS\AmqpAgent\Helper; use stdClass; use ReflectionObject; /** * A trait containing methods for for manipulating and working with arrays. * @since 2.0.0 */trait ArrayProxyTrait {/**      * Gets a value from an array via dot-notation representation.     * @param array &$array The array to get the value from.     * @param string $key The dotted key representation.     * @param mixed $default [optional] The default fallback value.     * @return mixed The requested value if found otherwise the default parameter.     */public static function getArrayValueByKey(array &$array, string $key, $default = null) { if (!strlen($key) || !count($array)) { return $default; } $data = &$array; if (strpos($key, '.') !== false) { $parts = explode('.', $key); foreach ($parts as $part) { if (!array_key_exists($part, $data)) { return $default; } $data = &$data[$part]; } return $data; } return array_key_exists($key, $data) ? $data[$key] : $default; } /**      * Sets a value of an array via dot-notation representation.     * @param array $array The array to set the value in.     * @param string $key The string key representation.     * @param mixed $value The value to set.     * @return bool True on success.     */public static function setArrayValueByKey(array &$array, string $key, $value): bool { if (!strlen($key)) { return false; } $parts = explode('.', $key); $lastPart = array_pop($parts); $data = &$array; if (!empty($parts)) { foreach ($parts as $part) { if (!isset($data[$part])) { $data[$part] = []; } $data = &$data[$part]; } } $data[$lastPart] = $value; return true; } /**      * Returns a string representation of an array by imploding it recursively with common formatting of data-types.     * @param array $array The array to implode.     * @return string     */public static function castArrayToString(array $array): string { $pieces = []; foreach ($array as $item) { switch (true) { case (is_array($item)): $pieces[] = self::castArrayToString($item); break; case (is_object($item)): $pieces[] = get_class($item) ?? 'object'; break; case (is_string($item)): $pieces[] = "'{$item}'"; break; case (is_bool($item)): $pieces[] = $item ? 'true' : 'false'; break; case (is_null($item)): $pieces[] = 'null'; break; default: $pieces[] = $item; } } return '[' . implode(', ', $pieces) . ']'; } /**      * Converts (casts) an array to an object (stdClass).     * @param array $array The array to convert.     * @param bool $useJson [optional] Whether to use json_decode/json_encode to cast the array, default is via iteration.     * @return stdClass The result object.     */public static function castArrayToObject(array $array, bool $useJson = false): stdClass { if ($useJson) { return json_decode(json_encode($array)); } $stdClass = new stdClass(); foreach ($array as $key => $value) { $stdClass->{$key} = is_array($value) ? self::castArrayToObject($value, $useJson) : $value; } return $stdClass; } /**      * Converts (casts) an object to an associative array.     * @param object $object The object to convert.     * @param bool $useJson [optional] Whether to use json_decode/json_encode to cast the object, default is via reflection.     * @return array The result array.     */public static function castObjectToArray($object, bool $useJson = false): array { if ($useJson) { return json_decode(json_encode($object), true); } $array = []; $reflectionClass = new ReflectionObject($object); foreach ($reflectionClass->getProperties() as $property) { $property->setAccessible(true); $array[$property->getName()] = $property->getValue($object); $property->setAccessible(false); } return $array; } } |