PHP Classes

File: example/InputTransformer/run.php

Recommend this page to a friend!
  Classes of nvb   PHP Queue Jobs Piping   example/InputTransformer/run.php   Download  
File: example/InputTransformer/run.php
Role: Example script
Content type: text/plain
Description: Example
Class: PHP Queue Jobs Piping
Send jobs to a pipeline to be executed later
Author: By
Last change:
Date: 3 years ago
Size: 1,652 bytes
 

Contents

Class file image Download
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2014-11-08
 */

namespace Example\InputTransformer;

use
Net\Bazzline\Component\ProcessPipe\ExecutableException;
use
Net\Bazzline\Component\ProcessPipe\ExecutableInterface;
use
Net\Bazzline\Component\ProcessPipe\Pipe;
use
stdClass;

require_once
__DIR__ . '/../../../vendor/autoload.php';

/**
 * Class ObjectToArrayTransformer
 */
class ObjectToArrayTransformer implements ExecutableInterface
{
   
/**
     * @param mixed $input
     * @return mixed
     * @throws \Net\Bazzline\Component\ProcessPipe\ExecutableException
     */
   
public function execute($input = null)
    {
        if (!
is_object($input)) {
            throw new
ExecutableException('input must be instance of object');
        }

       
$array = array();

        foreach (
get_object_vars($input) as $property => $value) {
           
$array[$property] = $value;
        }

        return
$array;
    }
}

/**
 * Class ArrayToJSONTransformer
 * @package De\Leibelt\ProcessPipe\Example\WithDataTransformer
 */
class ArrayToJSONTransformer implements ExecutableInterface
{
   
/**
     * @param mixed $input
     * @return mixed
     * @throws ExecutableException
     */
   
public function execute($input = null)
    {
        if (!
is_array($input)) {
            throw new
ExecutableException('input must be an array');
        }

        return
json_encode($input);
    }
}

$pipe = new Pipe(
    new
ObjectToArrayTransformer(),
    new
ArrayToJSONTransformer()
);

$object = new stdClass();

$object->foo = 'bar';
$object->bar = 'foo';
$object->foobar = 'barfoo';

echo
$pipe->execute($object) . PHP_EOL;