예제 #1
0
 /**
  * Tests Numerics::assertPositiveInteger()
  * 
  */
 public function testAssertPossitiveInteger()
 {
     $input = '7215';
     $output = \Altumo\Validation\Numerics::assertPositiveInteger($input);
     $this->assertTrue(is_integer($output));
     $input = 7215.0;
     $output = \Altumo\Validation\Numerics::assertPositiveInteger($input);
     $this->assertTrue(is_integer($output));
     $input = (double) 7215;
     $output = \Altumo\Validation\Numerics::assertPositiveInteger($input);
     $this->assertTrue(is_integer($output));
     $input = (double) 7215;
     $output = \Altumo\Validation\Numerics::assertPositiveInteger($input);
     $this->assertTrue(is_integer($output));
     $input = 7215;
     $output = \Altumo\Validation\Numerics::assertPositiveInteger($input);
     $this->assertTrue(is_integer($output));
     try {
         $input = 72.56;
         $output = \Altumo\Validation\Numerics::assertPositiveInteger($input);
         $this->assertTrue(false);
     } catch (\Exception $e) {
         $this->assertTrue(true);
     }
     try {
         $input = 0;
         $output = \Altumo\Validation\Numerics::assertPositiveInteger($input);
         $this->assertTrue(false);
     } catch (\Exception $e) {
         $this->assertTrue(true);
     }
 }
예제 #2
0
 /**
  * Setter for the page_number field on this ApiPager.
  * 
  * @param integer $page_number
  */
 public function setPageNumber($page_number)
 {
     try {
         $page_number = \Altumo\Validation\Numerics::assertPositiveInteger($page_number);
     } catch (\Exception $e) {
         $page_number = 1;
     }
     $this->page_number = $page_number;
 }
예제 #3
0
 /**
  * Gets all stories by project id.
  * 
  * @param integer $project_id
  * @return array
  */
 public function getAllStoriesByProjectId($project_id)
 {
     try {
         $project_id = \Altumo\Validation\Numerics::assertPositiveInteger($project_id);
         $stories = $this->sendRequest('/services/v3/projects/' . $project_id . '/stories');
         if (property_exists($stories, 'story')) {
             $stories = $stories->story;
         }
     } catch (\Exception $e) {
         $stories = array();
     }
     return $stories;
 }
예제 #4
0
 protected function composeFrequentCronCommand($command, $frequency)
 {
     $command = \Altumo\Validation\Strings::assertNonEmptyString($command, '$command expects non-empty string');
     $frequency = \Altumo\Validation\Numerics::assertPositiveInteger($frequency, '$frequency expects positive integer');
     return sprintf(self::getFrequentCronPath() . " --command=%s --frequency=%s", escapeshellarg($command), escapeshellarg($frequency));
 }
