Skip to content

Instantly share code, notes, and snippets.

@bigin
Last active August 24, 2016 13:54
Show Gist options
  • Save bigin/ae8d1ae5e5f73be4c8ff3f57f24db0e5 to your computer and use it in GitHub Desktop.
Save bigin/ae8d1ae5e5f73be4c8ff3f57f24db0e5 to your computer and use it in GitHub Desktop.
<?php
/**
* Class ItemSimplificator, just a simple decorator class
*/
class ItemSimplificator
{
protected $_item;
public function __construct(Item $item) {
$this->_item = $item;
$this->separate();
}
public function __call($method, $args) {
return call_user_func_array(array($this->_item, $method), $args);
}
public function __get($key) {
return $this->_item->$key;
}
public function __set($key, $val) {
return $this->_item->$key = $val;
}
protected function separate()
{
foreach($this->_item->fields as $field)
{
// Check field type
switch($field->type)
{
case 'fileupload':
$this->simplifyFileUpload($field);
continue;
case 'imageupload':
$this->simplifyImageUpload($field);
continue;
case 'text':
case 'longtext':
case 'slug':
case 'chunk':
case 'datepicker':
case 'money':
case 'hidden':
case 'editor':
case 'checkbox':
case 'dropdown':
$this->simplifyTextField($field);
continue;
/*case 'password':
continue;*/
}
}
}
protected function simplifyFileUpload($field)
{
$this->_item->{$field->name} = new StdClass();
foreach($field->file_name as $key => $name) {
$this->_item->{$field->name}->file_name[$key] = $name;
$this->_item->{$field->name}->path[$key] = $field->path[$key];
$this->_item->{$field->name}->fullpath[$key] = $field->fullpath[$key];
$this->_item->{$field->name}->url[$key] = $field->url[$key];
$this->_item->{$field->name}->fullurl[$key] = $field->fullurl[$key];
$this->_item->{$field->name}->title[$key] = $field->title[$key];
}
}
protected function simplifyImageUpload($field)
{
$this->_item->{$field->name} = new StdClass();
foreach($field->file_name as $key => $name) {
$this->_item->{$field->name}->imagename[$key] = $name;
$this->_item->{$field->name}->imagepath[$key] = $field->path[$key];
$this->_item->{$field->name}->imagefullpath[$key] = $field->fullpath[$key];
$this->_item->{$field->name}->imageurl[$key] = $field->url[$key];
$this->_item->{$field->name}->imagefullurl[$key] = $field->fullurl[$key];
$this->_item->{$field->name}->imagetitle[$key] = $field->title[$key];
}
}
protected function simplifyTextField($field)
{
$this->_item->{$field->name} = $field->value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment