public function Test_for_element_size() { $element = 'check_this_size'; $expected_value = 15; $this->assertEqual(Ak::size($element), $expected_value); $element = '123'; $expected_value = 3; $this->assertEqual(Ak::size($element), $expected_value); $element = 123; $expected_value = 123; $this->assertEqual(Ak::size($element), $expected_value); $element = array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => array('E', 'F')); $expected_value = 5; $this->assertEqual(Ak::size($element), $expected_value); }
function &singleton($class_name, &$arguments) { static $instances; if (!isset($instances[$class_name])) { if (is_object($arguments)) { $instances[$class_name] =& new $class_name($arguments); } else { if (Ak::size($arguments) > 0) { eval("\$instances[\$class_name] =& new \$class_name(" . var_export($arguments, true) . "); "); } else { $instances[$class_name] =& new $class_name(); } } $instances[$class_name]->__singleton_id = md5(microtime() . rand(1000, 9000)); } return $instances[$class_name]; }
/** * Send binary data to the user as a file download. May set content type, apparent file name, * and specify whether to show data inline or download as an attachment. * * Options: * * <tt>filename</tt> - Suggests a filename for the browser to use. * * <tt>type</tt> - specifies an HTTP content type. * Defaults to 'application/octet-stream'. * * <tt>disposition</tt> - specifies whether the file will be shown inline or downloaded. * Valid values are 'inline' and 'attachment' (default). * * Generic data download: * sendData($buffer) * * Download a dynamically-generated tarball: * sendData(Ak::compress('dir','tgz'), array('filename' => 'dir.tgz')); * * Display an image Active Record in the browser: * sendData($image_data, array('type' =>Ak::mime_content_type('image_name.png'), 'disposition' => 'inline')); * * See +sendFile+ for more information on HTTP Content-* headers and caching. */ function sendData($data, $options = array()) { $options['length'] = empty($options['length']) ? Ak::size($data) : $options['length']; $this->_sendFileHeaders($options); $this->performed_render = false; $this->renderText($data); }
function test_implicitly_multipart_messages_with_custom_order() { $TestMailer =& new TestMailer(); $this->assertTrue($Mail = $TestMailer->create('implicitly_multipart_example', $this->recipient, null, array("text/yaml", "text/plain"))); $this->assertEqual(3, Ak::size($Mail->parts)); $this->assertEqual("text/html", $Mail->parts[0]->content_type); $this->assertEqual("text/plain", $Mail->parts[1]->content_type); $this->assertEqual("text/yaml", $Mail->parts[2]->content_type); }
/** * Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: * * class Person extends ActiveRecord * { * function validate() * { * $this->validatesLengthOf('first_name', array('maximum'=>30)); * $this->validatesLengthOf('last_name', array('maximum'=>30,'message'=> "less than %d if you don't mind")); * $this->validatesLengthOf('last_name', array('within'=>array(7, 32))); * $this->validatesLengthOf('last_name', array('in'=>array(6, 20), 'too_long' => "pick a shorter name", 'too_short' => "pick a longer name")); * $this->validatesLengthOf('fav_bra_size', array('minimum'=>1, 'too_short'=>"please enter at least %d character")); * $this->validatesLengthOf('smurf_leader', array('is'=>4, 'message'=>"papa is spelled with %d characters... don't play me.")); * } * } * * NOTE: Be aware that $this->validatesLengthOf('field', array('is'=>5)); Will match a string containing 5 characters (Ie. "Spain"), an integer 5, and an array with 5 elements. You must supply additional checking to check for appropriate types. * * Configuration options: * <tt>minimum</tt> - The minimum size of the attribute * <tt>maximum</tt> - The maximum size of the attribute * <tt>is</tt> - The exact size of the attribute * <tt>within</tt> - A range specifying the minimum and maximum size of the attribute * <tt>in</tt> - A synonym(or alias) for :within * <tt>allow_null</tt> - Attribute may be null; skip validation. * * <tt>too_long</tt> - The error message if the attribute goes over the maximum (default "is" "is too long (max is %d characters)") * <tt>too_short</tt> - The error message if the attribute goes under the minimum (default "is" "is too short (min is %d characters)") * <tt>wrong_length</tt> - The error message if using the "is" method and the attribute is the wrong size (default "is" "is the wrong length (should be %d characters)") * <tt>message</tt> - The error message to use for a "minimum", "maximum", or "is" violation. An alias of the appropriate too_long/too_short/wrong_length message */ function validatesLengthOf($attribute_names, $options = array()) { // Merge given options with defaults. $default_options = array( 'too_long' => $this->_defaultErrorMessages['too_long'], 'too_short' => $this->_defaultErrorMessages['too_short'], 'wrong_length' => $this->_defaultErrorMessages['wrong_length'], 'allow_null' => false ); $range_options = array(); foreach ($options as $k=>$v){ if(in_array($k,array('minimum','maximum','is','in','within'))){ $range_options[$k] = $v; $option = $k; $option_value = $v; } } // Ensure that one and only one range option is specified. switch (count($range_options)) { case 0: trigger_error(Ak::t('Range unspecified. Specify the "within", "maximum", "minimum, or "is" option.'), E_USER_ERROR); return false; break; case 1: $options = array_merge($default_options, $options); break; default: trigger_error(Ak::t('Too many range options specified. Choose only one.'), E_USER_ERROR); return false; break; } switch ($option) { case 'within': case 'in': if(empty($option_value) || !is_array($option_value) || count($option_value) != 2 || !is_numeric($option_value[0]) || !is_numeric($option_value[1])){ trigger_error(Ak::t('%option must be a Range (array(min, max))',array('%option',$option)), E_USER_ERROR); return false; } $attribute_names = Ak::toArray($attribute_names); foreach ($attribute_names as $attribute_name){ if((!empty($option['allow_null']) && !isset($this->$attribute_name)) || (Ak::size($this->$attribute_name)) < $option_value[0]){ $this->addError($attribute_name, sprintf($options['too_short'], $option_value[0])); }elseif((!empty($option['allow_null']) && !isset($this->$attribute_name)) || (Ak::size($this->$attribute_name)) > $option_value[1]){ $this->addError($attribute_name, sprintf($options['too_long'], $option_value[1])); } } break; case 'is': case 'minimum': case 'maximum': if(empty($option_value) || !is_numeric($option_value) || $option_value <= 0){ trigger_error(Ak::t('%option must be a nonnegative Integer',array('%option',$option_value)), E_USER_ERROR); return false; } // Declare different validations per option. $validity_checks = array('is' => "==", 'minimum' => ">=", 'maximum' => "<="); $message_options = array('is' => 'wrong_length', 'minimum' => 'too_short', 'maximum' => 'too_long'); $message = sprintf(!empty($options['message']) ? $options['message'] : $options[$message_options[$option]],$option_value); $attribute_names = Ak::toArray($attribute_names); foreach ($attribute_names as $attribute_name){ if((!$options['allow_null'] && !isset($this->$attribute_name)) || eval("return !(".Ak::size(@$this->$attribute_name)." {$validity_checks[$option]} $option_value);")){ $this->addError($attribute_name, $message); } } break; default: break; } return true; }
function Test_of_directChildren() { $Categories =& new AkTestNestedCategory(); $this->assertFalse($Categories->nested_set->directChildren()); $Categories = $Categories->find('first', array('conditions' => array('description = ?', 'Category 1.5'))); $ChildNodes = $Categories->nested_set->directChildren(); $this->assertEqual(Ak::size($ChildNodes), $Categories->nested_set->childrenCount()); $Categories =& new AkTestNestedCategory(1); $ChildNodes = $Categories->nested_set->directChildren(); $this->assertEqual(Ak::size($ChildNodes), 5); $Categories = $Categories->find('first', array('conditions' => array('description = ?', 'Category 1'))); $ChildNodes = $Categories->nested_set->directChildren(); $this->assertEqual(Ak::size($ChildNodes), 10); $Categories->nested_set->setScopeCondition(" department = 'sales' "); $this->assertFalse($Categories->nested_set->allChildren()); }