【Laravel3.0.0源码阅读分析】消息类message.php
作者:互联网
<?php namespace Laravel;
// 消息类
class Messages {
/**
* All of the registered messages.
* 所有已注册的消息。
* @var array
*/
public $messages;
/**
* Create a new Messages instance.
* 创建一个新的消息实例。
* @param array $messages
* @return void
*/
public function __construct($messages = array())
{
$this->messages = (array) $messages;
}
/**
* Add a message to the collector.
* 向收集器添加消息。
* <code>
* // Add a message for the e-mail attribute
* $messages->add('email', 'The e-mail address is invalid.');
* </code>
*
* @param string $key
* @param string $message
* @return void
*/
public function add($key, $message)
{
if ($this->unique($key, $message)) $this->messages[$key][] = $message;
}
/**
* Determine if a key and message combination already exists.
* 确定键和消息组合是否已经存在。
* @param string $key
* @param string $message
* @return bool
*/
protected function unique($key, $message)
{
return ! isset($this->messages[$key]) or ! in_array($message, $this->messages[$key]);
}
/**
* Determine if messages exist for a given key.
* 确定给定键是否存在消息
* @param string $key
* @return bool
*/
public function has($key)
{
return $this->first($key) !== '';
}
/**
* Get the first message from the container for a given key.
* 从容器中获取给定键的第一条消息。
* <code>
* // Echo the first message for the e-mail attribute
* echo $messages->first('email');
*
* // Format the first message for the e-mail attribute
* echo $messages->first('email', '<p>:message</p>');
* </code>
*
* @param string $key
* @param string $format
* @return string
*/
public function first($key, $format = ':message')
{
return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
}
/**
* Get all of the messages from the container for a given key.
* 从容器中获取给定键的所有消息。
* <code>
* // Echo all of the messages for the e-mail attribute
* echo $messages->get('email');
*
* // Format all of the messages for the e-mail attribute
* echo $messages->get('email', '<p>:message</p>');
* </code>
*
* @param string $key
* @param string $format
* @return array
*/
public function get($key, $format = ':message')
{
if (array_key_exists($key, $this->messages))
{
return $this->format($this->messages[$key], $format);
}
return array();
}
/**
* Get all of the messages for every key in the container.
* 获取容器中每个键的所有消息。
* <code>
* // Get all of the messages in the collector
* $all = $messages->all();
*
* // Format all of the messages in the collector
* $all = $messages->all('<p>:message</p>');
* </code>
*
* @param string $format
* @return array
*/
public function all($format = ':message')
{
$all = array();
foreach ($this->messages as $messages)
{
$all = array_merge($all, $this->format($messages, $format));
}
return $all;
}
/**
* Format an array of messages.
* 格式化消息数组。
* @param array $messages
* @param string $format
* @return array
*/
protected function format($messages, $format)
{
$messages = (array) $messages;
foreach ($messages as $key => &$message)
{
$message = str_replace(':message', $message, $format);
}
return $messages;
}
}
github地址: https://github.com/liu-shilong/laravel3-scr
标签:return,format,messages,Laravel3.0,源码,key,message,array 来源: https://blog.csdn.net/qq2942713658/article/details/117385170