Skip to content

Instantly share code, notes, and snippets.

@afridley
Created January 17, 2019 18:08
Show Gist options
  • Save afridley/dec347fc024d4c369d9afad0dfaa5918 to your computer and use it in GitHub Desktop.
Save afridley/dec347fc024d4c369d9afad0dfaa5918 to your computer and use it in GitHub Desktop.
neo element api example
<?php
require craft()->path->getPluginsPath().'elementapi/vendor/autoload.php';
require craft()->path->getAppPath().'../../vendor/autoload.php';
use League\Fractal\TransformerAbstract;
use Cocur\Chain\Chain;
class ProviderApiTransformer extends TransformerAbstract
{
private static function createBlockTypeFilter($type) {
return function(Neo_BlockModel $block) use ($type) {
return $block->type->handle == $type;
};
}
protected $imageConfig = [
'mode' => 'fit',
'width' => 200,
'height' => 80,
'quality' => 75,
'position' => 'center-center'
];
public function transform(EntryModel $entry)
{
$neoProducts = $entry->products->find();
$results = [
'id' => $entry->id,
'slug' => $entry->slug,
'title' => $entry->title,
'imageUrl' => $entry->image->first() ? $entry->image->first()->getUrl($this->imageConfig) : null,
'products' => Chain::create($neoProducts)
->filter(self::createBlockTypeFilter('product'))
->map(function(Neo_BlockModel $product) use ($neoProducts) {
return [
'id' => $product->id,
'type' => $product->productType,
'note' => $product->note,
'handle' => $product->type->handle,
'level' => $product->level,
'destinations' => Chain::create($neoProducts)
->filter(self::createBlockTypeFilter('destination'))
->map(function(Neo_BlockModel $dest) use ($product) {
return [
'id' => $dest->id,
'transferSpeed' => $dest->transferSpeed,
'transferAmounts' => Chain::create($dest->transferAmounts->limit(null)->find())
->map(function(MatrixBlockModel $block) {
return [
'id' => $block->id,
'icuAmount' => $block->icuAmount,
'icuRate' => $block->icuRate,
'icuFee' => $block->icuFee,
'targetCurrency' => $block->targetCurrency
];
})->array,
'countries' => Chain::create($dest->destinationCountries->find())
->map(function(CategoryModel $country) {
return $country->countryIsoCode;
})->array
];
})->array
];
})->array
];
return $results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment