| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /****************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016 Richard Klees <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This software is licensed under The MIT License. You should have received  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * a copy of the license along with the code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Lechimp\Dicto\Graph; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Lechimp\Dicto\Graph\Predicate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * The complete graph. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class Graph { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var array<string, array<int, Node>> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $nodes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $id_counter = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Create a new node in the graph. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param   string                      $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param   array<string,mixed>|null    $properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return  Node | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 75 |  |     public function create_node($type, array $properties = null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 75 |  |         $node = $this->build_node($this->id_counter, $type, $properties); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 75 |  |         if (!array_key_exists($type, $this->nodes)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 75 |  |             $this->nodes[$type] = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 75 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 75 |  |         $this->nodes[$type][$this->id_counter] = $node; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 75 |  |         $this->id_counter++; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 75 |  |         return $node; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 75 |  |     protected function build_node($id, $type, array $properties = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 75 |  |         return new Node($id, $type, $properties); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Add a relation to the graph. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param   Node                $left | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param   string              $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param   array<string,mixed> $properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param   Node                $right | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return  Relation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 48 |  |     public function add_relation(Node $left, $type, array $properties, Node $right) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 48 |  |         return $left->add_relation($type, $properties, $right); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Get nodes from the graph, maybe filtered by a filter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param   Predicate|null    $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return  Iterator<Node> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 59 |  |     public function nodes(Predicate $filter = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 59 |  |         if ($filter !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 50 |  |             $types = $filter->for_types(array_keys($this->nodes)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 50 |  |             $filter = $filter->compile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 50 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 9 |  |             $types = array_keys($this->nodes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 59 |  |         foreach ($this->nodes as $type => $nodes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 58 |  |             if (!in_array($type, $types)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 28 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 58 |  |             foreach ($nodes as $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 58 |  |                 if ($filter === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 8 |  |                     yield $node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 8 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 50 |  |                     if ($filter($node)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 48 |  |                         yield $node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 48 |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 58 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 59 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 59 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Get the node with the given id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param   int     $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @throws  \InvalidArgumentException   if $id is unknown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return  Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |     public function node($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |         assert('is_int($id)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |         foreach ($this->nodes as $nodes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 2 |  |             if (array_key_exists($id, $nodes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |                 return $nodes[$id]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         throw new \InvalidArgumentException("Unknown node id '$id'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Build a query on the graph. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return  Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 56 |  |     public function query() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 56 |  |         return new QueryImpl($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  | 
            
                        
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.