Example #1
0
	/**
	 * Get Message
	 *
	 * Shows the error message which can be taken from loaded language file.
	 *
	 * @param	string	HTML to prefix error message
	 * @param	string	HTML to postfix error message
	 * @param	string	Message to use, or false to try and load it from Lang class
	 * @return	string
	 */
	public function get_message($msg = false)
	{
		$msg = $msg === false
				? __('validation.'.$this->callback) ?: __('validation.'.Arr::element(explode(':', $this->callback), 0))
				: $msg;
		if ($msg == false)
		{
			return 'Validation rule '.$this->callback.' failed for '.$this->field->label;
		}

		// to safe some performance when there are no variables in the $msg
		if (strpos(':', $msg) !== false)
		{
			return $msg;
		}

		$find			= array(':field', ':label', ':value', ':rule');
		$replace		= array($this->field->key, $this->field->label, $this->value, $this->callback);
		foreach($this->params as $key => $val)
		{
			$find[]		= ':param:'.$key;
			$replace[]	= $val;
		}

		return str_replace($find, $replace, $msg);
	}
Example #2
0
 /**
  * Tests Arr::element()
  * 
  * @test
  */
 public function test_element()
 {
     $person = array("name" => "Jack", "age" => "21", "location" => array("city" => "Pittsburgh", "state" => "PA", "country" => "US"));
     $expected = "Jack";
     $output = Arr::element($person, "name", "Unknown Name");
     $this->assertEquals($expected, $output);
     $expected = "Unknown job";
     $output = Arr::element($person, "job", "Unknown job");
     $this->assertEquals($expected, $output);
     $expected = "Pittsburgh";
     $output = Arr::element($person, "location.city", "Unknown City");
     $this->assertEquals($expected, $output);
 }
Example #3
0
 /**
  * Get Message
  *
  * Shows the error message which can be taken from loaded language file.
  *
  * @param	string	HTML to prefix error message
  * @param	string	HTML to postfix error message
  * @param	string	Message to use, or false to try and load it from Lang class
  * @return	string
  */
 public function get_message($msg = false, $open = '', $close = '')
 {
     $open = \Config::get('validation.open_single_error', $open);
     $close = \Config::get('validation.close_single_error', $close);
     if ($msg === false) {
         $msg = $this->field->fieldset()->validation()->get_message($this->rule);
         $msg = $msg === false ? __('validation.' . $this->rule) ?: __('validation.' . Arr::element(explode(':', $this->rule), 0)) : $msg;
     }
     if ($msg == false) {
         return $open . 'Validation rule ' . $this->rule . ' failed for ' . $this->field->label . $close;
     }
     // only parse when there's tags in the message
     return $open . (strpos($msg, ':') === false ? $msg : $this->_replace_tags($msg)) . $close;
 }
Example #4
0
 /**
  * Get Message
  *
  * Shows the error message which can be taken from loaded language file.
  *
  * @param	string	HTML to prefix error message
  * @param	string	HTML to postfix error message
  * @param	string	Message to use, or false to try and load it from Lang class
  * @return	string
  */
 public function get_message($msg = false, $open = null, $close = null)
 {
     $open = \Config::get('validation.open_single_error', '');
     $close = \Config::get('validation.close_single_error', '');
     if ($msg === false) {
         $msg = $this->field->fieldset()->validation()->get_message($this->callback);
         $msg = $msg === false ? __('validation.' . $this->callback) ?: __('validation.' . Arr::element(explode(':', $this->callback), 0)) : $msg;
     }
     if ($msg == false) {
         return $open . 'Validation rule ' . $this->callback . ' failed for ' . $this->field->label . $close;
     }
     // to safe some performance when there are no variables in the $msg
     if (strpos(':', $msg) !== false) {
         return $open . $msg . $close;
     }
     $find = array(':field', ':label', ':value', ':rule');
     $replace = array($this->field->name, $this->field->label, $this->value, $this->callback);
     foreach ($this->params as $key => $val) {
         $find[] = ':param:' . ($key + 1);
         $replace[] = $val;
     }
     return $open . str_replace($find, $replace, $msg) . $close;
 }
Example #5
0
 /**
  * Converts a file size number to a byte value. File sizes are defined in
  * the format: SB, where S is the size (1, 8.5, 300, etc.) and B is the
  * byte unit (K, MiB, GB, etc.). All valid byte units are defined in
  * static::$byte_units
  *
  * Usage:
  * <code>
  * echo Num::bytes('200K');  // 204800
  * echo static::bytes('5MiB');  // 5242880
  * echo static::bytes('1000');  // 1000
  * echo static::bytes('2.5GB'); // 2684354560
  * </code>
  *
  * @author     Kohana Team
  * @copyright  (c) 2009-2011 Kohana Team
  * @license    http://kohanaframework.org/license
  * @param      string   file size in SB format
  * @return     float
  */
 public static function bytes($size = 0)
 {
     // Prepare the size
     $size = trim((string) $size);
     // Construct an OR list of byte units for the regex
     $accepted = implode('|', array_keys(static::$byte_units));
     // Construct the regex pattern for verifying the size format
     $pattern = '/^([0-9]+(?:\\.[0-9]+)?)(' . $accepted . ')?$/Di';
     // Verify the size format and store the matching parts
     if (!preg_match($pattern, $size, $matches)) {
         throw new Exception('The byte unit size, "' . $size . '", is improperly formatted.');
     }
     // Find the float value of the size
     $size = (double) $matches[1];
     // Find the actual unit, assume B if no unit specified
     $unit = Arr::element($matches, 2, 'B');
     // Convert the size into bytes
     $bytes = $size * pow(2, static::$byte_units[$unit]);
     return $bytes;
 }
