├── ChangeLog.md ├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src └── ObjectReflector.php /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to `sebastianbergmann/object-reflector` are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. 4 | 5 | ## [5.0.0] - 2025-02-07 6 | 7 | ### Removed 8 | 9 | * This component is no longer supported on PHP 8.2 10 | 11 | ## [4.0.1] - 2024-07-03 12 | 13 | ### Changed 14 | 15 | * This project now uses PHPStan instead of Psalm for static analysis 16 | 17 | ## [4.0.0] - 2024-02-02 18 | 19 | ### Removed 20 | 21 | * This component is no longer supported on PHP 8.1 22 | 23 | ## [3.0.0] - 2023-02-03 24 | 25 | ### Changed 26 | 27 | * `ObjectReflector::getAttributes()` has been renamed to `ObjectReflector::getProperties()` 28 | 29 | ### Removed 30 | 31 | * This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0 32 | 33 | ## [2.0.4] - 2020-10-26 34 | 35 | ### Fixed 36 | 37 | * `SebastianBergmann\ObjectReflector\Exception` now correctly extends `\Throwable` 38 | 39 | ## [2.0.3] - 2020-09-28 40 | 41 | ### Changed 42 | 43 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 44 | 45 | ## [2.0.2] - 2020-06-26 46 | 47 | ### Added 48 | 49 | * This component is now supported on PHP 8 50 | 51 | ## [2.0.1] - 2020-06-15 52 | 53 | ### Changed 54 | 55 | * Tests etc. are now ignored for archive exports 56 | 57 | ## [2.0.0] - 2020-02-07 58 | 59 | ### Removed 60 | 61 | * This component is no longer supported on PHP 7.0, PHP 7.1, and PHP 7.2 62 | 63 | ## [1.1.1] - 2017-03-29 64 | 65 | * Fixed [#1](https://github.com/sebastianbergmann/object-reflector/issues/1): Attributes with non-string names are not handled correctly 66 | 67 | ## [1.1.0] - 2017-03-16 68 | 69 | ### Changed 70 | 71 | * Changed implementation of `ObjectReflector::getattributes()` to use `(array)` cast instead of `ReflectionObject` 72 | 73 | ## 1.0.0 - 2017-03-12 74 | 75 | * Initial release 76 | 77 | [5.0.0]: https://github.com/sebastianbergmann/object-reflector/compare/4.0...5.0.0 78 | [4.0.1]: https://github.com/sebastianbergmann/object-reflector/compare/4.0.0...4.0.1 79 | [4.0.0]: https://github.com/sebastianbergmann/object-reflector/compare/3.0...4.0.0 80 | [3.0.0]: https://github.com/sebastianbergmann/object-reflector/compare/2.0.4...3.0.0 81 | [2.0.4]: https://github.com/sebastianbergmann/object-reflector/compare/2.0.3...2.0.4 82 | [2.0.3]: https://github.com/sebastianbergmann/object-reflector/compare/2.0.2...2.0.3 83 | [2.0.2]: https://github.com/sebastianbergmann/object-reflector/compare/2.0.1...2.0.2 84 | [2.0.1]: https://github.com/sebastianbergmann/object-reflector/compare/2.0.0...2.0.1 85 | [2.0.0]: https://github.com/sebastianbergmann/object-reflector/compare/1.1.1...2.0.0 86 | [1.1.1]: https://github.com/sebastianbergmann/object-reflector/compare/1.1.0...1.1.1 87 | [1.1.0]: https://github.com/sebastianbergmann/object-reflector/compare/1.0.0...1.1.0 88 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017-2025, Sebastian Bergmann 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/sebastian/object-reflector/v)](https://packagist.org/packages/sebastian/object-reflector) 2 | [![CI Status](https://github.com/sebastianbergmann/object-reflector/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/object-reflector/actions) 3 | [![codecov](https://codecov.io/gh/sebastianbergmann/object-reflector/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/object-reflector) 4 | 5 | # sebastian/object-reflector 6 | 7 | Allows reflection of object properties, including inherited and private as well as protected ones. 8 | 9 | ## Installation 10 | 11 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 12 | 13 | ``` 14 | composer require sebastian/object-reflector 15 | ``` 16 | 17 | If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: 18 | 19 | ``` 20 | composer require --dev sebastian/object-reflector 21 | ``` 22 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure. 4 | 5 | **Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** 6 | 7 | Instead, please email `sebastian@phpunit.de`. 8 | 9 | Please include as much of the information listed below as you can to help us better understand and resolve the issue: 10 | 11 | * The type of issue 12 | * Full paths of source file(s) related to the manifestation of the issue 13 | * The location of the affected source code (tag/branch/commit or direct URL) 14 | * Any special configuration required to reproduce the issue 15 | * Step-by-step instructions to reproduce the issue 16 | * Proof-of-concept or exploit code (if possible) 17 | * Impact of the issue, including how an attacker might exploit the issue 18 | 19 | This information will help us triage your report more quickly. 20 | 21 | ## Web Context 22 | 23 | The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit. 24 | 25 | The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes. 26 | 27 | If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context. 28 | 29 | Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes. 30 | 31 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/object-reflector", 3 | "description": "Allows reflection of object attributes, including inherited and non-public ones", 4 | "homepage": "https://github.com/sebastianbergmann/object-reflector/", 5 | "license": "BSD-3-Clause", 6 | "authors": [ 7 | { 8 | "name": "Sebastian Bergmann", 9 | "email": "sebastian@phpunit.de" 10 | } 11 | ], 12 | "support": { 13 | "issues": "https://github.com/sebastianbergmann/object-reflector/issues", 14 | "security": "https://github.com/sebastianbergmann/object-reflector/security/policy" 15 | }, 16 | "prefer-stable": true, 17 | "config": { 18 | "platform": { 19 | "php": "8.3.0" 20 | }, 21 | "optimize-autoloader": true, 22 | "sort-packages": true 23 | }, 24 | "require": { 25 | "php": ">=8.3" 26 | }, 27 | "require-dev": { 28 | "phpunit/phpunit": "^12.0" 29 | }, 30 | "autoload": { 31 | "classmap": [ 32 | "src/" 33 | ] 34 | }, 35 | "autoload-dev": { 36 | "classmap": [ 37 | "tests/_fixture/" 38 | ] 39 | }, 40 | "extra": { 41 | "branch-alias": { 42 | "dev-main": "5.0-dev" 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/ObjectReflector.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\ObjectReflector; 11 | 12 | use function count; 13 | use function explode; 14 | 15 | final class ObjectReflector 16 | { 17 | /** 18 | * @return array 19 | */ 20 | public function getProperties(object $object): array 21 | { 22 | $properties = []; 23 | $className = $object::class; 24 | 25 | foreach ((array) $object as $name => $value) { 26 | $name = explode("\0", (string) $name); 27 | 28 | if (count($name) === 1) { 29 | $name = $name[0]; 30 | } elseif ($name[1] !== $className) { 31 | $name = $name[1] . '::' . $name[2]; 32 | } else { 33 | $name = $name[2]; 34 | } 35 | 36 | $properties[$name] = $value; 37 | } 38 | 39 | return $properties; 40 | } 41 | } 42 | --------------------------------------------------------------------------------