MediaSearch_Model
[ class tree: MediaSearch_Model ] [ index: MediaSearch_Model ] [ all elements ]

Source for file Abstract.php

Documentation is available at Abstract.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     MediaSearch_Model
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * This model class represents a single file with its metadata.
  23.  *
  24.  * @see Zend_Application_Bootstrap_Bootstrap
  25.  *
  26.  * @abstract
  27.  * @category   MediaSearch
  28.  * @package    MediaSearch_Model
  29.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  30.  */
  31. abstract class MediaSearch_Model_Abstract {
  32.     /**
  33.      * Database mapper class.
  34.      *
  35.      * @access private
  36.      * @var MediaSearch_Model_FileMetaMapper 
  37.      */
  38.     private $mapper;
  39.     /**
  40.      * Unique id.
  41.      *
  42.      * @access private
  43.      * @var int 
  44.      */
  45.     private $id;
  46.     
  47.     /**
  48.      * Can take a associatove array with initial values.
  49.      * @param array $options 
  50.      */
  51.     public function __construct(array $options null{
  52.         if (null !== $options{
  53.             $this->setOptions($options);
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * Returns the unique id.
  59.      *
  60.      * @access public
  61.      * @return int 
  62.      */
  63.     public function getId({
  64.         return $this->id;
  65.     }
  66.  
  67.     /**
  68.      * Sets the unique id. This id need to be unique!
  69.      *
  70.      * @access public
  71.      * @param int $id 
  72.      * @return MediaSearch_Model_FileMeta 
  73.      */
  74.     public function setId($id{
  75.         $this->id = (int) $id;
  76.  
  77.         return $this;
  78.     }
  79.     
  80.     /**
  81.      * Set interceptor to access model propertoes.
  82.      *
  83.      * @todo remove this methods we use explicit setters
  84.      *
  85.      * @access public
  86.      * @throws Exception On invalid name.
  87.      * @param string $name 
  88.      * @param mixed $value 
  89.      */
  90.     public function __set($name$value{
  91.         $method 'set' $name;
  92.  
  93.         if (('mapper' === $name|| !$this->hasMethod($method)) {
  94.             throw new Exception('Invalid filemeta property');
  95.         }
  96.  
  97.         $this->$method($value);
  98.     }
  99.  
  100.     /**
  101.      * Set interceptor to access model propertoes.
  102.      *
  103.      * @todo remove this methods we use explicit setters
  104.      * 
  105.      * @access public
  106.      * @throws Exception On invalid name.
  107.      * @param string $name 
  108.      * @param mixed $value 
  109.      */
  110.     public function __get($name{
  111.         $method 'get' $name;
  112.  
  113.         if (('mapper' === $name|| !$this->hasMethod($method)) {
  114.             throw new Exception('Invalid filemeta property');
  115.         }
  116.  
  117.         return $this->$method();
  118.     }
  119.  
  120.     /**
  121.      * Sets the object by an associative option array.
  122.      *
  123.      * @access public
  124.      * @param array $options 
  125.      * @return self 
  126.      */
  127.     public function setOptions(array $options{
  128.         foreach ($options as $key => $value{
  129.             $method 'set' ucfirst($key);
  130.  
  131.             if ($this->hasMethod($method)) {
  132.                 $this->$method($value);
  133.             }
  134.         }
  135.  
  136.         return $this;
  137.     }
  138.  
  139.     /**
  140.      * Checks if a method exists. Thats used for the __set() and __get()
  141.      *
  142.      * I cache the results of reflection in a static array because its a
  143.      * expensive call.
  144.      * 
  145.      * @access private
  146.      * @staticvar array $methods 
  147.      * @param string $name 
  148.      * @return bool 
  149.      */
  150.     private function hasMethod($name{
  151.         static $methods array();
  152.  
  153.         $className get_class($this);
  154.  
  155.         if (!isset($methods[$className])) {
  156.             $methods[$classNameget_class_methods($this);
  157.         }
  158.  
  159.         return in_array($name$methods[$className]);
  160.     }
  161.  
  162.     /**
  163.      * Returns the proper maper class name. Its naming schema
  164.      * is own class name appended with 'Mapper'.
  165.      *
  166.      * Example:
  167.      * model class -> MediaSearch_Model_Foo
  168.      * mapper class name -> MediaSearch_Model_FooMapper
  169.      *
  170.      * @return string 
  171.      */
  172.     private function getMapperClassName({
  173.         return get_class($this'Mapper';
  174.     }
  175.  
  176.     /**
  177.      * Sets the database mappper obejct.
  178.      *
  179.      * @access public
  180.      * @param MediaSearch_Model_AbstractMapper $mapper 
  181.      * @return MediaSearch_Model_Abstract 
  182.      */
  183.     public function setMapper(MediaSearch_Model_AbstractMapper $mapper{
  184.         $className $this->getMapperClassName();
  185.  
  186.         if (!$mapper instanceof $className{
  187.             $message  'The given mapper object is of type ' get_class($mapper);
  188.             $message .= ' But object of type ' $className ' is required!';
  189.             throw new Exception($message);
  190.         }
  191.  
  192.         $this->mapper $mapper;
  193.  
  194.         return $this;
  195.     }
  196.  
  197.     /**
  198.      * Returns the database mappper obejct.
  199.      *
  200.      * @access public
  201.      * @return MediaSearch_Model_AbstractMapper 
  202.      */
  203.     public function getMapper({
  204.         if (null === $this->mapper{
  205.             $className $this->getMapperClassName();
  206.             $this->setMapper(new $className());
  207.         }
  208.  
  209.         return $this->mapper;
  210.     }
  211.  
  212.     /**
  213.      * Save the model object persistent.
  214.      *
  215.      * @access public
  216.      * @return MediaSearch_Model_Abstract 
  217.      */
  218.     public function save({
  219.         $this->getMapper()->save($this);
  220.  
  221.         return $this;
  222.     }
  223.  
  224.     /**
  225.      * Finds a object by the id and loads itself.
  226.      *
  227.      * @access public
  228.      * @param int $id 
  229.      * @return MediaSearch_Model_Abstract 
  230.      */
  231.     public function find($id{
  232.         $this->getMapper()->find($id$this);
  233.  
  234.         return $this;
  235.     }
  236.  
  237.     /**
  238.      * Fetches all objects and returns in an array.
  239.      * You can provide an optional limit and offset.
  240.      *
  241.      * @access public
  242.      * @param int $limit 
  243.      * @param int $offset 
  244.      * @return array 
  245.      */
  246.     public function fetchAll($limit null$offset null{
  247.         return $this->getMapper()->fetchAll($limit$offset);
  248.     }
  249.  
  250.     /**
  251.      * Counts how much objects are persistent available.
  252.      *
  253.      * @access public
  254.      * @return int 
  255.      */
  256.     public function count({
  257.         return $this->getMapper()->count();
  258.     }
  259. }

Documentation generated on Mon, 17 Aug 2009 14:51:19 +0200 by phpDocumentor 1.4.2