PrimitiveIdentifierList.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2007-2008 by Denis M. Gabaidulin, Konstantin V. Arkhipov *
00004  *                                                                          *
00005  *   This program is free software; you can redistribute it and/or modify   *
00006  *   it under the terms of the GNU Lesser General Public License as         *
00007  *   published by the Free Software Foundation; either version 3 of the     *
00008  *   License, or (at your option) any later version.                        *
00009  *                                                                          *
00010  ****************************************************************************/
00011 
00015     final class PrimitiveIdentifierList extends PrimitiveIdentifier
00016     {
00017         protected $value = array();
00018         private $ignoreEmpty = false;
00019         
00023         public function clean()
00024         {
00025             parent::clean();
00026             
00027             // restoring our very own default
00028             $this->value = array();
00029             
00030             return $this;
00031         }
00032         
00036         public function setValue($value)
00037         {
00038             if ($value) {
00039                 Assert::isArray($value);
00040                 Assert::isInstance(current($value), $this->className);
00041             }
00042             
00043             $this->value = $value;
00044             
00045             return $this;
00046         }
00047         
00048         public function importValue($value)
00049         {
00050             if ($value instanceof UnifiedContainer) {
00051                 if ($value->isLazy())
00052                     return $this->import(
00053                         array($this->name => $value->getList())
00054                     );
00055                 elseif (
00056                     $value->getParentObject()->getId()
00057                     && ($list = $value->getList())
00058                 ) {
00059                     return $this->import(
00060                         array($this->name => ArrayUtils::getIdsArray($list))
00061                     );
00062                 } else {
00063                     return parent::importValue(null);
00064                 }
00065             }
00066             
00067             if (is_array($value)) {
00068                 try {
00069                     if ($this->scalar)
00070                         Assert::isScalar(current($value));
00071                     else
00072                         Assert::isInteger(current($value));
00073                     
00074                     return $this->import(
00075                         array($this->name => $value)
00076                     );
00077                 } catch (WrongArgumentException $e) {
00078                     return $this->import(
00079                         array($this->name => ArrayUtils::getIdsArray($value))
00080                     );
00081                 }
00082             }
00083             
00084             return parent::importValue($value);
00085         }
00086         
00087         public function import($scope)
00088         {
00089             if (!$this->className)
00090                 throw new WrongStateException(
00091                     "no class defined for PrimitiveIdentifierList '{$this->name}'"
00092                 );
00093             
00094             if (!BasePrimitive::import($scope))
00095                 return null;
00096             
00097             if (!is_array($scope[$this->name]))
00098                 return false;
00099             
00100             $list = array_unique($scope[$this->name]);
00101             
00102             $values = array();
00103             
00104             foreach ($list as $id) {
00105                 if ((string) $id == "" && $this->isIgnoreEmpty())
00106                     continue;
00107 
00108                 if (
00109                     ($this->scalar && !Assert::checkScalar($id))
00110                     || (!$this->scalar && !Assert::checkInteger($id))
00111                 )
00112                     return false;
00113                 
00114                 $values[] = $id;
00115             }
00116             
00117             $objectList = $this->dao()->getListByIds($values);
00118             
00119             if (
00120                 count($objectList) == count($values)
00121                 && !($this->min && count($values) < $this->min)
00122                 && !($this->max && count($values) > $this->max)
00123             ) {
00124                 $this->value = $objectList;
00125                 return true;
00126             }
00127             
00128             return false;
00129         }
00130         
00131         public function exportValue()
00132         {
00133             if (!$this->value)
00134                 return null;
00135             
00136             return ArrayUtils::getIdsArray($this->value);
00137         }
00138 
00139         public function setIgnoreEmpty($orly = true)
00140         {
00141             $this->ignoreEmpty = ($orly === true);
00142 
00143             return $this;
00144         }
00145 
00146         public function isIgnoreEmpty()
00147         {
00148             return $this->ignoreEmpty;
00149         }
00150     }
00151 ?>