예제 #5
0
 /**
  * Gets the Request message body in its deserialized form (json_decoded or xml parsed array).
  * 
  * If the request body requires expansion (see messageBodyRequiresExpansion), the result will
  * be modified to include the ids being requested.
  * 
  * Example: 
  * 
  * PUT /community/posts/1,2,3
  * 
  * {
  *   "author_id": 40,
  *   "message": "Hello there!"
  * }
  * 
  * Will result in an expanded body that looks like:
  * 
  * array(
  *  array(
  *   id => 1,
  *   author_id => 40,
  *   messahe => "Hello there!"
  *  ),
  * 
  *  array(
  *   id => 2,
  *   author_id => 40,
  *   messahe => "Hello there!"
  *  ),
  * 
  *  array(
  *   id => 3,
  *   author_id => 40,
  *   messahe => "Hello there!"
  *  )
  * )
  * 
  * @throws Exception //if message body was empty or could not be parsed.
  * @throws Exception //if duplicate remote_ids are detected.
  * @throws Exception //provided remote_id is not a positive integer
  * @return array
  */
 public function getMessageBody()
 {
     $base_object_modifications = $this->getMessageBodyData();
     if ($this->messageBodyRequiresExpansion()) {
         // Get the ids that will be used to expand the message body
         try {
             $object_ids = \Altumo\Validation\Arrays::sanitizeCsvArrayPostitiveInteger($this->getParameter('ids', ''));
         } catch (Exception $e) {
         }
         // The "id" parameter cannot be batch modified, so it cannot be part of the body in this type of request.
         if (isset($base_object_modifications['id'])) {
             throw new \Exception('When applying the same change to multiple objects, "id" cannot be included as part of the message body.');
         }
         // $products_array will end up looking as if the request contained multiple objects with IDs to modify.
         $objects = array();
         foreach ($object_ids as $object_id) {
             $objects[] = array_merge($base_object_modifications, array('id' => $object_id));
         }
         $base_object_modifications = $objects;
     }
     // This allows a single object to be passed. Peope who use the API
     // to post/put a single object don't have to wrap it in an array
     $base_object_keys = array_keys($base_object_modifications);
     // if the first index is not numerical,
     if (!empty($base_object_keys) && $base_object_keys[0] !== 0) {
         // wrap modifications in another array
         $base_object_modifications = array($base_object_modifications);
     }
     //get the hightest supplied remote_id; also, check that none of the supplied
     //remote_ids are duplicates
     $remote_id = 0;
     $used_remote_ids = array();
     foreach ($base_object_modifications as $index => $object) {
         if (is_array($object) && array_key_exists('remote_id', $object)) {
             $current_remote_id = $object['remote_id'];
             try {
                 $current_remote_id = \Altumo\Validation\Numerics::assertPositiveInteger($current_remote_id);
             } catch (\Exception $e) {
                 throw new \Exception('Provided remote_id must be a positive integer.');
             }
             //ensure remote_ids are unique
             if (array_key_exists($current_remote_id, $used_remote_ids)) {
                 throw new \Exception('Duplicate remote_id detected. Please ensure remote_ids are unique.');
             } else {
                 $used_remote_ids[$current_remote_id] = true;
             }
             //if this remote_id is higher than the current highest, save the higher of the two
             if ($current_remote_id > $remote_id) {
                 $remote_id = $current_remote_id;
             }
         }
     }
     //ensure remote_id integers are present; set them if they aren't
     foreach ($base_object_modifications as $index => &$object) {
         if (is_array($object) && !array_key_exists('remote_id', $object)) {
             $remote_id++;
             $object['remote_id'] = $remote_id;
         }
     }
     return $base_object_modifications;
 }
예제 #6
0
 /**
  * Determines if there are errors set for this ApiResponse for a specific remote_id.
  * 
  * @param integer $remote_id //primary key of the remote system
  * @throws Exception //if $remote_id is not a positive integer or castable as one
  * @return boolean
  */
 public function hasErrorsForRemoteId($remote_id)
 {
     $remote_id = \Altumo\Validation\Numerics::assertPositiveInteger($remote_id);
     return array_key_exists($remote_id, $this->remote_ids);
 }
