Skip to content

PrimitiveRule, изменение внутренностей Form::addRule, checkRules и т.д. #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
72 changes: 31 additions & 41 deletions core/Form/Form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,18 @@ public static function create()

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

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

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];
}
return null;
}
Expand Down Expand Up @@ -88,7 +85,6 @@ public function getInnerErrors()
public function dropAllErrors()
{
$this->errors = array();
$this->violated = array();

return $this;
}
Expand Down Expand Up @@ -134,11 +130,9 @@ public function markWrong($name, $label = null)
{
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'
$name.' does not match known primitives'
);

if ($label !== null)
Expand All @@ -154,11 +148,9 @@ public function markGood($primitiveName)
{
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'
$primitiveName.' does not match known primitives'
);

return $this;
Expand Down Expand Up @@ -200,13 +192,6 @@ 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]]
Expand All @@ -220,13 +205,6 @@ public function getTextualErrorFor($name)
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]]
Expand All @@ -242,14 +220,7 @@ public function getErrorDescriptionFor($name)
**/
public function addErrorDescription($name, $errorType, $description)
{

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

$this->describedLabels[$name][$errorType] = $description;

Expand Down Expand Up @@ -290,6 +261,24 @@ public function getMissingLabel($primitiveName)
return $this->getErrorLabel($primitiveName, Form::MISSING);
}

/**
* @return Form
**/
public function checkRules()
{
foreach ($this->getPrimitiveList() as $primitiveName => $primitive) {
if ($primitive instanceof PrimitiveRule) {
if ($primitive->import(null, $this)) {
$this->markGood($primitiveName);
} else {
$this->markWrong($primitiveName);
Copy link
Contributor

Choose a reason for hiding this comment

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

аргументы не плохо бы привести к одному знаменателю
ну и везде в 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.

В этом методе нет аргументов. Внутренние переменные вполне прилично называются. Про Form не понял.

Copy link
Contributor

Choose a reason for hiding this comment

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

ну это уже обсуждалось вроде там откуда это взято.
про 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.

Так это там архитектура меняется, а тут оно rules, PrimitiveRule и только вокруг них. Опять же никто не решил к какому общему названию приходить. И опять же это локальные переменные внутри функции - они по названию говорят что значат и это хорошо.

Copy link
Contributor

Choose a reason for hiding this comment

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

согласен. погоды не делает. никто не решил но уже 3 высказались )

}
}
}

return $this;
}

/**
* @return Form
**/
Expand Down Expand Up @@ -328,6 +317,9 @@ public function importOne($primitiveName, $scope)
public function importValue($primitiveName, $value)
{
$prm = $this->get($primitiveName);
if ($prm instanceof PrimitiveRule) {
return $this;
}

return $this->checkImportResult($prm, $prm->importValue($value));
}
Expand Down Expand Up @@ -395,6 +387,10 @@ public function getProto()
**/
private function importPrimitive($scope, BasePrimitive $prm)
{
if ($prm instanceof PrimitiveRule) {
return $this;
}

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

Expand Down Expand Up @@ -464,13 +460,7 @@ 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->get($name);

$this->labels[$name][$errorType] = $label;

Expand Down
5 changes: 5 additions & 0 deletions core/Form/Primitive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,10 @@ public static function enumList($name)
{
return new PrimitiveEnumList($name);
}

public static function rule($name)
{
return new PrimitiveRule($name);
}
}
?>
45 changes: 45 additions & 0 deletions core/Form/Primitives/PrimitiveRule.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/***************************************************************************
* Copyright (C) 2008 by Konstantin V. Arkhipov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Primitives
**/
final class PrimitiveRule extends BasePrimitive
{
/**
* @var LogicalObject
*/
private $expression = null;

public function __clone()
{
$this->expression = clone $this->expression;
}

/**
* @return PrimitiveRule
**/
public function setExpression(LogicalObject $exp)
{
$this->expression = $exp;

return $this;
}

public function import($scope, Form $form = null)
{
Assert::isNotNull($form, 'expects Form as 2-nd argument');
Assert::isNotNull($this->expression, 'setExpression first');

return $this->expression->toBoolean($form) === true;
}
}
?>
36 changes: 9 additions & 27 deletions core/Form/RegulatedForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
**/
abstract class RegulatedForm extends PlainForm
{
protected $rules = array(); // forever
protected $violated = array(); // rules

/**
* @throws WrongArgumentException
* @return Form
Expand All @@ -28,9 +25,9 @@ public function addRule($name, LogicalObject $rule)
{
Assert::isString($name);

$this->rules[$name] = $rule;

return $this;
return $this->add(
Primitive::rule($name)->setExpression($rule)
);
}

/**
Expand All @@ -39,32 +36,17 @@ public function addRule($name, LogicalObject $rule)
**/
public function dropRuleByName($name)
{
if (isset($this->rules[$name])) {
unset($this->rules[$name]);
return $this;
$rule = $this->get($name);
if (!$rule instanceof PrimitiveRule) {
throw new MissingElementException("no such PrimitiveRule with '{$name}' name");
}

throw new MissingElementException(
"no such rule with '{$name}' name"
);
return $this->drop($name);
}

public function ruleExists($name)
{
return isset($this->rules[$name]);
}

/**
* @return Form
**/
public function checkRules()
{
foreach ($this->rules as $name => $logicalObject) {
if (!$logicalObject->toBoolean($this))
$this->violated[$name] = Form::WRONG;
}

return $this;
return $this->primitiveExists($name)
Copy link
Contributor

Choose a reason for hiding this comment

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

&& $this->get($name) instanceof PrimitiveRule;
}
}
?>