Refactor separator check

Add InvalidArgument Exception if name is invalid
Remove unused method is_distinct
This commit is contained in:
Romain Neutron
2012-03-21 15:40:42 +01:00
parent 28d61125d3
commit f1921e5aff

View File

@@ -200,19 +200,8 @@ class databox_field implements cache_cacheableInterface
$this->dces_element = new $dc_class(); $this->dces_element = new $dc_class();
} }
if (!$this->multi) $this->separator = self::checkMultiSeparator($row['separator'], $this->multi);
{
$separator = "";
}
else
{
$separator = $row['separator'];
if (strpos($separator, ';') === false)
$separator .= ';';
}
$this->separator = $separator;
$this->thumbtitle = $row['thumbtitle']; $this->thumbtitle = $row['thumbtitle'];
return $this; return $this;
@@ -441,7 +430,14 @@ class databox_field implements cache_cacheableInterface
{ {
$previous_name = $this->name; $previous_name = $this->name;
$this->name = self::generateName($name); $name = self::generateName($name);
if($name === '')
{
throw new \Exception_InvalidArgument();
}
$this->name = $name;
if ($this->name !== $previous_name) if ($this->name !== $previous_name)
{ {
@@ -599,6 +595,8 @@ class databox_field implements cache_cacheableInterface
$this->multi = $multi; $this->multi = $multi;
$this->set_separator(';');
return $this; return $this;
} }
@@ -645,14 +643,26 @@ class databox_field implements cache_cacheableInterface
*/ */
public function set_separator($separator) public function set_separator($separator)
{ {
if (strpos($separator, ';') === false) $this->separator = self::checkMultiSeparator($separator, $this->multi);
$separator .= ';';
$this->separator = $separator;
return $this; return $this;
} }
protected static function checkMultiSeparator($separator, $multi)
{
if (!$multi)
{
return '';
}
if (strpos($separator, ';') === false)
{
$separator .= ';';
}
return $separator;
}
/** /**
* *
* @param string $type * @param string $type
@@ -746,15 +756,6 @@ class databox_field implements cache_cacheableInterface
return $this->multi; return $this->multi;
} }
/**
*
* @return boolean
*/
public function is_distinct()
{
return true;
}
/** /**
* *
* @return boolean * @return boolean
@@ -827,12 +828,19 @@ class databox_field implements cache_cacheableInterface
(`id`, `name`, `src`, `readonly`, `indexable`, `type`, `tbranch`, (`id`, `name`, `src`, `readonly`, `indexable`, `type`, `tbranch`,
`thumbtitle`, `multi`, `business`, `thumbtitle`, `multi`, `business`,
`report`, `sorter`) `report`, `sorter`)
VALUES (null, :name, '', 0, 1, 'text', '', VALUES (null, :name, '', 0, 1, 'string', '',
null, 0, null, 0,
0, 1, :sorter)"; 0, 1, :sorter)";
$name = self::generateName($name);
if($name === '')
{
throw new \Exception_InvalidArgument();
}
$stmt = $databox->get_connection()->prepare($sql); $stmt = $databox->get_connection()->prepare($sql);
$stmt->execute(array(':name' => self::generateName($name), ':sorter' => $sorter)); $stmt->execute(array(':name' => $name, ':sorter' => $sorter));
$id = $databox->get_connection()->lastInsertId(); $id = $databox->get_connection()->lastInsertId();
$stmt->closeCursor(); $stmt->closeCursor();