예제 #7
0
 /**
  * This method calls the setters on the provided $model with the values from 
  * $request_object, according to the $field_maps.
  * 
  * @param boolean $update
  *   //PUT (update) request if true, POST (create) if false
  * 
  * @param \sfAltumoPlugin\Api\ApiResponse $response
  *   //the response that will contain the errors
  * 
  * @param \BaseObject $model
  *   //the model the is having its accessors called
  * 
  * @param array $field_maps
  *   //the field maps from $request_object array keys to $model accessors
  * 
  * @param mixed $request_object
  *   //the array that contains the fields the will be placed into the model
  * 
  * @param integer $remote_id
  *   //the remote_id of the record being referenced. If this is null, the
  *     $request_object must contain it
  * 
  * @throws \Exception
  *   //if the accessor method doesn't exist
  * 
  * @throws \Exception                    
  *   //if the accessor method doesn't exist
  * 
  * @throws \Exception                    
  *   //if the field is required and couldn't be found in $request_object
  * 
  * @throws \Exception
  *   //if $remote_id is not an integer
  * 
  * @return array 
  *   //an array of $type objects (models)
  */
 public static function callObjectSetters($update, $response, $model, &$field_maps, &$request_object, $remote_id = null)
 {
     foreach ($field_maps as $field_map) {
         //validate the accessor
         $accessor_suffix = $field_map->getModelAccessor();
         $setter_method = 'set' . $accessor_suffix;
         $getter_method = 'get' . $accessor_suffix;
         if (!method_exists($model, $setter_method)) {
             continue;
             //skip it if method doesn't exist
             throw new \Exception('Accessor method does not exist: ' . $setter_method);
         }
         //validate the remote_id
         if (is_null($remote_id)) {
             if (!array_key_exists('remote_id', $request_object)) {
                 throw new \Exception('The request_object must have a remote_id');
             } else {
                 $remote_id = $request_object['remote_id'];
             }
         } else {
             $remote_id = \Altumo\Validation\Numerics::assertPositiveInteger($remote_id);
         }
         //validate that the field exists in the request
         //don't worry about the field not being there if this is an update
         $field_key = $field_map->getRequestField();
         if (!array_key_exists($field_key, $request_object)) {
             if (!$update) {
                 if ($field_map->isRequired()) {
                     $response->addError('Field does not exist: ' . $field_map->getDescription(), $remote_id, $field_map);
                 }
             }
             continue;
         }
         //invoke the setter and attach error, if there is one
         try {
             call_user_func_array(array($model, $setter_method), array($request_object[$field_key]));
         } catch (\Exception $e) {
             $response->addError($e->getMessage(), $remote_id, $field_map);
         }
     }
 }
예제 #8
0
 /**
  * Initializes the Message object. Pre-sets all required variables
  * 
  * // Parameters for this functions can be specified by:
  *   - app_mailer_smtp_server
  *   - app_mailer_smtp_port
  *   - app_mailer_smtp_security_scheme
  *   - app_mailer_smtp_user
  *   - app_mailer_smtp_password
  *   - app_mailer_reroute_to
  * 
  * They can be individually overridden by passing in parameters.
  * 
  * If all of them are null, the system's default mail transport is used.
  * 
  * 
  * @param string $smtp_server                    // host name or ip of the smtp server to use
  * @param int $smtp_port                         // port for the smtp server
  * @param string $smtp_security_scheme           // encryption scheme 
  *                                               // - self::TRANSPORT_SECURITY_SCHEME_SSL
  *                                               // - self::TRANSPORT_SECURITY_SCHEME_TLS
  * @param mixed $smtp_username                   // username for the smtp server
  * @param mixed $smtp_passowrd                   // password for the smtp server
  * @param mixed $testing_mode_reroute_to_email   // reroute this message to a
  *                                               // different email address
  *                                               // instead of sending it to
  *                                               // the recipients.
  *                                               // (for testing purposes)
  * 
  * @return \Swift_Transport
  */
 protected function initialize($smtp_server = null, $smtp_port = null, $smtp_security_scheme = null, $smtp_username = null, $smtp_passowrd = null, $testing_mode_reroute_to_email = null)
 {
     if (is_null($smtp_server)) {
         $smtp_server = \sfConfig::get('app_mailer_smtp_server', null);
     }
     if (is_null($smtp_port)) {
         $smtp_port = \sfConfig::get('app_mailer_smtp_port', null);
     }
     if (is_null($smtp_security_scheme)) {
         $smtp_security_scheme = \sfConfig::get('app_mailer_smtp_security_scheme', null);
     }
     if (is_null($smtp_username)) {
         $smtp_username = \sfConfig::get('app_mailer_smtp_user', null);
     }
     if (is_null($smtp_passowrd)) {
         $smtp_passowrd = \sfConfig::get('app_mailer_smtp_password', null);
     }
     if (is_null($testing_mode_reroute_to_email)) {
         $testing_mode_reroute_to_email = \sfConfig::get('app_mailer_reroute_to', null);
     }
     if (!is_null($smtp_server) || !is_null($smtp_port) || !is_null($smtp_security_scheme) || !is_null($smtp_username) || !is_null($smtp_passowrd)) {
         if (!is_null($smtp_server)) {
             $smtp_server = \Altumo\Validation\Strings::assertNonEmptyString($smtp_server);
         }
         if (!is_null($smtp_port)) {
             $smtp_port = \Altumo\Validation\Numerics::assertPositiveInteger($smtp_port);
         }
         if (!is_null($smtp_security_scheme)) {
             if (!in_array($smtp_security_scheme, array(self::TRANSPORT_SECURITY_SCHEME_SSL, self::TRANSPORT_SECURITY_SCHEME_TLS))) {
                 throw new Exception('Invalid email transport security scheme.');
             }
         }
         if (!is_null($smtp_username)) {
             $smtp_username = \Altumo\Validation\Strings::assertString($smtp_username);
         }
         if (!is_null($smtp_passowrd)) {
             $smtp_passowrd = \Altumo\Validation\Strings::assertString($smtp_passowrd);
         }
         $this->setSwiftTransport(\Swift_SmtpTransport::newInstance($smtp_server, $smtp_port, $smtp_security_scheme)->setUsername($smtp_username)->setPassword($smtp_passowrd));
     } else {
         $this->setSwiftTransport(\Swift_SmtpTransport::newInstance());
     }
     // enable testing mode if required.
     if (!is_null($testing_mode_reroute_to_email)) {
         $this->setTestingModeRerouteToEmail($testing_mode_reroute_to_email);
     }
     $this->setEnabled(\sfConfig::get('app_mailer_enabled', false));
     return $this->getSwiftTransport();
 }
