00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class FormUtils extends StaticFactory
00017 {
00018 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 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 (
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 ?>