openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

ModelBase.class.php (1644B)


      1 <?php
      2 namespace cms\model;
      3 
      4 use LogicException;
      5 use util\text\TextMessage;
      6 
      7 abstract class ModelBase
      8 {
      9     public function __construct()
     10     {
     11     }
     12 
     13     /**
     14      * All public properties of this object.
     15      * @return array
     16      */
     17     public function getProperties()
     18     {
     19         return get_object_vars( $this );
     20     }
     21 
     22 	/**
     23 	 * The logical name of this object.
     24 	 *
     25 	 * @return string name
     26 	 */
     27     public abstract function getName();
     28 
     29 	/**
     30 	 * Loading the instance from the database
     31 	 */
     32     public abstract function load();
     33 
     34 	/**
     35 	 * Saving the instance to the database
     36 	 */
     37     protected abstract function save();
     38 
     39 	/**
     40 	 * Adding the instance to the database
     41 	 */
     42     protected abstract function add();
     43 
     44 	/**
     45 	 * Delete this instance
     46 	 */
     47     public abstract function delete();
     48 
     49 	/**
     50 	 * Returns the unique ID of this object.
     51 	 *
     52 	 * @return int
     53 	 */
     54     public abstract function getId();
     55 
     56 
     57 	/**
     58 	 * Is this instance already persistent in the database?
     59 	 *
     60 	 * @return bool
     61 	 */
     62 	public function isPersistent()
     63 	{
     64 		return (bool) $this->getId();
     65 	}
     66 
     67 
     68 	/**
     69 	 * Persist the object in the database
     70 	 */
     71 	public function persist()
     72 	{
     73 		if   ( ! $this->isPersistent() )
     74 			$this->add();
     75 
     76 		$this->save();
     77 	}
     78 
     79 	/**
     80 	 * Updates the already existing object in the database
     81 	 *
     82 	 * @throws LogicException if not persistent
     83 	 */
     84 	public function update()
     85 	{
     86 		if   ( ! $this->isPersistent() )
     87 			throw new LogicException(TextMessage::create('Object ${0} is not persistent and cannot be updated', [ get_class($this).' '.$this->getName() ]) );
     88 
     89 		$this->save();
     90 	}
     91 
     92 
     93 	public function __toString() {
     94 		return $this->getId().':'.$this->getName();
     95 	}
     96 }