PHP code example of iconic / engine

1. Go to this page and download the library: Download iconic/engine 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/ */

    

iconic / engine example snippets


$engine = new \Iconic\Engine\Engine();
$engine->allow('view');
$allowed = $engine->can('view'); //returns true
$allowed = $engine->can('edit'); //returns false

$post = new Post();
$post->status = "submitted";
$engine = new \Iconic\Engine\Engine();

$engine->allow('publish')->of('status', 'draft', 'published');

$allowed = $engine->can('publish', $post); //returns false
$engine->apply('publish', $post); //throws Exception

$post->status = "draft";
$allowed = $engine->can('publish', $post); //returns true
$engine->apply('publish', $post); //changes post status to "published"

$user = new User();
$user->role = "user";

$engine = new \Iconic\Engine\Engine();
$engine->allow('edit')->if('role', 'editor');
$allowed = $engine->can('edit', null, $user); //returns false

$editor = new User();
$editor->role = 'editor';
$user = new User();
$user->role = 'user';
$draftPost = new Post();
$draftPost->status = "draft";
$deletedPost = new Post();
$deletedPost->status = "deleted";

$engine = new \Iconic\Engine\Engine();
$engine->allow('publish')->of('status', 'draft', 'published');
$engine->allow('publish')->if('role', 'editor');

$engine->can('publish', $draftPost, $editor); //returns true
$engine->apply('publish', $draftPost, $editor); //changes post status to "published"

$engine->can('publish', $deletedPost, $editor); //returns false
$engine->apply('publish', $deletedPost, $editor); //throws exception
$engine->can('publish', $draftPost, $user); //returns false  
$engine->apply('publish', $draftPost, $user); //throws exception