包 | XS |
---|---|
继承关系 | class XSFieldScheme |
实现接口 | IteratorAggregate, Traversable |
版本 | 1.0.0 |
源代码 | sdk/php/lib/XSFieldScheme.class.php |
名称 | 描述 | 定义于 |
---|---|---|
__toString() | 将对象转换为配置文件字符串 | XSFieldScheme |
addField() | 添加字段到方案中 | XSFieldScheme |
checkValid() | 判断该字段方案是否有效、可用 | XSFieldScheme |
getAllFields() | 获取项目所有字段结构设置 | XSFieldScheme |
getField() | 获取项目字段元数据 | XSFieldScheme |
getFieldBody() | 获取内容字段元数据 | XSFieldScheme |
getFieldId() | 获取主键字段元数据 | XSFieldScheme |
getFieldTitle() | 获取标题字段元数据 | XSFieldScheme |
getIterator() | IteratorAggregate 接口, 以支持 foreach 遍历访问所有字段 | XSFieldScheme |
getVnoMap() | 获取所有字段的vno与名称映映射关系 | XSFieldScheme |
logger() | 获取搜索日志的字段方案 | XSFieldScheme |
public void __toString()
|
public function __toString()
{
$str = '';
foreach ($this->_fields as $field) {
$str .= $field->toConfig() . "\n";
}
return $str;
}
将对象转换为配置文件字符串
public void addField(mixed $field, array $config=NULL)
| ||
$field | mixed | 若类型为 XSFieldMeta 表示要添加的字段对象, 若类型为 string 表示字段名称, 连同 $config 参数一起创建字段对象 |
$config | array | 当 $field 参数为 string 时作为新建字段的配置内容 |
public function addField($field, $config = null)
{
if (!$field instanceof XSFieldMeta) {
$field = new XSFieldMeta($field, $config);
}
if (isset($this->_fields[$field->name])) {
throw new XSException('Duplicated field name: `' . $field->name . '\'');
}
if ($field->isSpeical()) {
if (isset($this->_typeMap[$field->type])) {
$prev = $this->_typeMap[$field->type];
throw new XSException('Duplicated ' . strtoupper($config['type']) . ' field: `' . $field->name . '\' and `' . $prev . '\'');
}
$this->_typeMap[$field->type] = $field->name;
}
$field->vno = ($field->type == XSFieldMeta::TYPE_BODY) ? self::MIXED_VNO : count($this->_vnoMap);
$this->_vnoMap[$field->vno] = $field->name;
// save field, ensure ID is the first field
if ($field->type == XSFieldMeta::TYPE_ID) {
$this->_fields = array_merge(array($field->name => $field), $this->_fields);
} else {
$this->_fields[$field->name] = $field;
}
}
添加字段到方案中 每个方案中的特殊类型字段都不能重复出现
public bool checkValid(bool $throw=false)
| ||
$throw | bool | 当没有通过检测时是否抛出异常, 默认为 false |
{return} | bool | 有效返回 true, 无效则返回 false |
public function checkValid($throw = false)
{
if (!isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {
if ($throw) {
throw new XSException('Missing field of type ID');
}
return false;
}
return true;
}
判断该字段方案是否有效、可用 每个方案必须并且只能包含一个类型为 ID 的字段
public XSFieldMeta[] getAllFields()
| ||
{return} | XSFieldMeta[] |
public function getAllFields()
{
return $this->_fields;
}
获取项目所有字段结构设置
public XSFieldMeta getField(mixed $name, bool $throw=true)
| ||
$name | mixed | 字段名称(string) 或字段序号(vno, int) |
$throw | bool | 当字段不存在时是否抛出异常, 默认为 true |
{return} | XSFieldMeta | 字段元数据对象, 若不存在则返回 false |
public function getField($name, $throw = true)
{
if (is_int($name)) {
if (!isset($this->_vnoMap[$name])) {
if ($throw === true) {
throw new XSException('Not exists field with vno: `' . $name . '\'');
}
return false;
}
$name = $this->_vnoMap[$name];
}
if (!isset($this->_fields[$name])) {
if ($throw === true) {
throw new XSException('Not exists field with name: `' . $name . '\'');
}
return false;
}
return $this->_fields[$name];
}
获取项目字段元数据
public XSFieldMeta getFieldBody()
| ||
{return} | XSFieldMeta | 类型为 BODY 的字段 |
public function getFieldBody()
{
if (isset($this->_typeMap[XSFieldMeta::TYPE_BODY])) {
$name = $this->_typeMap[XSFieldMeta::TYPE_BODY];
return $this->_fields[$name];
}
return false;
}
获取内容字段元数据
public XSFieldMeta getFieldId()
| ||
{return} | XSFieldMeta | 类型为 ID 的字段 |
public function getFieldId()
{
if (isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {
$name = $this->_typeMap[XSFieldMeta::TYPE_ID];
return $this->_fields[$name];
}
return false;
}
获取主键字段元数据
public XSFieldMeta getFieldTitle()
| ||
{return} | XSFieldMeta | 类型为 TITLE 的字段 |
public function getFieldTitle()
{
if (isset($this->_typeMap[XSFieldMeta::TYPE_TITLE])) {
$name = $this->_typeMap[XSFieldMeta::TYPE_TITLE];
return $this->_fields[$name];
}
foreach ($this->_fields as $name => $field) {
if ($field->type === XSFieldMeta::TYPE_STRING && !$field->isBoolIndex()) {
return $field;
}
}
return false;
}
获取标题字段元数据
public void getIterator()
|
public function getIterator()
{
return new ArrayIterator($this->_fields);
}
IteratorAggregate 接口, 以支持 foreach 遍历访问所有字段
public array getVnoMap()
| ||
{return} | array | vno为键, 字段名为值的数组 |
public function getVnoMap()
{
return $this->_vnoMap;
}
获取所有字段的vno与名称映映射关系
public static XSFieldScheme logger()
| ||
{return} | XSFieldScheme | 搜索日志字段方案 |
public static function logger()
{
if (self::$_logger === null) {
$scheme = new self;
$scheme->addField('id', array('type' => 'id'));
$scheme->addField('pinyin');
$scheme->addField('partial');
$scheme->addField('total', array('type' => 'numeric', 'index' => 'self'));
$scheme->addField('lastnum', array('type' => 'numeric', 'index' => 'self'));
$scheme->addField('currnum', array('type' => 'numeric', 'index' => 'self'));
$scheme->addField('currtag', array('type' => 'string'));
$scheme->addField('body', array('type' => 'body'));
self::$_logger = $scheme;
}
return self::$_logger;
}
获取搜索日志的字段方案
包 | XS |
---|---|
继承关系 | class XSFieldScheme |
实现接口 | IteratorAggregate, Traversable |
版本 | 1.0.0 |
源代码 | sdk/php/lib/XSFieldScheme.class.php |
名称 | 描述 | 定义于 |
---|---|---|
__toString() | 将对象转换为配置文件字符串 | XSFieldScheme |
addField() | 添加字段到方案中 | XSFieldScheme |
checkValid() | 判断该字段方案是否有效、可用 | XSFieldScheme |
getAllFields() | 获取项目所有字段结构设置 | XSFieldScheme |
getField() | 获取项目字段元数据 | XSFieldScheme |
getFieldBody() | 获取内容字段元数据 | XSFieldScheme |
getFieldId() | 获取主键字段元数据 | XSFieldScheme |
getFieldTitle() | 获取标题字段元数据 | XSFieldScheme |
getIterator() | IteratorAggregate 接口, 以支持 foreach 遍历访问所有字段 | XSFieldScheme |
getVnoMap() | 获取所有字段的vno与名称映映射关系 | XSFieldScheme |
logger() | 获取搜索日志的字段方案 | XSFieldScheme |
public void __toString()
|
public function __toString()
{
$str = '';
foreach ($this->_fields as $field) {
$str .= $field->toConfig() . "\n";
}
return $str;
}
将对象转换为配置文件字符串
public void addField(mixed $field, array $config=NULL)
| ||
$field | mixed | 若类型为 XSFieldMeta 表示要添加的字段对象, 若类型为 string 表示字段名称, 连同 $config 参数一起创建字段对象 |
$config | array | 当 $field 参数为 string 时作为新建字段的配置内容 |
public function addField($field, $config = null)
{
if (!$field instanceof XSFieldMeta) {
$field = new XSFieldMeta($field, $config);
}
if (isset($this->_fields[$field->name])) {
throw new XSException('Duplicated field name: `' . $field->name . '\'');
}
if ($field->isSpeical()) {
if (isset($this->_typeMap[$field->type])) {
$prev = $this->_typeMap[$field->type];
throw new XSException('Duplicated ' . strtoupper($config['type']) . ' field: `' . $field->name . '\' and `' . $prev . '\'');
}
$this->_typeMap[$field->type] = $field->name;
}
$field->vno = ($field->type == XSFieldMeta::TYPE_BODY) ? self::MIXED_VNO : count($this->_vnoMap);
$this->_vnoMap[$field->vno] = $field->name;
// save field, ensure ID is the first field
if ($field->type == XSFieldMeta::TYPE_ID) {
$this->_fields = array_merge(array($field->name => $field), $this->_fields);
} else {
$this->_fields[$field->name] = $field;
}
}
添加字段到方案中 每个方案中的特殊类型字段都不能重复出现
public bool checkValid(bool $throw=false)
| ||
$throw | bool | 当没有通过检测时是否抛出异常, 默认为 false |
{return} | bool | 有效返回 true, 无效则返回 false |
public function checkValid($throw = false)
{
if (!isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {
if ($throw) {
throw new XSException('Missing field of type ID');
}
return false;
}
return true;
}
判断该字段方案是否有效、可用 每个方案必须并且只能包含一个类型为 ID 的字段
public XSFieldMeta[] getAllFields()
| ||
{return} | XSFieldMeta[] |
public function getAllFields()
{
return $this->_fields;
}
获取项目所有字段结构设置
public XSFieldMeta getField(mixed $name, bool $throw=true)
| ||
$name | mixed | 字段名称(string) 或字段序号(vno, int) |
$throw | bool | 当字段不存在时是否抛出异常, 默认为 true |
{return} | XSFieldMeta | 字段元数据对象, 若不存在则返回 false |
public function getField($name, $throw = true)
{
if (is_int($name)) {
if (!isset($this->_vnoMap[$name])) {
if ($throw === true) {
throw new XSException('Not exists field with vno: `' . $name . '\'');
}
return false;
}
$name = $this->_vnoMap[$name];
}
if (!isset($this->_fields[$name])) {
if ($throw === true) {
throw new XSException('Not exists field with name: `' . $name . '\'');
}
return false;
}
return $this->_fields[$name];
}
获取项目字段元数据
public XSFieldMeta getFieldBody()
| ||
{return} | XSFieldMeta | 类型为 BODY 的字段 |
public function getFieldBody()
{
if (isset($this->_typeMap[XSFieldMeta::TYPE_BODY])) {
$name = $this->_typeMap[XSFieldMeta::TYPE_BODY];
return $this->_fields[$name];
}
return false;
}
获取内容字段元数据
public XSFieldMeta getFieldId()
| ||
{return} | XSFieldMeta | 类型为 ID 的字段 |
public function getFieldId()
{
if (isset($this->_typeMap[XSFieldMeta::TYPE_ID])) {
$name = $this->_typeMap[XSFieldMeta::TYPE_ID];
return $this->_fields[$name];
}
return false;
}
获取主键字段元数据
public XSFieldMeta getFieldTitle()
| ||
{return} | XSFieldMeta | 类型为 TITLE 的字段 |
public function getFieldTitle()
{
if (isset($this->_typeMap[XSFieldMeta::TYPE_TITLE])) {
$name = $this->_typeMap[XSFieldMeta::TYPE_TITLE];
return $this->_fields[$name];
}
foreach ($this->_fields as $name => $field) {
if ($field->type === XSFieldMeta::TYPE_STRING && !$field->isBoolIndex()) {
return $field;
}
}
return false;
}
获取标题字段元数据
public void getIterator()
|
public function getIterator()
{
return new ArrayIterator($this->_fields);
}
IteratorAggregate 接口, 以支持 foreach 遍历访问所有字段
public array getVnoMap()
| ||
{return} | array | vno为键, 字段名为值的数组 |
public function getVnoMap()
{
return $this->_vnoMap;
}
获取所有字段的vno与名称映映射关系
public static XSFieldScheme logger()
| ||
{return} | XSFieldScheme | 搜索日志字段方案 |
public static function logger()
{
if (self::$_logger === null) {
$scheme = new self;
$scheme->addField('id', array('type' => 'id'));
$scheme->addField('pinyin');
$scheme->addField('partial');
$scheme->addField('total', array('type' => 'numeric', 'index' => 'self'));
$scheme->addField('lastnum', array('type' => 'numeric', 'index' => 'self'));
$scheme->addField('currnum', array('type' => 'numeric', 'index' => 'self'));
$scheme->addField('currtag', array('type' => 'string'));
$scheme->addField('body', array('type' => 'body'));
self::$_logger = $scheme;
}
return self::$_logger;
}
获取搜索日志的字段方案
留下一条评论吧!
请到论坛 登录 后刷新本页面!