FormUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 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 /* $Id: FormUtils.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class FormUtils extends StaticFactory
00017     {
00018         /* void */ public static function object2form(
00019             $object, Form $form, $ignoreNull = true
00020         )
00021         {
00022             Assert::isTrue(is_object($object));
00023             
00024             $primitives = $form->getPrimitiveList();
00025             $class = new ReflectionClass($object);
00026             
00027             $isPrototyped = ($object instanceof Prototyped);
00028             
00029             if ($isPrototyped) {
00030                 $propertyList = $object->proto()->getPropertyList();
00031             } else {
00032                 $propertyList = $class->getProperties();
00033             }
00034             
00035             foreach ($propertyList as $property) {
00036                 $name = $property->getName();
00037                 
00038                 if (isset($primitives[$name])) {
00039                     $getter = 'get'.ucfirst($name);
00040                     if ($class->hasMethod($getter)) {
00041                         $value = $object->$getter();
00042                         if (!$ignoreNull || ($value !== null)) {
00043                             $form->importValue($name, $value);
00044                         }
00045                     }
00046                 }
00047             }
00048         }
00049         
00050         /* void */ public static function form2object(
00051             Form $form, $object, $ignoreNull = true
00052         )
00053         {
00054             Assert::isTrue(is_object($object));
00055             
00056             $class = new ReflectionClass($object);
00057             
00058             foreach ($form->getPrimitiveList() as $name => $prm) {
00059                 $setter = 'set'.ucfirst($name);
00060                 
00061                 if ($prm instanceof ListedPrimitive)
00062                     $value = $prm->getChoiceValue();
00063                 else
00064                     $value = $prm->getValue();
00065                 
00066                 if (
00067                     $class->hasMethod($setter)
00068                     && (!$ignoreNull || ($value !== null))
00069                 ) {
00070                     if ( // magic!
00071                         $prm->getName() == 'id'
00072                         && (
00073                             $value instanceof Identifiable
00074                         )
00075                     ) {
00076                         $value = $value->getId();
00077                     }
00078                     
00079                     if ($value === null) {
00080                         $dropper = 'drop'.ucfirst($name);
00081                         
00082                         if ($class->hasMethod($dropper)) {
00083                             $object->$dropper();
00084                             continue;
00085                         }
00086                     }
00087                     
00088                     $object->$setter($value);
00089                 }
00090             }
00091         }
00092         
00093         public static function checkPrototyped(Prototyped $object)
00094         {
00095             $form = $object->proto()->makeForm();
00096             
00097             self::object2form($object, $form, false);
00098             
00099             return $form->getErrors();
00100         }
00101     }
00102 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4