• This repository has been archived on 19/Jul/2022
  • Stars
    star
    1
  • Language
    PHP
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A configurable PHP library for fetching recommendations from a GraphAware Recommendation setup

GraphAware-Reco client for PHP

The client is designed to work in turning recommendations and scores from a GraphAware Recommendation endpoint into objects that are easy to work with.

The current implementation uses Guzzle but can easily build your own to work with any HTTP client library.

Install

Use composer to get the package

composer require peterfox/graphaware-reco-client

Basic Setup

A basic example can use the common implementations of the interfaces set out but it's easy to create your own or extend the common ones to make it work with your own tweaks to the recommendations end point you've created.

use GraphAwareReco\Bridge\Guzzle\Client;
use GraphAwareReco\Bridge\Guzzle\RecommendationDescriptionBuilder;
use GraphAwareReco\Domain\Common\RecommendationFactory;
use GraphAwareReco\Domain\Common\RecommendationService;
use GraphAwareReco\Domain\Common\JsonResponseParser;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Command\Guzzle\GuzzleClient;

$service = new RecommendationService('http://localhost:7474/');

$description = RecommendationDescriptionBuilder::getDescriptionFromService($service);

$guzzle = new GuzzleClient(new Guzzle(), $description, ['defaults' => []]);

$client = new Client($guzzle, new RecommendationFactory(), new JsonResponseParser());

You can then just supply a parameter array to fetch a set of recommendations

$recommendations = $client->getRecommendations(['id' => 1, 'limit' => 30]);

Implement your own service

You can implement your own setup as needed, the common classes are mainly for examples and because they cover how most developers would set up a recommendation endpoint for their service.

###Create a recommendation service

The purpose of the RecommendationService interface is to simply state the uri for the endpoint and any arguments that you'll use for to request your recommendations.

use GraphAwareReco\Domain\Model\RecommendationService as RecommendationServiceInterface;

class RecommendationService implements RecommendationServiceInterface
{
    /**
     * @var string
     */
    private $baseUrl;

    /**
     * @param string $baseUrl
     */
    public function __construct($baseUrl)
    {
        $this->baseUrl = $baseUrl;
    }

    /**
     * @return string
     */
    public function getBaseUrl()
    {
        return $this->baseUrl;
    }

    /**
     * @return array
     */
    public function getUriParameters()
    {
        return ['id' => 'string'];
    }

    /**
     * @return array
     */
    public function getQueryParameters()
    {
        return ['limit' => 'string'];
    }

    /**
     * @return string
     */
    public function getRecommendationPath()
    {
        return '/graphaware/recommendation/{id}';
    }
}

###Create a response parser

Overall the response parser is simply used as a way of taking the json as an array and breaking it down into an array of recommendations. By default most setups will just be an array of recommendations already so nothing happens in the common example but if you need to change that you can.

use GraphAwareReco\Domain\Model\JsonResponseParser as ResponseParserInterface;

class JsonResponseParser implements ResponseParserInterface
{

    /**
     * @param array $result
     * @return array
     */
    public function parse(array $result)
    {
        return $result;
    }
}

###Create a recommendation model

Your models might differ from the common example as each recommendation might provide more details about the nodes it's recommending, as such you can implement your own.

use GraphAwareReco\Domain\Model\Recommendation as RecommendationInterface;
use GraphAwareReco\Domain\Model\Score;

class Recommendation implements RecommendationInterface
{
    /**
     * @var
     */
    private $identifier;
    /**
     * @var
     */
    private $uuid;
    /**
     * @var
     */
    private $score;

    public function __construct($identifier, $uuid, $score)
    {
        $this->identifier = $identifier;
        $this->uuid = $uuid;
        $this->score = $score;
    }

    /**
     * @return string
     */
    public function getUUID()
    {
        return $this->uuid;
    }

    /**
     * @return mixed
     */
    public function getItemIdentifier()
    {
        return $this->identifier;
    }

    /**
     * @return Score
     */
    public function getScore()
    {
        $this->score;
    }
}

###Create a recommendation factory

If you have made your own recommendation models you'll need to implement a factory for the client to initiate an instance of it using the date for each recommendation.

use GraphAwareReco\Domain\Common\Recommendation as RecommendationImpl;
use GraphAwareReco\Domain\Model\Recommendation;
use GraphAwareReco\Domain\Model\RecommendationFactory as RecommendationFactoryInterface;
use GraphAwareReco\Domain\Model\Score;

class RecommendationFactory implements RecommendationFactoryInterface
{
    /**
     * @param array $data
     * @return Recommendation
     */
    public function getRecommendation(array $data)
    {
        return new RecommendationImpl($data['id'], $data['uuid'], Score::fromArray($data['score']));
    }
}

Testing

There's some basic testing you can run for the library, namely PHPSpec and Behat. To run the behat tests you must also run the tutu server to emulate a GraphAware Reco setup.

###Run tutu

cd tutu && php -S localhost:7474 -t web -d date.timezone=UTC

###Run behat:

bin/behat

###Run phpspec:

bin/phpspec

More Repositories

1

laravel-webhook-demo

The example code for the article https://medium.com/@SlyFireFox/laravel-innovations-making-your-own-webhook-mechanism-through-notifications-96e75e99a2b1
PHP
17
star
2

laravel-elixir-mjml

A task plugin for running MJML templates in Laravel Elixir
JavaScript
11
star
3

hexavel

A modified version of the Laravel Framework
PHP
7
star
4

openai-laravel-demo

A demo app for using OpenAI with Laravel
PHP
7
star
5

macros-demo

A Demo for Macros
PHP
7
star
6

nova-social-login-demo

A demo for how to implement Social Logins for Laravel Nova
PHP
5
star
7

laravel-runscope

A PHP library for making it easy to use Runscope with your web hooks and external API calls
PHP
5
star
8

notifications-demo

The code in this project is an example of how to create a simple streamlined email unsubscribe mechanism for Laravel's notification system
PHP
5
star
9

hieroglyph

A package to simplify changing between different icon sets
PHP
3
star
10

roadrunner-plugin-template

Template project for a RoadRunner plugin
Go
3
star
11

bitpayclient

An OOP PHP client for interacting with the BitPay API
PHP
2
star
12

validation-rule-demo

The example code for the article https://medium.com/@SlyFireFox/test-driven-development-for-custom-laravel-validation-rules-669d01e34a65
PHP
2
star
13

seo-demo

The code in this project is an example of how to create root level URLs for dynamic content without causing wildcard clashes
PHP
2
star
14

make-command-demo

A demo for making new Make commands in Laravel
PHP
2
star
15

laravel-incident-logs-demo

The example code for the article https://medium.com/@SlyFireFox/laravel-how-to-make-incident-logs-d7fa88e48490
PHP
2
star
16

hexavel-spark

A library for making installs of Spark compatible with Laravel Spark
PHP
1
star
17

torino

An example of a simple Onion address generator
JavaScript
1
star
18

waitformysql

A mini golang util for waiting for a mysql database to be up
Go
1
star
19

laravel-casts-examples

A demo for Laravel Custom Casts with common examples for Money, Location/Address and Date Intervals
PHP
1
star
20

testing-trait-hooks-demo

Demo for Trait hooks to use in Test Cases
PHP
1
star
21

custom-make-model-command

A demo for customising the Laravel make model command to use singular table names.
PHP
1
star