PHP code example of pbmengine / pbm-stream-sdk

1. Go to this page and download the library: Download pbmengine/pbm-stream-sdk 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/ */

    

pbmengine / pbm-stream-sdk example snippets


# in ./config/pbm-stream.php
return [
    'url' => env('PBM_STREAM_URL', ''),
    'project' => env('PBM_STREAM_PROJECT', ''),
    'access_key' => env('PBM_STREAM_ACCESS_KEY', '')
];

# via helper
stream('logins')->record(['user' => 1, 'age' => 30]);

# via Facade
\Pbmengine\Stream\Facades\Stream::collection('logins')->record(['user' => 1, 'age' => 30]);

# record event
stream('logins')->record(['user' => 1, 'age' => 30]);

# update event
stream('logins')->updateEvent('<event id>', ['age' => 30]);

# update several events with condition
# field is the condition field with value
# e.g. field is userId and value is 300
# stream('logins')->updateEvents('userId', 300, ['age' => 30]);
# means: update all events where userId is 300 and set age to 30
stream('logins')->updateEvents('field', 'value', ['age' => 30]);

# update events where you have several = conditions
stream('logins')->updateEventsWithConditions([
    'event' => 'items.purchased',
    'customerId' => 5
], [
    'age' => 30,
    'itemsPurchased' => true
]);

# delete event
stream('logins')->deleteEvent('<event id>');

# get project information
stream()->project();

# get project collections
stream()->collections();

# get collection informations
stream('logins')->collection();

# validate collection event
stream('logins')->validateEvent(['user' => 2, 'age' => 10]);

# test event to check if it's a valid event for any collection
# you do not need any collection for this request
stream()->testEvent(['user' => 2, 'age' => 10]);

# create collection index
# timestamp and _id are automatically indexed
stream('logins')->createIndex('<field>');

# drop collection index
stream('logins')->dropIndex('<field>');

# query options
$response = stream('logins')
    ->query()
    ->select(['_id', 'event', 'itemPrice'])
    ->where('a', '=', 2)
    ->whereIn('<column>', ['array', '...']) 
    ->orWhere('b', '>', 6)
    ->timeFrame('<start date iso 8601>', '<end date iso 8601>')
    ->timeFrame(TimeFrame::THIS_DAYS, 5)
    ->timeFrame(TimeFrame::PREVIOUS_DAYS, 6)
    ->groupBy('<column>') // (['field a', 'field b'])
    ->orderBy('<column>', 'asc') // second parameter is optional, default is asc 
    ->orderByDesc('<column>') // order desc
    ->count(); // sum(field) | avg(field) | max(field) | min(field) | countUnique(field) | selectUnique(field)

// get events
$response = stream('pages')
    ->query()
    ->where('event', '=', 'page.viewed')
    ->orWhere('event', '=', 'login.viewed')
    ->take(10)
    ->orderByDesc('timestamp')
    ->get();

// get events with pagination
$response = stream('pages')
    ->query()
    ->where('event', '=', 'page.viewed')
    ->orderByDesc('timestamp')
    ->paginate(10, 1); // per page 10 events on page 1

# complex queries
# for more complex queries use the aggregate function
stream('pages')
    ->query()
    ->aggregate([
        ['$match' => ['event' => 'pageViewed']]
    ]);


# get() method
$response = stream('pages')->take(1)->get()->json();

# paginate() method
$response = stream('pages')->paginate(1)->json();

# count(), max(<column>), min(<column>), sum(<column>), avg(<column>) method
$response = stream('pages')->avg('customerAge')->json();

# testing events 
$response = stream()->testEvent(['event' => 'test', '2983' => 12, 'test' => null])->json();

# validate events for collection 
$response = stream('purchases')->validateEvent(['event' => 'test'])->json();
`shell
php artisan vendor:publish