예제 #1
0
 public function __construct($data = NULL)
 {
     if (!is_null($data) && !is_numeric($data) && !is_array($data) && !$data instanceof stdClass) {
         throw new ShopException('Invalid $data type');
     }
     if (is_array($data) || $data instanceof stdClass) {
         $this->loadFromArray((array) $data);
     }
     /**
      * Look for Item repository.
      * For IDE purposes like code completion $this->repo's type should be hinted in each Item the way I did in Cart.
      */
     $repoClass = $this->getRepo();
     $reflection = new ReflectionClass($repoClass);
     $repoClass = (string) $reflection->getName();
     $this->repo = $reflection->isInstantiable() ? new $repoClass() : null;
     /**
      * Look for srl field if it's not already set in class $meta
      */
     $reflection = new ReflectionClass($this);
     if ($srlField = $this->getMeta('srl')) {
         //if srl is given
         if (!$reflection->hasProperty($srlField)) {
             throw new ShopException("Srl field '{$srlField}' doesn't exist");
         }
     } elseif ($reflection->hasProperty('srl')) {
         $this->setMeta('srl', 'srl');
     } else {
         //else return the first _srl field
         foreach ($this as $field => $value) {
             if (substr($field, strlen($field) - 4, strlen($field)) === '_srl') {
                 $this->setMeta('srl', $field);
                 break;
             }
         }
         if (!$this->getMeta('srl')) {
             throw new ShopException('Couldn\'t identify the _srl column');
         }
     }
     //data can also be a serial
     if (is_numeric($data)) {
         if ($obj = $this->repo->get($data, "get%E", get_called_class())) {
             $this->copy($obj);
         } else {
             throw new ShopException("No such {$this->repo->entity} srl {$data}");
         }
     }
     if (is_null(self::$cache)) {
         self::$cache = new ShopCache();
     }
 }