# PrimitiveEnumerationList.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007-2008 by Ivan Y. Khvostishkov                       *
    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 PrimitiveEnumerationList extends PrimitiveEnumeration
    00016     {
    00017         protected $value = array();
    00018
    00022         public function clean()
    00023         {
    00024             parent::clean();
    00025
    00026             // restoring our very own default
    00027             $this->value = array();
    00028
    00029             return $this;
    00030         }
    00031
    00035         public function setValue(/* Enumeration */ $value)
    00036         {
    00037             if ($value) {
    00038                 Assert::isArray($value);
    00039                 Assert::isInstance(current($value), 'Enumeration');
    00040             }
    00041
    00042             $this->value = $value;
    00043
    00044             return $this;
    00045         }
    00046
    00047         public function importValue($value)
    00048         {
    00049             if (is_array($value)) {
    00050                 try {
    00051                     Assert::isInteger(current($value));
    00052
    00053                     return $this->import(
    00054                         array($this->name => $value)
    00055                     );
    00056                 } catch (WrongArgumentException $e) {
    00057                     return $this->import(
    00058                         array($this->name => ArrayUtils::getIdsArray($value))
    00059                     );
    00060                 }
    00061             }
    00062
    00063             return parent::importValue($value);
    00064         }
    00065
    00066         public function import($scope)
    00067         {
    00068             if (!$this->className)
    00069                 throw new WrongStateException(
    00070                     "no class defined for PrimitiveIdentifierList '{$this->name}'"
    00071                 );
    00072
    00073             if (!BasePrimitive::import($scope))
    00074                 return null;
    00075
    00076             if (!is_array($scope[$this->name]))
    00077                 return false;
    00078
    00079             $list = array_unique($scope[$this->name]);
    00080
    00081             $values = array();
    00082
    00083             foreach ($list as $id) {
    00084                 if (!Assert::checkInteger($id))
    00085                     return false;
    00086
    00087                 $values[] = $id;
    00088             }
    00089
    00090             $objectList = array();
    00091
    00092             foreach ($values as $value) {
    00093                 $className = $this->className;
    00094                 $objectList[] = new $className($value);
    00095             }
    00096
    00097             if (count($objectList) == count($values)) {
    00098                 $this->value = $objectList;
    00099                 return true;
    00100             }
    00101
    00102             return false;
    00103         }
    00104
    00105         public function exportValue()
    00106         {
    00107             if (!$this->value)
    00108                 return null;
    00109
    00110             return ArrayUtils::getIdsArray($this->value);
    00111         }
    00112     }
    00113 ?>