|
| 1 | +Usage |
| 2 | +===== |
| 3 | + |
| 4 | +Initialize the detector |
| 5 | +----------------------- |
| 6 | + |
| 7 | +Simple initiate the class. |
| 8 | + |
| 9 | +```php |
| 10 | +require __DIR__.'/vendor/autoload.php'; |
| 11 | + |
| 12 | +use Antalaron\Component\CircularReferenceDetect\CircularReferenceDetect; |
| 13 | + |
| 14 | +$detector = new CircularReferenceDetect(); |
| 15 | +``` |
| 16 | + |
| 17 | +Or if you prefer the singleton, use it. |
| 18 | + |
| 19 | +```php |
| 20 | +require __DIR__.'/vendor/autoload.php'; |
| 21 | + |
| 22 | +use Antalaron\Component\CircularReferenceDetect\CircularReferenceDetect; |
| 23 | + |
| 24 | +$detector = CircularReferenceDetect::newInstance(); |
| 25 | +``` |
| 26 | + |
| 27 | +Check if circular reference exists |
| 28 | +---------------------------------- |
| 29 | + |
| 30 | +The `hasCircularReference()` returns the circle found first, or false, if there |
| 31 | +is no circle. |
| 32 | + |
| 33 | +```php |
| 34 | +$a = [ |
| 35 | + 'a' => ['b'], |
| 36 | + 'b' => ['c'], |
| 37 | + 'c' => ['a'], |
| 38 | +]; |
| 39 | + |
| 40 | +$b = [ |
| 41 | + 'a' => ['b'], |
| 42 | + 'b' => ['c'], |
| 43 | +]; |
| 44 | + |
| 45 | +(bool) $detector->hasCircularReference($a); // true |
| 46 | +$detector->hasCircularReference($a); // ['a', 'b', 'c', 'a'] |
| 47 | + |
| 48 | +(bool) $detector->hasCircularReference($b); // false |
| 49 | +``` |
| 50 | + |
| 51 | +Arguments of `hasCircularReference()` |
| 52 | +------------------------------------- |
| 53 | + |
| 54 | +Basically the first argument is an array, with a key/value pair, where the value |
| 55 | +is an array, the elemnts of the value is referenced to the key. |
| 56 | + |
| 57 | +```php |
| 58 | +$a = [ |
| 59 | + 'a' => ['b', 'd', 'e'], |
| 60 | + 'b' => ['c', 'd'], |
| 61 | + 'c' => ['a'], |
| 62 | +]; |
| 63 | + |
| 64 | +$detector->hasCircularReference($a); |
| 65 | +```` |
| 66 | + |
| 67 | +You can also search for specific references as starting points, if you pass the |
| 68 | +previous array az the second argument, and to the first argument an array of |
| 69 | +starting points. It won't find a circle, because from the point of view of the |
| 70 | +array `$b`, the circle cannot be reached. |
| 71 | + |
| 72 | +```php |
| 73 | +$a = [ |
| 74 | + 'a' => ['b'], |
| 75 | + 'b' => ['c'], |
| 76 | + 'c' => ['a'], |
| 77 | + 'd' => ['e'], |
| 78 | + 'e' => ['f'], |
| 79 | +]; |
| 80 | +$b = [ |
| 81 | + 'd', |
| 82 | + 'e', |
| 83 | +]; |
| 84 | + |
| 85 | +$detector->hasCircularReference($b, $a); // false |
| 86 | +``` |
| 87 | + |
| 88 | +Options |
| 89 | +------- |
| 90 | + |
| 91 | +There is a limit in the search (50), but you can set is with the first argument |
| 92 | +of the constructor, or with the `setMaxDepth()` method. |
| 93 | + |
| 94 | +The greater circles will not be found. And by default, the return value will be |
| 95 | +false, unless shorter circle can be found. |
| 96 | + |
| 97 | +You can configure that in case of out of limit the class throws an exception, |
| 98 | +so you can check that even if circle weas not found, but there is a possibility |
| 99 | +to one exists. Pass `true` to the second argument to the constructor, or |
| 100 | +call `setThrowExceptionOnReachMaxDepth(true)`. |
0 commit comments