Skip to content

Instantly share code, notes, and snippets.

@nWidart
Created March 28, 2014 10:50
Show Gist options
  • Select an option

  • Save nWidart/9830032 to your computer and use it in GitHub Desktop.

Select an option

Save nWidart/9830032 to your computer and use it in GitHub Desktop.
<?php
class GalleryCreator
{
/**
* @var string
*/
protected $sType;
/**
* @var State model
*/
protected $oModel;
/**
* @var int
*/
protected $iProp;
/**
* Set the type on the class
* Also make the property
*
* @param $sType
* @return $this
*/
public function setType($sType)
{
$this->sType = $sType;
$this->iProp = "i{$this->sType}";
return $this;
}
/**
* Set the model on the class
*
* @param $oModel
* @throws InvalidTypeException
* @return $this
*/
public function setClass($oModel)
{
$this->oModel = $oModel;
if (is_null($this->oModel->{$this->iProp})) throw new InvalidTypeException('Type not set on the model');
return $this;
}
/**
* Create the gallery if necessary
* Set on the model the galleryId
*/
public function create()
{
// 1. Check if model already has a gallery of that type
$oGallery = Query::first('GalleryGallery', array(array('iId', '=', $this->oModel->{$this->iProp})));
// 1b. If not make it
if (empty($oGallery)) $oGallery = new GalleryGallery();
// Setting properties
$oGallery->sNameLngFr = "{$this->sType}_" . get_class($this->oModel) . "_{$this->oModel->sNameLngFr}";
$oGallery->iValidate = State::PROD;
$oGallery->save();
// Set the galleryId on the model
$this->oModel->{$this->iProp} = $oGallery->iId;
$this->oModel->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment