Example #1
0
 /**
  * Update properties of $object based on what is provided in $request.
  * $properties can have 'simple' and 'complex' properties.
  * - A simple property is just a name using underscores, and it's getter and setter methods should
  *   be the camel case version of the property name.
  * - An advanced property can have:
  *   > A getter/setter base name
  *   > A flag indicating it is important. Important properties are checked to determined if they
  *     really changed. For example: properties that should cause a problem to be rejudged,
  *     like time limits or memory constraints.
  *   > A transform method that takes the new property value stored in the request and transforms
  *     it into the proper form that should be stored in $object. For example:
  *     function($value) { return gmdate('Y-m-d H:i:s', $value); }
  *
  * @param Request $request
  * @param object $object
  * @param array $properties
  * @return boolean True if there were changes to any property marked as 'important'.
  */
 protected static function updateValueProperties($request, $object, $properties)
 {
     $importantChange = false;
     foreach ($properties as $source => $info) {
         if (is_int($source)) {
             // Simple property:
             $source = $info;
             $info = array(Controller::toCamelCase($source));
         }
         if (is_null($request[$source])) {
             continue;
         }
         // Get the base name for the property accessors.
         if (isset($info[0]) || isset($info['accessor'])) {
             $accessor = isset($info[0]) ? $info[0] : $info['accessor'];
         } else {
             $accessor = Controller::toCamelCase($source);
         }
         // Get or calculate new value.
         $value = $request[$source];
         if (isset($info[2]) || isset($info['transform'])) {
             $transform = isset($info[2]) ? $info[2] : $info['transform'];
             $value = $transform($value);
         }
         // Important property, so check if it changes.
         if (isset($info[1]) || isset($info['important'])) {
             $important = isset($info[1]) ? $info[1] : $info['important'];
             if ($important) {
                 $getter = 'get' . $accessor;
                 if ($value != $object->{$getter}()) {
                     $importantChange = true;
                 }
             }
         }
         $setter = 'set' . $accessor;
         $object->{$setter}($value);
     }
     return $importantChange;
 }