Created
March 28, 2014 10:50
-
-
Save nWidart/9830032 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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