Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00015 final class FormUtils extends StaticFactory
00016 {
00017 public static function object2form(
00018 $object, Form $form, $ignoreNull = true
00019 )
00020 {
00021 Assert::isTrue(is_object($object));
00022
00023 $primitives = $form->getPrimitiveList();
00024
00025 if ($object instanceof Prototyped) {
00026 $proto = $object->proto();
00027
00028 foreach (array_keys($proto->getExpandedPropertyList()) as $name) {
00029 if ($form->primitiveExists($name)) {
00030 $proto->importPrimitive(
00031 $name,
00032 $form,
00033 $form->get($name),
00034 $object,
00035 $ignoreNull
00036 );
00037 }
00038 }
00039 } else {
00040 $class = new ReflectionClass($object);
00041
00042 foreach ($class->getProperties() as $property) {
00043 $name = $property->getName();
00044
00045 if (isset($primitives[$name])) {
00046 $getter = 'get'.ucfirst($name);
00047 if ($class->hasMethod($getter)) {
00048 $value = $object->$getter();
00049 if (!$ignoreNull || ($value !== null)) {
00050 $form->importValue($name, $value);
00051 }
00052 }
00053 }
00054 }
00055 }
00056 }
00057
00058 public static function form2object(
00059 Form $form, $object, $ignoreNull = true
00060 )
00061 {
00062 Assert::isTrue(is_object($object));
00063
00064 if ($object instanceof Prototyped) {
00065 $proto = $object->proto();
00066 $list = $proto->getExpandedPropertyList();
00067
00068 foreach ($form->getPrimitiveList() as $name => $prm) {
00069 if (isset($list[$name])) {
00070 $proto->exportPrimitive($name, $prm, $object, $ignoreNull);
00071 }
00072 }
00073 } else {
00074 $class = new ReflectionClass($object);
00075
00076 foreach ($form->getPrimitiveList() as $name => $prm) {
00077 $setter = 'set'.ucfirst($name);
00078
00079 if ($prm instanceof ListedPrimitive)
00080 $value = $prm->getChoiceValue();
00081 else
00082 $value = $prm->getValue();
00083
00084 if (
00085 $class->hasMethod($setter)
00086 && (!$ignoreNull || ($value !== null))
00087 ) {
00088 if (
00089 $prm->getName() == 'id'
00090 && (
00091 $value instanceof Identifiable
00092 )
00093 ) {
00094 $value = $value->getId();
00095 }
00096
00097 if ($value === null) {
00098 $dropper = 'drop'.ucfirst($name);
00099
00100 if ($class->hasMethod($dropper)) {
00101 $object->$dropper();
00102 continue;
00103 }
00104 }
00105
00106 $object->$setter($value);
00107 }
00108 }
00109 }
00110 }
00111
00112 public static function checkPrototyped(Prototyped $object)
00113 {
00114 $form = $object->proto()->makeForm();
00115
00116 self::object2form($object, $form, false);
00117
00118 return $form->getErrors();
00119 }
00120 }
00121 ?>