├── src ├── exception │ ├── Exception.php │ ├── RuntimeException.php │ ├── OutOfBoundsException.php │ └── InvalidArgumentException.php ├── node │ ├── NodeReference.php │ ├── NodeReferenceCollection.php │ ├── NodeCollectionIterator.php │ ├── NodeReferenceCollectionIterator.php │ ├── NodeCollection.php │ └── Node.php ├── functions.php ├── Builder.php └── DotWriter.php ├── composer.json ├── LICENSE ├── ChangeLog.md ├── SECURITY.md └── README.md /src/exception/Exception.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\ObjectGraph; 11 | 12 | /** 13 | * @internal This interface is not covered by the backward compatibility promise 14 | */ 15 | interface Exception 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/exception/RuntimeException.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\ObjectGraph; 11 | 12 | /** 13 | * @internal This class is not covered by the backward compatibility promise 14 | */ 15 | final class RuntimeException extends \RuntimeException implements Exception 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/exception/OutOfBoundsException.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\ObjectGraph; 11 | 12 | /** 13 | * @internal This class is not covered by the backward compatibility promise 14 | */ 15 | final class OutOfBoundsException extends \OutOfBoundsException implements Exception 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/exception/InvalidArgumentException.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\ObjectGraph; 11 | 12 | /** 13 | * @internal This class is not covered by the backward compatibility promise 14 | */ 15 | final class InvalidArgumentException extends \InvalidArgumentException implements Exception 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/node/NodeReference.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\ObjectGraph; 11 | 12 | /** 13 | * @internal This class is not covered by the backward compatibility promise 14 | */ 15 | final class NodeReference 16 | { 17 | private int $id; 18 | 19 | public function __construct(int $id) 20 | { 21 | $this->id = $id; 22 | } 23 | 24 | public function id(): int 25 | { 26 | return $this->id; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/object-graph", 3 | "description": "Provides useful operations on PHP object graphs", 4 | "homepage": "https://github.com/sebastianbergmann/object-graph/", 5 | "license": "BSD-3-Clause", 6 | "authors": [ 7 | { 8 | "name": "Sebastian Bergmann", 9 | "email": "sebastian@phpunit.de" 10 | } 11 | ], 12 | "config": { 13 | "platform": { 14 | "php": "8.3.0" 15 | }, 16 | "optimize-autoloader": true, 17 | "sort-packages": true 18 | }, 19 | "prefer-stable": true, 20 | "minimum-stability": "dev", 21 | "require": { 22 | "php": ">=8.3", 23 | "sebastian/object-enumerator": "^5.0", 24 | "sebastian/object-reflector": "^3.0" 25 | }, 26 | "autoload": { 27 | "classmap": [ 28 | "src/" 29 | ], 30 | "files": [ 31 | "src/functions.php" 32 | ] 33 | }, 34 | "autoload-dev": { 35 | "classmap": [ 36 | "tests/_fixture/" 37 | ] 38 | }, 39 | "extra": { 40 | "branch-alias": { 41 | "dev-main": "4.0-dev" 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/node/NodeReferenceCollection.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\ObjectGraph; 11 | 12 | use function array_values; 13 | use function count; 14 | use Countable; 15 | use IteratorAggregate; 16 | 17 | /** 18 | * @template-implements IteratorAggregate 19 | * 20 | * @internal This class is not covered by the backward compatibility promise 21 | */ 22 | final class NodeReferenceCollection implements Countable, IteratorAggregate 23 | { 24 | /** 25 | * @var list 26 | */ 27 | private array $references; 28 | 29 | public function __construct(NodeReference ...$references) 30 | { 31 | $this->references = array_values($references); 32 | } 33 | 34 | public function count(): int 35 | { 36 | return count($this->references); 37 | } 38 | 39 | /** 40 | * @return list 41 | */ 42 | public function asArray(): array 43 | { 44 | return $this->references; 45 | } 46 | 47 | public function getIterator(): NodeReferenceCollectionIterator 48 | { 49 | return new NodeReferenceCollectionIterator($this); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/node/NodeCollectionIterator.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\ObjectGraph; 11 | 12 | use function count; 13 | use Iterator; 14 | 15 | /** 16 | * @template-implements Iterator 17 | * 18 | * @internal This class is not covered by the backward compatibility promise 19 | */ 20 | final class NodeCollectionIterator implements Iterator 21 | { 22 | /** 23 | * @var array 24 | */ 25 | private array $nodes; 26 | private int $position = 1; 27 | 28 | public function __construct(NodeCollection $nodes) 29 | { 30 | $this->nodes = $nodes->asArray(); 31 | } 32 | 33 | public function rewind(): void 34 | { 35 | $this->position = 1; 36 | } 37 | 38 | public function valid(): bool 39 | { 40 | return $this->position <= count($this->nodes); 41 | } 42 | 43 | public function key(): int 44 | { 45 | return $this->position; 46 | } 47 | 48 | public function current(): Node 49 | { 50 | return $this->nodes[$this->position]; 51 | } 52 | 53 | public function next(): void 54 | { 55 | $this->position++; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/node/NodeReferenceCollectionIterator.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\ObjectGraph; 11 | 12 | use function count; 13 | use Iterator; 14 | 15 | /** 16 | * @template-implements Iterator 17 | * 18 | * @internal This class is not covered by the backward compatibility promise 19 | */ 20 | final class NodeReferenceCollectionIterator implements Iterator 21 | { 22 | /** 23 | * @var list 24 | */ 25 | private array $references; 26 | private int $position = 0; 27 | 28 | public function __construct(NodeReferenceCollection $references) 29 | { 30 | $this->references = $references->asArray(); 31 | } 32 | 33 | public function rewind(): void 34 | { 35 | $this->position = 0; 36 | } 37 | 38 | public function valid(): bool 39 | { 40 | return $this->position < count($this->references); 41 | } 42 | 43 | public function key(): int 44 | { 45 | return $this->position; 46 | } 47 | 48 | public function current(): NodeReference 49 | { 50 | return $this->references[$this->position]; 51 | } 52 | 53 | public function next(): void 54 | { 55 | $this->position++; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/node/NodeCollection.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\ObjectGraph; 11 | 12 | use function count; 13 | use Countable; 14 | use IteratorAggregate; 15 | 16 | /** 17 | * @template-implements IteratorAggregate 18 | * 19 | * @internal This class is not covered by the backward compatibility promise 20 | */ 21 | final class NodeCollection implements Countable, IteratorAggregate 22 | { 23 | /** 24 | * @var array 25 | */ 26 | private array $nodes = []; 27 | 28 | public function __construct(Node ...$nodes) 29 | { 30 | foreach ($nodes as $node) { 31 | $this->nodes[$node->id()] = $node; 32 | } 33 | } 34 | 35 | public function count(): int 36 | { 37 | return count($this->nodes); 38 | } 39 | 40 | /** 41 | * @return array 42 | */ 43 | public function asArray(): array 44 | { 45 | return $this->nodes; 46 | } 47 | 48 | /** 49 | * @throws OutOfBoundsException 50 | */ 51 | public function findNodeById(int $id): Node 52 | { 53 | if (!isset($this->nodes[$id])) { 54 | throw new OutOfBoundsException; 55 | } 56 | 57 | return $this->nodes[$id]; 58 | } 59 | 60 | public function getIterator(): NodeCollectionIterator 61 | { 62 | return new NodeCollectionIterator($this); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to `sebastian/object-graph` are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. 4 | 5 | ## [4.0.0] - 202Y-MM-DD 6 | 7 | ### Removed 8 | 9 | * This component is no longer supported on PHP 8.1 and PHP 8.2 10 | 11 | ## [3.0.1] - 2024-07-03 12 | 13 | ### Changed 14 | 15 | * This project now uses PHPStan instead of Psalm for static analysis 16 | 17 | ## [3.0.0] - 2023-02-19 18 | 19 | ### Fixed 20 | 21 | * Fixed [#11](https://github.com/sebastianbergmann/object-graph/pull/11): Curly braces and pipe characters are not escaped correctly 22 | 23 | ### Removed 24 | 25 | * This component is no longer supported on PHP 7.4 and PHP 8.0 26 | 27 | ## [2.0.0] - 2021-02-24 28 | 29 | ### Removed 30 | 31 | * This component is no longer supported on PHP 7.1, PHP 7.2, and PHP 7.3 32 | 33 | ## [1.1.0] - 2017-11-27 34 | 35 | ### Changed 36 | 37 | * The `DotWriter` now emits information about array keys 38 | 39 | ## [1.0.1] - 2017-10-18 40 | 41 | ### Fixed 42 | 43 | * Fixed [#2](https://github.com/sebastianbergmann/object-graph/issues/2): `DotWriter` does not escape attribute values 44 | 45 | ## 1.0.0 - 2017-10-15 46 | 47 | * Initial release 48 | 49 | [4.0.0]: https://github.com/sebastianbergmann/object-graph/compare/3.0.1...main 50 | [3.0.1]: https://github.com/sebastianbergmann/object-graph/compare/3.0.0...3.0.1 51 | [3.0.0]: https://github.com/sebastianbergmann/object-graph/compare/2.0.0...3.0.0 52 | [2.0.0]: https://github.com/sebastianbergmann/object-graph/compare/1.1.0...2.0.0 53 | [1.1.0]: https://github.com/sebastianbergmann/object-graph/compare/1.0.1...1.1.0 54 | [1.0.1]: https://github.com/sebastianbergmann/object-graph/compare/1.0.0...1.0.1 55 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/functions.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\ObjectGraph; 11 | 12 | use const PATHINFO_EXTENSION; 13 | use function exec; 14 | use function pathinfo; 15 | use function sprintf; 16 | use function sys_get_temp_dir; 17 | use function tempnam; 18 | use function unlink; 19 | 20 | /** 21 | * @param array|object $objectGraph 22 | * 23 | * @throws InvalidArgumentException 24 | * @throws RuntimeException 25 | * 26 | * @codeCoverageIgnore 27 | * 28 | * @no-named-arguments Parameter names are not covered by the backward compatibility promise 29 | */ 30 | function object_graph_dump(string $filename, array|object $objectGraph): void 31 | { 32 | $format = pathinfo($filename, PATHINFO_EXTENSION); 33 | $nodes = (new Builder)->build($objectGraph); 34 | 35 | switch ($format) { 36 | case 'dot': 37 | $writer = new DotWriter; 38 | $writer->write($filename, $nodes); 39 | 40 | return; 41 | 42 | case 'pdf': 43 | case 'png': 44 | case 'svg': 45 | $tmpfile = tempnam(sys_get_temp_dir(), 'object_graph_dump'); 46 | 47 | $writer = new DotWriter; 48 | $writer->write($tmpfile, $nodes); 49 | 50 | exec( 51 | sprintf( 52 | 'dot -T%s -o %s %s', 53 | $format, 54 | $filename, 55 | $tmpfile, 56 | ), 57 | ); 58 | 59 | unlink($tmpfile); 60 | 61 | return; 62 | 63 | default: 64 | throw new InvalidArgumentException( 65 | sprintf( 66 | 'Unknown format "%s"', 67 | $format, 68 | ), 69 | ); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sebastian/object-graph 2 | 3 | [![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.3-8892BF.svg?style=flat-square)](https://php.net/) 4 | [![Latest Stable Version](https://img.shields.io/packagist/v/sebastian/object-graph.svg?style=flat-square)](https://packagist.org/packages/sebastian/object-graph) 5 | [![CI Status](https://github.com/sebastianbergmann/object-graph/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/object-graph/actions) 6 | [![Code Coverage](https://img.shields.io/codecov/c/github/sebastianbergmann/object-graph.svg?style=flat-square)](https://codecov.io/gh/sebastianbergmann/object-graph) 7 | 8 | Provides useful operations on PHP object graphs. 9 | 10 | ## Installation 11 | 12 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 13 | 14 | ``` 15 | composer require sebastian/object-graph 16 | ``` 17 | 18 | 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: 19 | 20 | ``` 21 | composer require --dev sebastian/object-graph 22 | ``` 23 | 24 | ## Usage 25 | 26 | ### Object Graph Visualization with GraphViz 27 | 28 | ```php 29 | add(new ShoppingCartItem('Foo', new Money(123, new Currency('EUR')), 1)); 34 | $cart->add(new ShoppingCartItem('Bar', new Money(456, new Currency('EUR')), 1)); 35 | 36 | object_graph_dump('graph.png', $cart); 37 | ``` 38 | 39 | ![Screenshot](example/example.svg) 40 | 41 | The `object_graph_dump()` function supports the [DOT Graph Description Language](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) (`.dot`), [Portable Document Format](https://en.wikipedia.org/wiki/Portable_Document_Format) (`.pdf`), [Portable Network Graphics](https://en.wikipedia.org/wiki/Portable_Network_Graphics) (`.png`), and [Scalable Vector Graphics](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) (`.svg`) output formats. 42 | 43 | The generation of PDF, PNG, and SVG files requires the [GraphViz](http://www.graphviz.org/) `dot` binary to be on the `$PATH`. 44 | -------------------------------------------------------------------------------- /src/node/Node.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\ObjectGraph; 11 | 12 | use function count; 13 | use RecursiveArrayIterator; 14 | use RecursiveIteratorIterator; 15 | 16 | /** 17 | * @internal This class is not covered by the backward compatibility promise 18 | */ 19 | final class Node 20 | { 21 | private int $id; 22 | private string $className; 23 | 24 | /** 25 | * @var array 26 | */ 27 | private array $properties; 28 | private ?NodeReferenceCollection $referencedNodes = null; 29 | 30 | /** 31 | * @param array $properties 32 | */ 33 | public function __construct(int $id, string $className, array $properties) 34 | { 35 | $this->id = $id; 36 | $this->className = $className; 37 | $this->properties = $properties; 38 | } 39 | 40 | /** 41 | * @return array 42 | */ 43 | public function properties(): array 44 | { 45 | return $this->properties; 46 | } 47 | 48 | public function className(): string 49 | { 50 | return $this->className; 51 | } 52 | 53 | public function id(): int 54 | { 55 | return $this->id; 56 | } 57 | 58 | public function referencedNodes(): NodeReferenceCollection 59 | { 60 | if ($this->referencedNodes !== null) { 61 | return $this->referencedNodes; 62 | } 63 | 64 | $referencedNodes = []; 65 | 66 | $iterator = new RecursiveIteratorIterator( 67 | new RecursiveArrayIterator( 68 | $this->properties, 69 | RecursiveArrayIterator::CHILD_ARRAYS_ONLY, 70 | ), 71 | ); 72 | 73 | foreach ($iterator as $element) { 74 | if ($element instanceof NodeReference) { 75 | $referencedNodes[] = $element; 76 | } 77 | } 78 | 79 | $this->referencedNodes = new NodeReferenceCollection(...$referencedNodes); 80 | 81 | return $this->referencedNodes; 82 | } 83 | 84 | public function referencesNodes(): bool 85 | { 86 | return count($this->referencedNodes()) > 0; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Builder.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\ObjectGraph; 11 | 12 | use function is_array; 13 | use function is_object; 14 | use SebastianBergmann\ObjectEnumerator\Enumerator; 15 | use SebastianBergmann\ObjectReflector\ObjectReflector; 16 | use SplObjectStorage; 17 | 18 | /** 19 | * @internal This class is not covered by the backward compatibility promise 20 | */ 21 | final class Builder 22 | { 23 | /** 24 | * @param array|object $objectGraph 25 | * 26 | * @throws RuntimeException 27 | */ 28 | public function build(array|object $objectGraph): NodeCollection 29 | { 30 | /** @var SplObjectStorage $map */ 31 | $map = new SplObjectStorage; 32 | $id = 1; 33 | $nodes = []; 34 | 35 | $objects = (new Enumerator)->enumerate($objectGraph); 36 | 37 | foreach ($objects as $object) { 38 | $map[$object] = $id++; 39 | } 40 | 41 | foreach ($objects as $object) { 42 | $properties = []; 43 | 44 | $reflectedProperties = (new ObjectReflector)->getProperties($object); 45 | 46 | foreach ($reflectedProperties as $name => $value) { 47 | if (is_array($value)) { 48 | $value = $this->processArray($value, $map); 49 | } elseif (is_object($value)) { 50 | $value = new NodeReference($map[$value]); 51 | } 52 | 53 | $properties[$name] = $value; 54 | } 55 | 56 | $nodes[] = new Node($map[$object], $object::class, $properties); 57 | } 58 | 59 | return new NodeCollection(...$nodes); 60 | } 61 | 62 | /** 63 | * @param array $array 64 | * @param SplObjectStorage $map 65 | * 66 | * @return array 67 | */ 68 | private function processArray(array $array, SplObjectStorage $map): array 69 | { 70 | $result = []; 71 | 72 | foreach ($array as $key => $value) { 73 | if (is_array($value)) { 74 | $value = $this->processArray($value, $map); 75 | } elseif (is_object($value)) { 76 | $value = new NodeReference($map[$value]); 77 | } 78 | 79 | $result[$key] = $value; 80 | } 81 | 82 | return $result; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/DotWriter.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\ObjectGraph; 11 | 12 | use const ENT_SUBSTITUTE; 13 | use const PHP_EOL; 14 | use function file_put_contents; 15 | use function htmlspecialchars; 16 | use function is_array; 17 | use function sprintf; 18 | use function str_replace; 19 | use function var_export; 20 | 21 | /** 22 | * @internal This class is not covered by the backward compatibility promise 23 | */ 24 | final class DotWriter 25 | { 26 | /** 27 | * @codeCoverageIgnore 28 | */ 29 | public function write(string $filename, NodeCollection $nodes): void 30 | { 31 | file_put_contents($filename, $this->render($nodes)); 32 | } 33 | 34 | public function render(NodeCollection $nodes): string 35 | { 36 | $buffer = <<<'EOT' 37 | digraph G { 38 | graph [fontsize=30 labelloc="t" label="" splines=true overlap=false rankdir = "LR"]; 39 | ratio = auto; 40 | 41 | 42 | EOT; 43 | 44 | foreach ($nodes as $node) { 45 | $properties = ''; 46 | 47 | foreach ($node->properties() as $name => $value) { 48 | if ($value instanceof NodeReference) { 49 | $value = '#' . $value->id(); 50 | } elseif (is_array($value)) { 51 | $value = $this->arrayToString($value); 52 | } else { 53 | $value = $this->valueToString($value); 54 | } 55 | 56 | $properties .= sprintf( 57 | '%s%s', 58 | $this->valueToString($name), 59 | $value, 60 | ); 61 | } 62 | 63 | $buffer .= sprintf( 64 | ' "object%d" [style="filled,bold", penwidth="%d", fillcolor="white", fontname="Courier New", shape="Mrecord", label=<%s
#%d%s
>];' . PHP_EOL, 65 | $node->id(), 66 | $node->id() === 1 ? 2 : 1, 67 | $node->id(), 68 | str_replace('\\', '\\\\', $node->className()), 69 | $properties, 70 | ); 71 | } 72 | 73 | $buffer .= PHP_EOL; 74 | 75 | foreach ($nodes as $node) { 76 | $processedReferencedNodes = []; 77 | 78 | foreach ($node->referencedNodes() as $referencedNode) { 79 | if (isset($processedReferencedNodes[$referencedNode->id()])) { 80 | continue; 81 | } 82 | 83 | $buffer .= sprintf( 84 | ' object%d -> object%d;' . PHP_EOL, 85 | $node->id(), 86 | $referencedNode->id(), 87 | ); 88 | 89 | $processedReferencedNodes[$referencedNode->id()] = true; 90 | } 91 | } 92 | 93 | return $buffer . '}' . PHP_EOL; 94 | } 95 | 96 | /** 97 | * @param array $array 98 | */ 99 | private function arrayToString(array $array): string 100 | { 101 | $buffer = ''; 102 | 103 | foreach ($array as $key => $value) { 104 | if ($value instanceof NodeReference) { 105 | $value = '#' . $value->id(); 106 | } elseif (is_array($value)) { 107 | $value = $this->arrayToString($value); 108 | } else { 109 | $value = $this->valueToString($value); 110 | } 111 | 112 | $buffer .= sprintf( 113 | '', 114 | $this->valueToString($key), 115 | $value, 116 | ); 117 | } 118 | 119 | return $buffer . '
[
%s => %s
]
'; 120 | } 121 | 122 | private function valueToString(mixed $value): string 123 | { 124 | return str_replace( 125 | [ 126 | '{', 127 | '}', 128 | '|', 129 | ], 130 | [ 131 | '{', 132 | '}', 133 | 'ǀ', 134 | ], 135 | htmlspecialchars( 136 | var_export($value, true), 137 | ENT_SUBSTITUTE, 138 | ), 139 | ); 140 | } 141 | } 142 | --------------------------------------------------------------------------------