Example #6
0
 /**
  * Shows a small notice error, only when not in production or when forced.
  * This is used by several libraries to notify the developer of certain things.
  * 
  * @param   string  $msg          the message to display
  * @param   bool    $always_show  whether to force display the notice or not
  * @return  void
  */
 public static function notice($msg, $always_show = false)
 {
     if (!$always_show && (\Fuel::$env == \Fuel::PRODUCTION || \Config::get('errors.notices', true) === false)) {
         return;
     }
     $trace = array_merge(array('file' => '(unknown)', 'line' => '(unknown)'), \Arr::element(debug_backtrace(), 1));
     logger(Fuel::L_DEBUG, 'Notice - ' . $msg . ' in ' . $trace['file'] . ' on line ' . $trace['line']);
     $data['message'] = $msg;
     $data['type'] = 'Notice';
     $data['filepath'] = \Fuel::clean_path($trace['file']);
     $data['line'] = $trace['line'];
     $data['function'] = $trace['function'];
     echo \View::factory('errors' . DS . 'php_short', $data, false);
 }
Example #7
0
 /**
  * Tests Arr::element()
  *
  * @test
  * @dataProvider person_provider
  */
 public function test_element_when_dot_notated_key_is_not_array($person)
 {
     $expected = "Unknown Name";
     $output = Arr::element($person, 'foo.first', 'Unknown Name');
     $this->assertEquals($expected, $output);
 }
Example #8
0
File: test.php Project: ralf57/fuel
<?php

include 'fuel/core/classes/autoloader.php';

Fuel\Core\Autoloader::register();
Fuel\Core\Autoloader::add_classes(array(
	'Fuel\\Core\\Arr'	=> __DIR__.'/fuel/core/classes/arr.php',
));

$ar = array('test' => 'foo', 'bar' => 'baz');

var_dump(Arr::element($ar, 'bar'));
Example #9
0
 /**
  * Filters a value for a specific column
  *
  * @param  string $field  The column name
  * @param  string $value  The value to filter
  * @return string
  *
  * @package    Kohana/ORM
  * @author     Kohana Team
  * @copyright  (c) 2007-2010 Kohana Team
  * @license    http://kohanaframework.org/license
  */
 protected function run_filters($name, $value)
 {
     if (!isset($this->_fields[$name]['filters'])) {
         return $value;
     }
     // Bind the field name and model so they can be used in the filter method
     $_bound = array(':field' => $name, ':model' => $this);
     foreach ($this->_fields[$name]['filters'] as $array) {
         // Value needs to be bound inside the loop so we are always using the
         // version that was modified by the filters that already ran
         $_bound[':value'] = $value;
         // Filters are defined as array($filter, $params)
         $filter = $array[0];
         $params = Arr::element($array, 1, array(':value'));
         foreach ($params as $key => $param) {
             if (is_string($param) and array_key_exists($param, $_bound)) {
                 // Replace with bound value
                 $params[$key] = $_bound[$param];
             }
         }
         if (is_array($filter) or !is_string($filter)) {
             // This is either a callback as an array or a lambda
             $value = call_user_func_array($filter, $params);
         } elseif (strpos($filter, '::') === FALSE) {
             // Use a function call
             $function = new ReflectionFunction($filter);
             // Call $function($this[$field], $param, ...) with Reflection
             $value = $function->invokeArgs($params);
         } else {
             // Split the class and method of the rule
             list($class, $method) = explode('::', $filter, 2);
             // Use a static method call
             $method = new ReflectionMethod($class, $method);
             // Call $Class::$method($this[$field], $param, ...) with Reflection
             $value = $method->invokeArgs(NULL, $params);
         }
     }
     return $value;
 }
	/**
	 * Get a key or a the whole configuration for called class.
	 * Search powered by \Fuel\Core\Arr::element();
	 * 
	 * @access public
	 * @static
	 * @param string $key (default: '')
	 * @return void
	 */
	public static function get_config($key = '')
	{
		if (! $key) return static::$config;
		
		return \Arr::element(static::$config, $key);
	}
Example #11
0
	/**
	 * Get Message
	 *
	 * Shows the error message which can be taken from loaded language file.
	 *
	 * @param	string	HTML to prefix error message
	 * @param	string	HTML to postfix error message
	 * @param	string	Message to use, or false to try and load it from Lang class
	 * @return	string
	 */
	public function get_message($msg = false, $open = null, $close = null)
	{
		$open   = \Config::get('validation.open_single_error', '');
		$close  = \Config::get('validation.close_single_error', '');

		if ($msg === false)
		{
			$msg = $this->field->fieldset()->validation()->get_message($this->callback);
			$msg = $msg === false
				? __('validation.'.$this->callback) ?: __('validation.'.Arr::element(explode(':', $this->callback), 0))
				: $msg;
		}
		if ($msg == false)
		{
			return $open.'Validation rule '.$this->callback.' failed for '.$this->field->label.$close;
		}

		// to safe some performance when there are no variables in the $msg
		if (strpos(':', $msg) !== false)
		{
			return $open.$msg.$close;
		}

		$value    = is_array($this->value) ? implode(', ', $this->value) : $this->value;
		$find     = array(':field', ':label', ':value', ':rule');
		$label    = is_array($this->field->label) ? $this->field->label['label'] : $this->field->label;
		if (\Config::get('validation.quote_labels', false))
		{
			// put the label in quotes if it contains spaces
			strpos($label, ' ') !== false and $label = '"'.$label.'"';
		}
		$replace  = array($this->field->name, $label, $value, $this->callback);
		foreach($this->params as $key => $val)
		{
			$find[]		= ':param:'.($key + 1);
			$replace[]	= $val;
		}

		return $open.str_replace($find, $replace, $msg).$close;
	}