PHP code example of michaeljennings / refinery

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

    

michaeljennings / refinery example snippets


// Here we have a product we want to refine
class Product
{
    public $price = 10
    public $description = 'Something really cool!'
}

// So we create a product refinery where we set the product template
class ProductRefinery extends Refinery
{
    public function setTemplate($product)
    {
        return [
            'price' => number_format($product->price, 2),
            'description' => $product->description,
            'full_description' => $product->description . ' For only £' . number_format($product->price, 2),
        ];
    }
}

// Then we create the instances and refine the product
$product = new Product();
$refinery = new ProductRefinery();

var_dump($refinery->refine($product));
 
// The above will output:
// ['price' => '10.00', 'description' => 'Something really cool!', 'full_description' => 'Something really cool! For only £10.00']

use Michaeljennings\Refinery\Refinery;

class Foo extends Refinery 
{
  
}

class Foo extends Refinery 
{
  public function setTemplate($data)
  {
    // Refine data
  }
}

$foo = new Foo;

$foo->refine($data);

$foo = new Foo;

$data = $foo->refine(['foo', 'bar']);
$multiDimensionalData = $foo->refine([['foo'], ['bar']]);

class Product {
  public price = '10';
}

class ProductRefinery extends Refinery {
  public function setTemplate($product)
  {
    return [
      'price' => number_format($product->price, 2),
    ];
  }
}

$product = new Product();
$refinery = new ProductRefinery();

$refinedProduct = $refinery->refine($product); // ['price' => 10.00]


$product = [
    'name' => 'Awesome Product',
    'description' => 'This is an awesome product',
    'price' => 10.00,
    'category' => [
        'name' => 'Awesome Products',
    ],
    'variants' => [
        [
            'size' => 'Medium',
        ],
        [
            'size' => 'Large',
        ]
    ]
]

class ProductRefinery extends Refinery
{
    public function setTemplate($product)
    {
        return [
            'name' => $product['name'],
            'description' => $product['description'],
            'price' => '£' . number_format($product['price'], 2),
        ];
    }

    public function category()
    {
        return $this->attach(CategoryRefinery::class);
    }

    public function variants()
    {
        return $this->attach(CategoryRefinery::class);
    }
}

// Will just refine the product data.
$refinedProduct = $productRefinery->refine($product);

// Will refine the product, category, and the variants.
$refinedProductWithAttachments $productRefinery->bring('category', 'variants')->refine($product);

$refinedProductWithAttachments $productRefinery->bring('category')->refine($product);

$refined = $productRefinery->bring(['category' => ['meta']])->refine($product);

$country = [
    'name' => 'United Kingdom',
    'symbol' => '£',
];

$refined = $productRefinery->with(['country' => $country])->refine($product);

class ProductRefinery extends Refinery
{
    public function setTemplate($product)
    {
        $symbol = $this->country['symbol'];
        // OR
        $symbol = $this->attributes['country']['symbol'];

        return [
            'name' => $product['name'],
            'description' => $product['description'],
            'price' => $symbol . number_format($product['price'], 2),
        ];
    }
}

class ProductRefinery extends Refinery
{
    public function largeVariant()
    {
        return $this->attach(VariantRefinery::class, function($product) {
            return array_filter($product['variants'], function($variant) {
                return $variant['size'] == 'Large';
            });
        });
    }
}

class ProductRefinery extends Refinery
{
    public function sizes()
    {
        return $this->attach(function($product) {
            $sizes = [];

            foreach ($product['variants'] as $variant) {
                $sizes[] = $variant['size'];
            }

            return $sizes;
        });
    }
}