# DTOSetter.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007 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
    00012     final class DTOSetter extends PrototypedSetter
    00013     {
    00014         private $getter = null;
    00015
    00016         public function set($name, $value)
    00017         {
    00018             if (!isset($this->mapping[$name]))
    00019                 throw new WrongArgumentException(
    00020                     "knows nothing about property '{$name}'"
    00021                 );
    00022
    00023             $primitive = $this->mapping[$name];
    00024
    00025             $setter = 'set'.ucfirst($primitive->getName());
    00026
    00027             if (!method_exists($this->object, $setter))
    00028                 throw new WrongArgumentException(
    00029                     "cannot find mutator for '$name' in class "
    00030                     .get_class($this->object)
    00031                 );
    00032
    00033             if (is_object($value)) {
    00034
    00035                 if (
    00036                     ($primitive instanceof PrimitiveAnyType)
    00037                     && ($value instanceof PrototypedEntity)
    00038                 )
    00039                     $value =
    00040                         ObjectToDTOConverter::create($value->entityProto())->
    00041                             make($value);
    00042                 else
    00043                     $value = $this->dtoValue($value, $primitive);
    00044
    00045             } elseif (is_array($value) && is_object(current($value))) {
    00046
    00047                 $dtoValue = array();
    00048
    00049                 foreach ($value as $oneValue) {
    00050                     Assert::isTrue(
    00051                         is_object($oneValue),
    00052                         'array must contain only objects'
    00053                     );
    00054
    00055                     $dtoValue[] = $this->dtoValue($oneValue, $primitive);
    00056                 }
    00057
    00058                 $value = $dtoValue;
    00059             }
    00060
    00061             return $this->object->$setter($value);
    00062         }
    00063
    00064         // TODO: use export for all primitives
    00065         private function dtoValue($value, BasePrimitive $primitive)
    00066         {
    00067             $result = null;
    00068
    00069             if ($value instanceof DTOClass) {
    00070
    00071                 $result = $value; // have been already built
    00072
    00073             } elseif ($primitive instanceof PrimitivePolymorphicIdentifier) {
    00074
    00075                 $result = PrimitivePolymorphicIdentifier::export($value);
    00076
    00077             } elseif ($primitive instanceof PrimitiveBoolean) {
    00078
    00079                 $result = (boolean)$value;
    00080
    00081             } elseif ($value instanceof Identifiable) {
    00082
    00083                 $result = $value->getId();
    00084
    00085             } elseif (
    00086                 $value instanceof Stringable
    00087             ) {
    00088                 $result = $value->toString();
    00089
    00090             } else
    00091                 throw new WrongArgumentException(
    00092                     'don\'t know how to convert to DTO value of class '
    00093                     .get_class($value)
    00094                 );
    00095
    00096             return $result;
    00097         }
    00098
    00102         public function getGetter()
    00103         {
    00104             if (!$this->getter) {
    00105                 $this->getter = new DTOGetter($this->proto, $this->object);
    00106             }
    00107
    00108             return $this->getter;
    00109         }
    00110     }
    00111 ?>