PHP code example of kamranahmedse / smasher

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

    

kamranahmedse / smasher example snippets


// Introduce the classes into your scope
use KamranAhmed\Smasher\Scanner;
use KamranAhmed\Smasher\JsonResponse;

// Instantiate the scanner class while passing the object
// of which type you need the response. Currently there is
// only JsonResponse that we have so..
$scanner = new Scanner(new JsonResponse());

// Scan the directory and return the JSON for the available content
$dirJson = $scanner->scan('/directory/to-scan');

// Or you can provide the path at which the json representation should
// be stored i.e.
// $scanner->scan('directory/to-scan', 'output/to-scan.json');


// Instantiate the scanner class while passing the object
// of which representation that you are going to use. We are going
// to convert JSON back to directory structure
$scanner = new Scanner(new JsonResponse());

// Specify the path where you need to populate the content in JSON
// Let's populate the directory structure in the JSON representation
// inside the output directory
$scanner->populate('output/', 'path/to/representation/to-use.json');

use KamranAhmed\Smasher\Scanner;
use KamranAhmed\Smasher\JsonResponse;

$scanner = new Scanner(new JsonResponse);
$scanner->scan('sample-path', 'output/sample-path.json');

class SuperResponse implements ResponseContract {

    /**
     * Formats the passed data for example a `JsonResponse` will encode to json, `XMLResponse`
     * will encode to xml etc
     * @param  array $data The data which is to be encoded
     * @return string
     */
    public function encode($data) {
        // Put your logic to convert a nested array to 
        // *super response format* here and return the resulting string
    }
    
    /**
     * Decodes the passed string and creates array from it
     * @param  string $response The existing response which is to be decoded to array
     * @return array
     */
    public function decode($response) {
        // Put your logic to convert the $response string to
        // to a nested array, like the one you encoded, and return
        // the array
    }
}


// Then pass it to scanner object
$scanner = new Scanner(new SuperResponse);

// Directory structure will be transformed to the format you need and 
`output/to-scan.super` file will be created
$scanner->scan('path/to-scan', 'output/to-scan.super');

// To create the directory structure from the response.
$scanner->populate('output/', 'to-scan.super');