PrimitiveClass.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2008 by 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 PrimitiveClass extends PrimitiveString
00016     {
00017         private $ofClassName = null;
00018         
00019         public function import($scope)
00020         {
00021             if (!($result = parent::import($scope)))
00022                 return $result;
00023             
00024             if (
00025                 !ClassUtils::isClassName($scope[$this->name])
00026                 || !$this->classExists($scope[$this->name])
00027                 || (
00028                     $this->ofClassName
00029                     && !ClassUtils::isInstanceOf(
00030                         $scope[$this->name],
00031                         $this->ofClassName
00032                     )
00033                 )
00034             ) {
00035                 $this->value = null;
00036                 
00037                 return false;
00038             }
00039             
00040             return true;
00041         }
00042         
00047         public function of($class)
00048         {
00049             $className = $this->guessClassName($class);
00050             
00051             Assert::isTrue(
00052                 class_exists($className, true)
00053                 || interface_exists($className, true),
00054                 "knows nothing about '{$className}' class"
00055             );
00056             
00057             $this->ofClassName = $className;
00058             
00059             return $this;
00060         }
00061         
00062         private function classExists($name)
00063         {
00064             try {
00065                 return class_exists($name, true);
00066             } catch (ClassNotFoundException $e) {
00067                 return false;
00068             }
00069         }
00070         
00071         private function guessClassName($class)
00072         {
00073             if (is_string($class))
00074                 return $class;
00075             
00076             elseif (is_object($class))
00077                 return get_class($class);
00078             
00079             throw new WrongArgumentException('strange class given - '.$class);
00080         }
00081     }
00082 ?>