Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions core/Cache/PeclMemcached.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ class PeclMemcached extends CachePeer
* @return PeclMemcached
**/
public static function create(
$host = Memcached::DEFAULT_HOST,
$port = Memcached::DEFAULT_PORT,
$connectTimeout = PeclMemcached::DEFAULT_TIMEOUT
$host = self::DEFAULT_HOST,
$port = self::DEFAULT_PORT,
$connectTimeout = self::DEFAULT_TIMEOUT
)
{
return new self($host, $port, $connectTimeout);
}

public function __construct(
$host = Memcached::DEFAULT_HOST,
$port = Memcached::DEFAULT_PORT,
$connectTimeout = PeclMemcached::DEFAULT_TIMEOUT
$host = self::DEFAULT_HOST,
$port = self::DEFAULT_PORT,
$connectTimeout = self::DEFAULT_TIMEOUT
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется тоже не из этого реквеста.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

упс ))) я просто прогонял тесты и эти ошибки задрали уже, случайно запушил сюда!

Надо бы их в отдельном реквесте пофиксить!

{
$this->host = $host;
Expand Down
172 changes: 59 additions & 113 deletions core/Form/Form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
**/
final class Form extends RegulatedForm
{
const WRONG = 0x0001;
const MISSING = 0x0002;

private $errors = array();
/**
* @deprecated
*/
const WRONG = BasePrimitive::WRONG;
/**
* @deprecated
*/
const MISSING = BasePrimitive::MISSING;

private $labels = array();
private $describedLabels = array();

Expand All @@ -40,22 +45,27 @@ public static function create()

public function getErrors()
{
return array_merge($this->errors, $this->violated);
$errors = array();

foreach ($this->primitives as $name => $prm)
if ($error = $prm->getError())
$errors[$name] = $error;

return $errors;
}

public function hasError($name)
{
return array_key_exists($name, $this->errors)
|| array_key_exists($name, $this->violated);
return array_key_exists($name, $this->getErrors());
}

public function getError($name)
{
if (array_key_exists($name, $this->errors)) {
return $this->errors[$name];
} elseif (array_key_exists($name, $this->violated)) {
return $this->violated[$name];
}
$errors = $this->getErrors();

if (array_key_exists($name, $errors))
return $errors[$name];

return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

правильно. ООП-шно. но hasError не имеет, более, смысла в контексте этих изменений, ибо если err !== null вернуть err иначе вернуть null =)

}

Expand Down Expand Up @@ -87,8 +97,8 @@ public function getInnerErrors()
**/
public function dropAllErrors()
{
$this->errors = array();
$this->violated = array();
foreach ($this->primitives as $prm)
$prm->dropError();

return $this;
}
Expand Down Expand Up @@ -122,7 +132,7 @@ public function disableImportFiltering()
**/
public function markMissing($primitiveName)
{
return $this->markCustom($primitiveName, Form::MISSING);
return $this->markCustom($primitiveName, BasePrimitive::MISSING);
}

/**
Expand All @@ -132,32 +142,16 @@ public function markMissing($primitiveName)
**/
public function markWrong($name)
{
if (isset($this->primitives[$name]))
$this->errors[$name] = self::WRONG;
elseif (isset($this->rules[$name]))
$this->violated[$name] = self::WRONG;
else
throw new MissingElementException(
$name.' does not match known primitives or rules'
);

return $this;
return $this->markCustom($name, BasePrimitive::WRONG);
}

/**
* @return Form
**/
public function markGood($primitiveName)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может быть заодно тогда привести к одному виду аргументы методов класса? $primitiveName или $name ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Согласен!

if (isset($this->primitives[$primitiveName]))
unset($this->errors[$primitiveName]);
elseif (isset($this->rules[$primitiveName]))
unset($this->violated[$primitiveName]);
else
throw new MissingElementException(
$primitiveName.' does not match known primitives or rules'
);

$this->get($primitiveName)->dropError();

return $this;
}

Expand All @@ -166,11 +160,9 @@ public function markGood($primitiveName)
*
* @return Form
**/
public function markCustom($primitiveName, $customMark)
public function markCustom($name, $customMark)
{
Assert::isInteger($customMark);

$this->errors[$this->get($primitiveName)->getName()] = $customMark;
$this->get($name)->setError($customMark);

return $this;
}
Expand All @@ -193,59 +185,20 @@ public function getTextualErrors()

public function getTextualErrorFor($name)
{
if (
isset(
$this->violated[$name],
$this->labels[$name][$this->violated[$name]]
)
)
return $this->labels[$name][$this->violated[$name]];
elseif (
isset(
$this->errors[$name],
$this->labels[$name][$this->errors[$name]]
)
)
return $this->labels[$name][$this->errors[$name]];
else
return null;
return $this->get($name)->getActualErrorLabel();
}

