Beispiel #1
0
 function testSet()
 {
     $array = [];
     CoreUtils::set($array, 'key', 'value');
     self::assertArrayHasKey('key', $array);
     self::assertEquals('value', $array['key']);
     $object = new stdClass();
     CoreUtils::set($object, 'key', 'value');
     self::assertObjectHasAttribute('key', $object);
     self::assertEquals('value', $object->key);
 }
Beispiel #2
0
 /**
  * POST data validator function used when creating/editing posts
  *
  * @param string    $thing "request"/"reservation"
  * @param array     $array Array to output the checked data into
  * @param Post|null $Post  Optional, exsting post to compare new data against
  */
 static function checkPostDetails($thing, &$array, $Post = null)
 {
     $editing = !empty($Post);
     $label = (new Input('label', 'string', array(Input::IS_OPTIONAL => true, Input::IN_RANGE => [3, 255], Input::CUSTOM_ERROR_MESSAGES => array(Input::ERROR_RANGE => 'The description must be between @min and @max characters'))))->out();
     if (isset($label)) {
         if (!$editing || $label !== $Post->label) {
             CoreUtils::checkStringValidity($label, 'The description', INVERSE_PRINTABLE_ASCII_PATTERN);
             CoreUtils::set($array, 'label', $label);
         }
     } else {
         if (!$editing && $thing !== 'reservation') {
             Response::fail('Description cannot be empty');
         } else {
             CoreUtils::set($array, 'label', null);
         }
     }
     if ($thing === 'request') {
         $type = (new Input('type', function ($value) {
             if (!in_array($value, array('chr', 'obj', 'bg'))) {
                 return Input::ERROR_INVALID;
             }
         }, array(Input::IS_OPTIONAL => true, Input::CUSTOM_ERROR_MESSAGES => array(Input::ERROR_INVALID => "Request type (@value) is invalid"))))->out();
         if (!isset($type) && !$editing) {
             respnd("Missing request type");
         }
         if (!$editing || isset($type) && $type !== $Post->type) {
             CoreUtils::set($array, 'type', $type);
         }
         if (Permission::sufficient('developer')) {
             $reserved_at = self::validateReservedAt();
             if (isset($reserved_at)) {
                 if ($reserved_at !== strtotime($Post->reserved_at)) {
                     CoreUtils::set($array, 'reserved_at', date('c', $reserved_at));
                 }
             } else {
                 CoreUtils::set($array, 'reserved_at', null);
             }
         }
     }
     if (Permission::sufficient('developer')) {
         $posted = (new Input('posted', 'timestamp', array(Input::IS_OPTIONAL => true, Input::CUSTOM_ERROR_MESSAGES => array(Input::ERROR_INVALID => '"Posted" timestamp (@value) is invalid'))))->out();
         if (isset($posted) && $posted !== strtotime($Post->posted)) {
             CoreUtils::set($array, 'posted', date('c', $posted));
         }
         $finished_at = self::validateFinishedAt();
         if (isset($finished_at)) {
             if ($finished_at !== strtotime($Post->finished_at)) {
                 CoreUtils::set($array, 'finished_at', date('c', $finished_at));
             }
         } else {
             CoreUtils::set($array, 'finished_at', null);
         }
     }
 }