0

I have a code that creates an Object. Let's say we can create an Object A and this object has R1 or R2 directions or permissions to the next Object let's name it Object B, so in order to have access from Object A to object B R1 or R2 has to be 1(or TRUE)

How to implement a function that will check if Object A can access Object D. Where in order for Object A it needs to have at least R1 or R2 access to Object Band where Object B needs to have R1 or R2 access to Object C where Object D needs to have at least R1 or R2 access to Object D.

<?php

class MyObject {
    function directionsRn($R1, $R2, $objSource, $objDest) {
        if ($R1 == 1) {
            echo("$objSource can access $objDest via R1 <br>");
        } else if ($R1 == 0) {
            echo("SORRY, but $objSource CANNOT access $objDest via R1 <br>");
        }  

        if ($R2 == 1) {
            echo("$objSource can access $objDest via R2<br>");
        }  else if ($R2 == 0) {
            echo("SORRY, but $objSource CANNOT access $objDest via R2 <br>");
        }
    }
}

$myObject_array[0] = new MyObject();
$myObject_array[1] = new MyObject();
$myObject_array[2] = new MyObject();

$myObject_array[0]->directionsRn(0, 1, 'A', 'B');
$myObject_array[1]->directionsRn(1, 0, 'D', 'B');
$myObject_array[2]->directionsRn(1, 0, 'C', 'D');

print_r($myObject_array);
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • 1
    This question is a little unclear - are "R"s a route or something? What is the expected output of your example code? – scrowler Jul 19 '16 at 00:19
  • "R"s are the names of access directions it can be anything it's just a name for an access direction. In this case there may be only two access directions for example A to B A has R1 and R2 access directions (literally arrows from A to B) where 0(zero) means no access via R1 for example and if R2 == 1 it means A has access to B via R2 – ThePickyGuy Jul 19 '16 at 00:24

1 Answers1

1

What you have is a graph where your objects are nodes (A,B, etc) and the permissions between the objects are edges.

Connection between any two nodes can be resolved with bread first search, in which you try to access to new nodes from the set of nodes you have already access by a distance of one edge. Important point is to ignore already accessed elements by flagging them as already-seen (with an array of boolean)

You can also find different approaches for checking connection of two nodes here

Community
  • 1
  • 1
emrhzc
  • 1,347
  • 11
  • 19
  • this is not quiet exactly what i am looking for the graph represented in those articles have one direction in my case it can be from A to B and from D to B then from D to C and from C to D and that's it. How to check and confirm that A is not having access to C ? – ThePickyGuy Jul 19 '16 at 00:53
  • As a general approach to apply one directional graph solutions on bidirectionals, you just process that if an edge from A to B, there's also an edge from B to A. – emrhzc Jul 19 '16 at 01:04
  • no, that's why i called them directions let's say A can have R1 and R2 both directed to B but Object B only has one direction to Object D – ThePickyGuy Jul 19 '16 at 01:13