public function getErrorDescriptionFor($name)
{
if (
isset(
$this->violated[$name],
$this->describedLabels[$name][$this->violated[$name]]
)
)
return $this->describedLabels[$name][$this->violated[$name]];
elseif (
isset(
$this->errors[$name],
$this->describedLabels[$name][$this->errors[$name]]
)
)
return $this->describedLabels[$name][$this->errors[$name]];
else
return null;
return $this->get($name)->getActualErrorDescription();
}

/**
* @return Form
**/
public function addErrorDescription($name, $errorType, $description)
{

if (
!isset($this->rules[$name])
&& !$this->get($name)->getName()
)
throw new MissingElementException(
"knows nothing about '{$name}'"
);

$this->describedLabels[$name][$errorType] = $description;
$this->get($name)->setErrorDescription($errorType, $description);

return $this;
}
Expand All @@ -255,15 +208,15 @@ public function addErrorDescription($name, $errorType, $description)
**/
public function addWrongLabel($primitiveName, $label)
{
return $this->addErrorLabel($primitiveName, Form::WRONG, $label);
return $this->addErrorLabel($primitiveName, BasePrimitive::WRONG, $label);
}

/**
* @return Form
**/
public function addMissingLabel($primitiveName, $label)
{
return $this->addErrorLabel($primitiveName, Form::MISSING, $label);
return $this->addErrorLabel($primitiveName, BasePrimitive::MISSING, $label);
}

/**
Expand All @@ -276,12 +229,12 @@ public function addCustomLabel($primitiveName, $customMark, $label)

public function getWrongLabel($primitiveName)
{
return $this->getErrorLabel($primitiveName, Form::WRONG);
return $this->getErrorLabel($primitiveName, BasePrimitive::WRONG);
}

public function getMissingLabel($primitiveName)
{
return $this->getErrorLabel($primitiveName, Form::MISSING);
return $this->getErrorLabel($primitiveName, BasePrimitive::MISSING);
}

/**
Expand All @@ -290,7 +243,7 @@ public function getMissingLabel($primitiveName)
public function import($scope)
{
foreach ($this->primitives as $prm)
$this->importPrimitive($scope, $prm);
$this->importPrimitive($scope, $prm);

return $this;
}
Expand Down Expand Up @@ -389,6 +342,13 @@ public function getProto()
**/
private function importPrimitive($scope, BasePrimitive $prm)
{
/**
* Because we check its lazy
* @see RegulatedForm::checkRules
*/
if($prm instanceof PrimitiveRule)
return $this;

if (!$this->importFiltering) {
if ($prm instanceof FiltrablePrimitive) {

Expand Down Expand Up @@ -421,27 +381,27 @@ private function importPrimitive($scope, BasePrimitive $prm)
**/
private function checkImportResult(BasePrimitive $prm, $result)
{
if (
$name = $prm->getName();
$error = $prm->getError();

if(
$prm instanceof PrimitiveAlias
&& $result !== null
)
$this->markGood($prm->getInner()->getName());

$name = $prm->getName();


if (null === $result) {
if ($prm->isRequired())
$this->errors[$name] = self::MISSING;
$this->markCustom($name, BasePrimitive::MISSING);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

по описанной идее за MISSING должен отвечать примитив а не Form

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это первая итерация, по хорошему все проверки должны проходить в примитивах ну собсно он должен выстовлять себе ошибки если таковы были.

Но я тут начал эту тему делать и прифигел, тама все примитивы надо будет затронуть практически, по этому оставляем на потом.


} elseif (true === $result) {
unset($this->errors[$name]);

} elseif ($error = $prm->getCustomError()) {
$this->markGood($name);

$this->errors[$name] = $error;

} else
$this->errors[$name] = self::WRONG;
} elseif ($error) {
$this->markCustom($name, $error);

} else
$this->markCustom($name, BasePrimitive::WRONG);

return $this;
}
Expand All @@ -458,28 +418,14 @@ private function checkImportResult(BasePrimitive $prm, $result)
**/
private function addErrorLabel($name, $errorType, $label)
{
if (
!isset($this->rules[$name])
&& !$this->get($name)->getName()
)
throw new MissingElementException(
"knows nothing about '{$name}'"
);

$this->labels[$name][$errorType] = $label;
$this->get($name)->setErrorLabel($errorType, $label);

return $this;
}

private function getErrorLabel($name, $errorType)
{
// checks for primitive's existence
$this->get($name);

if (isset($this->labels[$name][$errorType]))
return $this->labels[$name][$errorType];

return null;
return $this->get($name)->getErrorLabel($errorType);
}
}
?>
8 changes: 8 additions & 0 deletions core/Form/Primitive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,13 @@ public static function enumList($name)
{
return new PrimitiveEnumList($name);
}

/**
* @return PrimitiveRule
**/
public static function rule($name)
{
return new PrimitiveRule($name);
}
}
?>
Loading