예제 #9
0
파일: Arrays.php 프로젝트: homer6/altumo
 /**
  * Ensures that the input is an array or a CSV string representing an array.
  * If it's a CSV string, it converts it into an array with the elements split 
  * at the comma delimeter.  This method removes empty values. 
  * 
  * Each value must be a postitive integer.  Throws and exception if they aren't
  * (doesn't throw on empty value, just removes it).  This method will santize
  * the values; so, if they're a string "2", they'll be converted to int 2.
  * 
  * 
  * Eg.
  *     sanitizeCsvArrayPostitiveInteger( '1,2,,,,3' );   //returns array( 1, 2, 3 );
  *     sanitizeCsvArrayPostitiveInteger( array( 1, 2, 3 ) );   //returns array( 1, 2, 3 );
  *     sanitizeCsvArrayPostitiveInteger( array( 1, "hello", 3 ) );   //throws Exception
  *     sanitizeCsvArrayPostitiveInteger( '1,2,,"hello",,3' );   //throws Exception
  * 
  * @param mixed $input
  * @param string $exception_message  
  *   //custom Exception message to throw. 
  *     If null, default message will be used.
  * 
  * @throws Exception //if $input is not null, a string or an array
  * @throws Exception //if $input contains elements that are not integers (or castable as integers)
  * @return array
  */
 public static function sanitizeCsvArrayPostitiveInteger($input, $exception_message = null)
 {
     //accept a single integer
     try {
         $number = \Altumo\Validation\Numerics::assertPositiveInteger($input, $exception_message);
         return array($number);
     } catch (\Exception $e) {
     }
     //or a collection of them
     $array = self::sanitizeCsvArray($input);
     foreach ($array as $key => $value) {
         $array[$key] = \Altumo\Validation\Numerics::assertPositiveInteger($value, $exception_message);
     }
     return $array;
 }
예제 #10
0
 /**
  * Gets a stories by Pivotal project id
  * 
  * @param integer $project_id
  * @throws \Exception //if $project_id is not a positive integer
  * @throws \Exception //if no Project could be found.
  * @return array
  */
 public function getStoriesByProjectId($project_id)
 {
     $project_id = \Altumo\Validation\Numerics::assertPositiveInteger($project_id);
     $collection = $this->getStoriesCollection();
     //get it from the database
     $cursor = $collection->find(array('project_id' => (string) $project_id));
     $cursor->sort(array('priority_order' => 1));
     $stories = array();
     while ($cursor->hasNext()) {
         $story = $cursor->getNext();
         $stories[] = $story;
     }
     return $stories;
 }