Giter Club home page Giter Club logo

phptree's Introduction

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage Mutation testing badge License Donate!

PhpTree

Description

A PHP implementation of tree data structure.

It provides different trees implementations:

  • Node: The base class.
  • N-ary node: (or K-ary tree) extends the base class and allows you to specify the capacity of a node, the maximum children a node can have.
  • Value node: extends the N-ary node and allows you to attach a value to the node.
  • KeyValue node: extends the Value node and allows you to attach a key and a value to the node.
  • Trie node: extends the KeyValue node, a simple Trie tree.
  • Auto-balanced node: extends the N-ary node and tries to keep the tree as symetric as possible. It automatically balance all the children as soon as they are added.
  • Merkle node: a tree in which every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes.

4 trees traversal algorithms:

  • In order
  • Post order
  • Pre order
  • Breadth first

Exporters and importers:

  • Ascii: Export a tree into an ascii graphic, just for swag and visualisation fun.
  • Graph: Export a tree into a Graph using the graphp/graphp library.
  • GraphViz: Export a tree into a script in GraphViz format.
  • Text: Export a tree into a simple string, easy for storing in a database.
  • nikic/php-ast: Import a tree from an AST generated by the PHP extension AST.
  • nikic/php-parser: Import a tree from an AST generated by nikic/php-parser.
  • microsoft/tolerant-php-parser: Import a tree from an AST generated by microsoft/tolerant-php-parser.

Modifier:

  • Reverse: To reverse a tree, all the children are mirrored.

Documentation

Blog post: https://not-a-number.io/2018/phptree-a-fast-tree-implementation

Changelog

See CHANGELOG.MD

Requirements

  • PHP >= 7.1

Installation

composer require loophp/phptree

Optional packages

  • drupol/launcher: To automatically open a resource using the proper application on your operating system.
  • graphp/graphp: To export a tree into a Graph.

Usage

<?php

declare(strict_types = 1);

use loophp\phptree\Exporter\Gv;
use loophp\phptree\Exporter\Image;
use loophp\phptree\Node\ValueNode;
use loophp\phptree\Exporter\Text;
use loophp\launcher\Launcher;

include './vendor/autoload.php';

// Create the root node.
$tree = new ValueNode('root', 2);

$nodes = [];
foreach (\range('A', 'Z') as $v) {
    $nodes[] = new ValueNode($v, 2);
}

// Add children to the root node.
$tree->add(...$nodes);

// Export to text.
$textExporter = new Text();
$textExporter->export($tree); // [root[A[C[G[O][P]][H[Q][R]]][D[I[S][T]][J[U][V]]]][B[E[K[W][X]][L[Y][Z]]][F[M][N]]]]โŽ

// Export to a GraphViz script with the Gv exporter
$graphvizExporter = new Gv();
$dotScript = $graphvizExporter->export($tree);
file_put_contents('tree.dot', $dotScript);
// Then do "dot -Tsvg tree.dot -o tree.svg" to export in SVG.

// Or use the Image exporter that does it for you.
$imageExporter = new Image();
$imageContent = $imageExporter->setFormat('png')->export($tree);
file_put_contents('tree.png', $imageContent);

// If you want to launch the image, you can use an optional package.
// do: composer require drupol/launcher
// then:
Launcher::open('tree.png');

Code quality, tests and benchmarks

Every time changes are introduced into the library, Github run the tests and the benchmarks.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit some inspections are executed with GrumPHP, run ./vendor/bin/grumphp run to check manually.

PHPBench is used to benchmark the library, to run the benchmarks: composer bench

PHPInfection is used to ensure that your code is properly tested, run composer infection to test your code.

Contributing

Feel free to contribute by sending pull requests. We are a usually very responsive team and we will help you going through your pull request from the beginning to the end.

For some reasons, if you can't contribute to the code and willing to help, sponsoring is a good, sound and safe way to show us some gratitude for the hours we invested in this package.

Sponsor me on Github and/or any of the contributors.

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.

phptree's People

Contributors

alanpoulain avatar bytestream avatar drupol avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phptree's Issues

Return value of image exporter

Steps required to reproduce the problem

  1. Create a testnode
  2. add another one to it as a child for realism purposes
  3. try to export it to a PNG
  4. die with a "it works??" response

Expected Result

  • String with the content: "it works??"

Actual Result

  • Return value of loophp\phptree\Exporter\Image::export() must be of the type string, null returned

                $testnode = new ValueNode("test");
                $testnode2 = new ValueNode("test2");

                $testnode->add($testnode2);
//                dd($testnode);

                $imageExporter = new Image();
                $imageContent = $imageExporter->setFormat('png')->export($testnode);
                die("itworks???");

NodeInterface::clone() should not use unserialize(serialize()).

The current NodeInterface::clone() method uses unserialize() and serialize().

This is not the best solution to clone an object, ideally we should have used __clone().

I've tests many solutions and the only one that works is with unserialize(serialize()).

You can easily test your solution using this code:

<?php

declare(strict_types = 1);

use Graphp\GraphViz\GraphViz;

include './vendor/autoload.php';

$exporterGraph = new \drupol\phptree\Exporter\Graph();

$root = new \drupol\phptree\Node\ValueNode('root', 2);
$root[] = new \drupol\phptree\Node\ValueNode('foo');

$clone = $root->clone();

echo 'Root count: ' . iterator_count($root->all()) . "\n";
echo 'Second clone count: ' . iterator_count($clone->all()) . "\n";

var_dump($root === $root[0]->getParent());
var_dump(sha1(spl_object_hash($root)) . ' === ' . sha1(spl_object_hash($root[0]->getParent())));
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($root));

var_dump($clone === $clone[0]->getParent());
var_dump(sha1(spl_object_hash($clone)) . ' === ' .sha1(spl_object_hash($clone[0]->getParent())));
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($clone));

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

composer
composer.json
  • php >= 8.1
  • drupol/launcher ^2.2
  • drupol/php-conventions ^5
  • drupol/phpmerkle ^2.2
  • friends-of-phpspec/phpspec-code-coverage ^6
  • graphp/graphviz ^0.2
  • infection/infection ^0.29
  • infection/phpspec-adapter ^0.2
  • loophp/launcher ^2.2.2
  • microsoft/tolerant-php-parser ^0.1
  • nikic/php-parser ^4
  • phpspec/phpspec ^7
github-actions
.github/workflows/continuous-integration.yml
  • actions/checkout v4
  • WyriHaximus/github-action-composer-php-versions-in-range v1
  • actions/checkout v4
  • kamiazya/setup-graphviz v1
  • shivammathur/setup-php v2
  • ramsey/composer-install v2
.github/workflows/prune.yaml
  • actions/stale v7.0.0
.github/workflows/release.yaml
  • actions/checkout v4.1.1
  • mindsers/changelog-reader-action v2
  • actions/create-release v1.1.4

  • Check this box to trigger a request for Renovate to run again on this repository

Deprecation warnings from php 8.1

When using the package with PHP 8.1, we get the following deprecation errors

Return type of loophp\phptree\Node\Node::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/platform/platform/vendor/loophp/phptree/src/Node/Node.php on line 207

This package requires php ^7.1.3 <7.3.14 but your PHP version (7.3.14) does not satisfy that requirement.

Please allow more recent PHP versions to use PHPtree.

Steps required to reproduce the problem

  1. run composer require loophp/phptree

Expected Result

  • phptree installs

Actual Result

  • version error
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "4.2.*"
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - This package requires php ^7.1.3 <7.3.14 but your PHP version (7.3.14) does not satisfy that requirement. ```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.