Skip to content

Instantly share code, notes, and snippets.

@avwilks
Created May 26, 2021 00:48
Show Gist options
  • Save avwilks/0a2c971a1931a3f80fa95b9f5358ea96 to your computer and use it in GitHub Desktop.
Save avwilks/0a2c971a1931a3f80fa95b9f5358ea96 to your computer and use it in GitHub Desktop.
Conditional Fields Graph Implementation
<?php
class Node
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
/**
* Should show is the edge between different nodes and answers with an anonymous function
* should the parent show the field passed in. The functions returns the field to be showed
* if the function rules is true. This would get attached to an event listener at runtime
* and be check when the event is fired.
*
* @param Node $field
* @param mixed $rules | An anonymous function that returns true if the conditions are true
*
* @return Node|false
*/
public function shouldShow(Node $field, $rules)
{
if ($rules()) {
return $field;
}
return false;
}
}
// We can have hard coded operators. This is a
function contains($fieldContents, $target)
{
if (strpos($fieldContents, $target)) {
return true;
} else {
return false;
}
}
$notContains = function ($fieldContents, $target) {
return !contains($fieldContents, $target);
};
$A = new Node(1234);
$B = new Node(5678);
echo $A->shouldShow($B, contains('USA is my home country', 'USA'));
echo $A->shouldShow($B, $notContains('USA is my home', 'USA'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment