PHP code example of primd / fluidgraph

1. Go to this page and download the library: Download primd/fluidgraph library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

primd / fluidgraph example snippets


use Bolt\Bolt;
use Bolt\connection\StreamSocket;

$graph = new FluidGraph\Graph(
	[
		'scheme'      => 'basic',
		'principal'   => 'memgraph',
		'credentials' => 'password'
	],
	new Bolt(new StreamSocket())
);

use FluidGraph\Node;
use FluidGraph\Entity

class Person extends Node
{
	use Entity\Id\Uuid7;

	public function __construct(
		public ?string $firstName = NULL,
		public ?string $lastName = NULL,
	) {}
}

use FluidGraph\Edge;
use FluidGraph\Entity;

class FriendsWith extends Edge
{
	use Entity\DateCreated;
	use Entity\DateModified;

	public string $description;
}

use FluidGraph\Node;
use FluidGraph\Entity;
use FluidGraph\Matching;
use FluidGraph\Reference;

use FluidGraph\Relationship\Many;

class Person extends Node
{
	use Entity\Id\Uuid7;

	// ADDED:
	public protected(set) Many $friendships;

	public function __construct(
		public ?string $firstName = NULL,
		public ?string $lastName = NULL,
	) {
		// ADDED:
		$this->friendships = Many::having(
			$this,
			FriendsWith::class,
			Reference::to,
			Matching::any,
			[
				Person::class
			]
		);
	}
}

$matt = new Person(firstName: 'Matt');
$jill = new Person(firstName: 'Jill');

$matt->friendships->set($jill, [
	'description' => 'Best friends forever!'
]);

foreach ($matt->friendships->get(Person::class) as $person) {
	echo 'Hi' . $person->firstName . PHP_EOL;
}

$matt->friendships->unset($jill);

$graph->attach($matt)->save();

$graph->detach($jill)->save();

$matt = $graph->findNode(Person::class, [
	'firstName' => 'Matt'
]);

use FluidGraph\Order;
use FluidGraph\Direction;

$people = $graph->findNodes(Person::class, NULL, 0, [], [
	Order::by(Direction::asc, 'lastName')
]);

foreach ($notifications->findByPerson($person->identity()) as $notification) {
	// Do things with notifications
}

$entity_or_element->status()

use FluidGraph\Status;

$entity_or_element->status(Status::attached, ...)

$entity_or_element->is($entity_or_element_or_label);

$entity->is($entity);

$entity->is($element);

$element->is($element);

$entity->is(Person::class);

$element->is(Person::class);

if ($person->is(Author::class)) {
	// Do things knowing the person is an author
}

$entity_or_element->assign([
	// Will work on both
	'validProperty' => 10,

	// Only works on Elements
	'invalidProperty' => 10
])

$author = $person->as(Author::class, [
    'penName' => 'Hairy Poster'
]);

$author->is(Person::class); // TRUE
$person->is(Author::class); // TRUE

$author_people = $graph->findNodes([Person::class, Author::class]);

$entity_or_element->of(Author::class, Archivable::Archived)

$entity_or_element->ofAny(...$nodes)

foreach($author->writings->all() as $wrote) {
	// Do things with $wrote
}

if ($edge = $entity->relationship->any()) {
	// Do things with the $edge
}

foreach($person->friendships->for($person) as $friends_with) {
	// Working with an Edge to a specific friend
}

foreach($person->friendships->forAny(...$people) as $friends_with) {

}

$id = '01976f54-66b3-7744-a593-44259dce9651';

$person = $graph->findNode(Person::class, function($eq) use ($id) {
	return $eq('id', $id);
});

use FluidGraph\Where\Eq;

$person = $graph->findNode(Person::class, function(Eq $eq) use ($id) {
	return $eq('id', $id);
});

return $eq($upper($md5('id')), md5($id));

$person = $graph->findNode(Person::class, function($all, $eq) {
	return $any(
		$eq('email', '[email protected]'),
		$all(
			$eq('firstName', 'Matthew'),
			$eq('lastName', 'Sahagian')
		),
	);
})

use FluidGraph\Order;
use FluidGraph\Direction;

$people = $graph->findNodes(
	Person::class,
	10,
	10,
	function ($eq) {
		return $eq('firstName', 'Matthew');
	},
	[
		Order::by(Direction::asc, 'lastName')
	]
);

use FluidGraph\Order;
use FluidGraph\Direction;

$people = $graph->query
	->match(Person::class)
	->take(10)
	->skip(10)
	->where(
		function ($eq) {
			return $eq('firstName', 'Matt');
		}
	)
	->sort(
		Order::by(Direction::asc, 'lastName')
	])
	->results()
    ->as(Person::class)
;

$person->friendships->take(10)->load()

$person->friendships->take(10)->skip($offset)->load(Person::class)

$friends = $person->friendships->find(Person::class, 10);

$friends = $person->friendships->find(Person::class, 10);

foreach ($friends as $friend) {
	if ($friend->is(Archivable::archived)) {
		$friends->unset($friend);
	}
}

$graph->save();

$person->friendships->find(Person::class, 10);

foreach ($person->friendships->get(Person::class) as $person) {
	// Do things with friendly person
}

$friends = $person->friendships->find(Person::class, 10);

foreach ($friends as $friend) {
	if ($friend->is(Archivable::archived)) {
		$friends->unset($friend);
	}
}

// ADDED:
$friends->merge();

$graph->save();

$friends = $person->friendships
	->match(Person::class)
	->take(10)
	->skip(10)
	->where(
		function ($eq) {
			return $eq('firstName', 'Matt');
		}
	)
	->sort(
		Order::by(Direction::asc, 'lastName')
	])
	->get()