/** * 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); } }
/** * Adds an item * * @param double $value * @param string $label * * @throws \Exception if $value doesn't validate * @throws \Exception if $label doesn't validate * * @return \sfAltumoPlugin\Geckoboard\Widget\FunnelWidget */ public function addItem($value, $label) { $value = \Altumo\Validation\Numerics::assertDouble($value); $label = \Altumo\Validation\Strings::assertString($label); $item = new \stdClass(); $item->value = $value; $item->label = $label; $this->items[] = $item; return $this; }
/** * * @param string $text * @param int $type * * @throws \Exception if maximum number of items reached * * @return \sfAltumoPlugin\Geckoboard\Widget\TextWidget */ public function addItem($text, $type = self::TYPE_NONE) { $item = new \stdClass(); // pre-validate and set text $item->text = \Altumo\Validation\Strings::assertString($text); // pre-validate and set type $item->type = \Altumo\Validation\Numerics::assertInteger($type); if (count($this->getItems()) > self::MAXIMUM_ITEM_COUNT - 1) { throw new \Exception("Cannot add new item, maximum count reached"); } // add to items $this->items[] = $item; return $this; }
/** * Recursively merges many arrays into a single complex array. * This is a polyvariadic method (it takes an infinite number of parameters). * $array3 will overwrite(merge) $array2, then $array2 will overwrite(merge) * $array1, which will then be returned. * * Numeric keys are lost. This treats arrays with numeric values as lists, * pushing the new values onto the list, instead of taking array index 0 and * overwriting array index 0. For the opposite behavior: * @see \Altumo\Arrays\Arrays::mergeArraysRecursivelyAsHashes * * @signature array mergeArraysRecursivelyAsLists ( array $array1 [, array $array2 [, array $... ]] ) * * @param array $array1 * @param array $array2 * @param array $array3 * ... * * This method is similar to the php function "array_merge_recursive" except * that function a few quirks that I didn't like. * @see http://ca3.php.net/manual/en/function.array-merge-recursive.php * * @throws \Exception //if not all parameters were array * @throws \Exception //if there were no parameters passed * @return array */ public static function mergeArraysRecursivelyAsLists() { //get all of the arrays passed $arrays = func_get_args(); //validate that each of the parameters passed are arrays foreach ($arrays as $array) { if (!is_array($array)) { throw new \Exception('Not all parameters passed were arrays.'); } } $array_count = count($arrays); //if there aren't any arrays passed, throw exception if ($array_count == 0) { throw new \Exception('This function requires at least two arrays as parameters.'); } //if there's only one, return it if ($array_count == 1) { return $arrays[0]; } /** * start at the last two arrays passed and compare them, passing the * array values down to the first array. */ for ($x = $array_count - 2; $x >= 0; $x--) { //iterate over each of the keys foreach ($arrays[$x + 1] as $key => $value) { //if there is an intersecting key, that is an array, merge them recursively if (\Altumo\Validation\Numerics::isInteger($key)) { $arrays[$x][] = $arrays[$x + 1][$key]; } else { if (array_key_exists($key, $arrays[$x])) { if (is_array($arrays[$x][$key]) && is_array($arrays[$x + 1][$key])) { $arrays[$x][$key] = self::mergeArraysRecursivelyAsLists($arrays[$x][$key], $arrays[$x + 1][$key]); } else { $arrays[$x][$key] = $arrays[$x + 1][$key]; } } else { $arrays[$x][$key] = $arrays[$x + 1][$key]; } } } } return $arrays[0]; }
/** * Sets the amber value * * @param double $value * * @return \sfAltumoPlugin\Geckoboard\Widget\RAGNumbersWidget */ public function setAmberValue($value) { $this->amber_value = \Altumo\Validation\Numerics::assertDouble($value); return $this; }
/** * 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(); }
/** * Throws an exception if this is not a string (or castable as one) or is not * with the string length range provided. * Returns the string if it was cast as one. * * @param mixed $string * @param integer $min_length * @param integer $max_length * @param string $exception_message * //custom Exception message to throw. * If null, default message will be used. * * @throws Exception //if argument passed is not a string * @throws Exception //if the string length was not within the provided range * @return string */ public static function assertStringAndLength($string, $min_length = null, $max_length = null, $exception_message = null) { if (!is_string($string)) { if (is_null($exception_message)) { $exception_message = 'Value passed is not a string.'; } throw new \Exception($exception_message); } $length = strlen($string); if (!is_null($min_length)) { $min_length = \Altumo\Validation\Numerics::assertUnsignedInteger($min_length); if ($length < $min_length) { if (is_null($exception_message)) { $exception_message = 'String length was less than the minimum allowed length ( ' . $min_length . ' ).'; } throw new \Exception($exception_message); } } if (!is_null($max_length)) { $max_length = \Altumo\Validation\Numerics::assertUnsignedInteger($max_length); if ($length > $max_length) { if (is_null($exception_message)) { $exception_message = 'String length was greater than the maximum allowed length ( ' . $max_length . ' ).'; } throw new \Exception($exception_message); } } return $string; }
/** * Setter for the flags field on this \sfAltumoPlugin\Api\ApiFieldMap. * * @param integer $flags */ public function setFlags($flags) { $flags = \Altumo\Validation\Numerics::assertUnsignedInteger($flags); $this->flags = $flags; $this->validateFlags(); }
/** * (non-PHPdoc) * @see Geckoboard\Widget.AbstractWidget::getContent() */ public function getContent() { // make sure we have a primary value \Altumo\Validation\Numerics::assertDouble($this->getItem(), 'An item is required'); $wrapper = new \stdClass(); $wrapper->item = $this->getItem(); $min = new \stdClass(); $min->value = $this->getMinValue(); $min->text = $this->getMinText(); $wrapper->min = $min; $max = new \stdClass(); $max->value = $this->getMaxValue(); $max->text = $this->getMaxText(); $wrapper->max = $max; return $wrapper; }
/** * @param double $value * * @return \sfAltumoPlugin\Geckoboard\Widget\LineChartWidget * * @throws \Exception if item fails to validate */ public function addItem($value) { $this->items[] = \Altumo\Validation\Numerics::assertDouble($value); return $this; }
/** * Converts cents to dollars. * * * @param int $amount * * @return float */ static function getCentsAsDollars($amount) { $amount = \Altumo\Validation\Numerics::assertInteger($amount); return $amount / 100; }
/** * 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; }
/** * (non-PHPdoc) * @see Geckoboard\Widget.AbstractWidget::getContent() */ public function getContent() { // make sure we have a primary value \Altumo\Validation\Numerics::assertDouble($this->getPrimaryValue()); $wrapper = new \stdClass(); $wrapper->item = array(); $item = new \stdClass(); $item->value = $this->getPrimaryValue(); $item->text = $this->getPrimaryText(); $wrapper->item[] = $item; if ($this->getSecondaryValue()) { $item = new \stdClass(); $item->value = $this->getSecondaryValue(); $item->text = $this->getSecondaryText(); $wrapper->item[] = $item; } return $wrapper; }
/** * 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; }
/** * Enables testing mode, which means this Message will NOT be sent to the * recipients specified by "To". This Message will be sent to * $reroute_messages_to instead. * * @param string $reroute_messages_to * // phone numnber to reroute message to. * * @return \sfAltumoPlugin\Phone\TextMessage */ public function setTestingModeRerouteToNumber($reroute_messages_to) { $this->testing_mode_reroute_messages_to = \Altumo\Validation\Numerics::assertUnsignedInteger($reroute_messages_to, '$reroute_messages_to expects a phone number.'); return $this; }
/** * 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; }
/** * Sends a SIGKILL (9) to the process_id (pid) given. * * * @param int $process_id // system pid to kill * * @returns true if process was killed, false otherwise. */ public static function killProcess($process_id) { $process_id = \Altumo\Validation\Numerics::assertUnsignedInteger($process_id, '$process_id expects unsinged integer'); $result = \Altumo\Utils\Shell::runWithPipedOutput("kill -9 {$process_id}"); return $result == 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; }
/** * 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); } } }
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)); }
/** * 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); }
/** * Setter for the total_results field on this ApiPager. * * @param integer $total_results * @throws Exception if $total_results is not an unsigned integer */ public function setTotalResults($total_results) { $this->total_results = \Altumo\Validation\Numerics::assertUnsignedInteger($total_results); }
/** * Sets the offset for retrieved data * * @param integer $offset * * @return RawQuery Returns itself */ public function setOffset($offset) { $this->offset = \Altumo\Validation\Numerics::assertInteger($offset); return $this; }