diff --git a/core/Form/Form.class.php b/core/Form/Form.class.php index b4f9813899..de15b75516 100644 --- a/core/Form/Form.class.php +++ b/core/Form/Form.class.php @@ -1,12 +1,12 @@ primitives as $name => $prm) { - if (null !== $prm->getError()) - $errors[$name] = $prm->getError(); - } + $this->proto = $proto; - return $errors; + return $this; } - - public function hasError($name) + + /** + * @return EntityProto + **/ + public function getProto() { - return $this->get($name)->getError() !== null; + return $this->proto; } - - public function getError($name) - { - if ($this->hasError($name)) { - return $this->get($name)->getError(); - } - return null; - } - - public function getInnerErrors() + + public function export() { - $result = $this->getErrors(); - + $result = array(); + foreach ($this->primitives as $name => $prm) { - if ( - ( - ($prm instanceof PrimitiveFormsList) - || ($prm instanceof PrimitiveForm) - ) - && $prm->getValue() - ) { - if ($errors = $prm->getInnerErrors()) { - $result[$name] = $errors; - } else { - unset($result[$name]); - } - } + if ($prm->isImported()) + $result[$name] = $prm->exportValue(); } - + return $result; } - + + public function exportValue($name) + { + return $this->get($name)->exportValue(); + } + + public function toFormValue($value) + { + if ($value instanceof FormField) + return $this->getValue($value->getName()); + elseif ($value instanceof LogicalObject) + return $value->toBoolean($this); + else + return $value; + } + + /** * @return Form **/ - public function dropAllErrors() + public function import($scope) { - foreach($this->primitives as $name => $prm) { - $prm->dropError(); - } + foreach ($this->primitives as $prm) + $this->importPrimitive($scope, $prm); return $this; } - + /** * @return Form **/ - public function enableImportFiltering() + public function importMore($scope) { - $this->importFiltering = true; - + foreach ($this->primitives as $prm) { + if (!$prm->isImported()) + $this->importPrimitive($scope, $prm); + } + return $this; } - + /** * @return Form **/ - public function disableImportFiltering() + public function importOne($name, $scope) { - $this->importFiltering = false; - - return $this; + return $this->importPrimitive($scope, $this->get($name)); } - - /** - * primitive marking - **/ - //@{ + /** * @return Form **/ - public function markMissing($primitiveName, $label = null) + public function importValue($name, $value) { - return $this->markCustom($primitiveName, BasePrimitive::MISSING, $label); + $this->get($name)->importValue($value); + + return $this; } - + /** - * rule or primitive - * * @return Form **/ - public function markWrong($name, $label = null) + public function importOneMore($name, $scope) { $prm = $this->get($name); - $prm->markWrong(); - - if ($label !== null) - $prm->setWrongLabel($label); - + + if (!$prm->isImported()) + return $this->importPrimitive($scope, $prm); + return $this; } - + /** * @return Form **/ - public function markGood($primitiveName) + private function importPrimitive($scope, BasePrimitive $prm) { - $this->get($primitiveName)->markGood(); - + $prm->import($scope); + return $this; } - + + /** - * Set's custom error mark for primitive. - * + * primitive marking + ** + //@{ + ** * @return Form **/ - public function markCustom($primitiveName, $customMark, $label = null) + public function markGood($name) { - $this->get($primitiveName)->setErrorLabel($customMark, $label); - + $this->get($name)->markGood(); + return $this; } - //@} - + /** - * Returns plain list of error's labels + * @return Form **/ - public function getTextualErrors() + public function markMissing($name, $label = null) { - $list = array(); - - foreach ($this->primitives as $name => $prm) { - if ($label = $this->getTextualErrorFor($name)) - $list[$name] = $label; - } - - return $list; - } - - public function getTextualErrorFor($name) - { - return $this->get($name)->getActualErrorLabel(); - } - - public function getErrorDescriptionFor($name) - { - return $this->get($name)->getActualErrorDescription(); + return $this->markCustom($name, BasePrimitive::MISSING, $label); } - + /** + * rule or primitive + * * @return Form **/ - public function addErrorDescription($name, $errorType, $description) + public function markWrong($name, $label = null) { - $this->get($name)->setErrorDescription($errorType, $description); + $prm = $this->get($name); + $prm->markWrong(); + + if ($label !== null) + $prm->setWrongLabel($label); return $this; } - + /** + * Set's custom error mark for primitive. + * * @return Form **/ - public function addWrongLabel($primitiveName, $label) + public function markCustom($name, $customMark, $label = null) { - return $this->addErrorLabel($primitiveName, BasePrimitive::WRONG, $label); + $this->get($name)->setErrorLabel($customMark, $label); + + return $this; } - - /** - * @return Form - **/ - public function addMissingLabel($primitiveName, $label) + //@} + + public function hasError($name) { - return $this->addErrorLabel($primitiveName, BasePrimitive::MISSING, $label); + return $this->get($name)->getError() !== null; } - - /** - * @return Form - **/ - public function addCustomLabel($primitiveName, $customMark, $label) + + public function getError($name) { - return $this->addErrorLabel($primitiveName, $customMark, $label); + return $this->get($name)->getError(); } - - public function getWrongLabel($primitiveName) + + public function getErrors() { - return $this->getErrorLabel($primitiveName, BasePrimitive::WRONG); + $errors = array(); + foreach($this->primitives as $name => $prm) { + if (null !== $prm->getError()) + $errors[$name] = $prm->getError(); + } + + return $errors; } - - public function getMissingLabel($primitiveName) + + public function getInnerErrors() { - return $this->getErrorLabel($primitiveName, BasePrimitive::MISSING); + $result = $this->getErrors(); + + foreach ($this->primitives as $name => $prm) { + if ( + ( + ($prm instanceof PrimitiveFormsList) + || ($prm instanceof PrimitiveForm) + ) + && $prm->getValue() + ) { + if ($errors = $prm->getInnerErrors()) { + $result[$name] = $errors; + } else { + unset($result[$name]); + } + } + } + + return $result; } - + /** - * @return Form + * Returns plain list of error's labels **/ - public function import($scope) + public function getTextualErrors() { - foreach ($this->primitives as $prm) - $this->importPrimitive($scope, $prm); - - return $this; + $list = array(); + + foreach ($this->primitives as $name => $prm) { + if ($label = $this->getTextualErrorFor($name)) + $list[$name] = $label; + } + + return $list; } - + + public function getTextualErrorFor($name) + { + return $this->get($name)->getActualErrorLabel(); + } + /** * @return Form **/ - public function importMore($scope) + public function dropAllErrors() { - foreach ($this->primitives as $prm) { - if (!$prm->isImported()) - $this->importPrimitive($scope, $prm); + foreach($this->primitives as $name => $prm) { + $prm->dropError(); } - + return $this; } - + + /** * @return Form **/ - public function importOne($primitiveName, $scope) + public function enableImportFiltering() { - return $this->importPrimitive($scope, $this->get($primitiveName)); + $this->importFiltering = true; + + return $this; } - + /** * @return Form **/ - public function importValue($primitiveName, $value) + public function disableImportFiltering() { - $this->get($primitiveName)->importValue($value); + $this->importFiltering = false; return $this; } - + /** * @return Form **/ - public function importOneMore($primitiveName, $scope) + public function setImportFiltering($really = false) { - $prm = $this->get($primitiveName); - - if (!$prm->isImported()) - return $this->importPrimitive($scope, $prm); - + $this->importFiltering = ($really === true); + return $this; } - - public function exportValue($primitiveName) - { - return $this->get($primitiveName)->exportValue(); - } - - public function export() + + + /** + * @return Form + **/ + public function addErrorDescription($name, $errorType, $description) { - $result = array(); - - foreach ($this->primitives as $name => $prm) { - if ($prm->isImported()) - $result[$name] = $prm->exportValue(); - } - - return $result; + $this->get($name)->setErrorDescription($errorType, $description); + + return $this; } - - public function toFormValue($value) + + public function getErrorDescriptionFor($name) { - if ($value instanceof FormField) - return $this->getValue($value->getName()); - elseif ($value instanceof LogicalObject) - return $value->toBoolean($this); - else - return $value; + return $this->get($name)->getActualErrorDescription(); } - + + /** * @return Form **/ - public function setProto(EntityProto $proto) + public function addWrongLabel($name, $label) { - $this->proto = $proto; - - return $this; + return $this->addErrorLabel($name, BasePrimitive::WRONG, $label); } - + /** - * @return EntityProto + * @return Form **/ - public function getProto() + public function addMissingLabel($name, $label) { - return $this->proto; + return $this->addErrorLabel($name, BasePrimitive::MISSING, $label); } - + /** * @return Form **/ - private function importPrimitive($scope, BasePrimitive $prm) + public function addCustomLabel($name, $customMark, $label) { - $prm->import($scope); + return $this->addErrorLabel($name, $customMark, $label); + } - return $this; + public function getWrongLabel($name) + { + return $this->getErrorLabel($name, BasePrimitive::WRONG); } - + + public function getMissingLabel($name) + { + return $this->getErrorLabel($name, BasePrimitive::MISSING); + } + + /** * Assigns specific label for given primitive and error type. * One more example of horrible documentation style. @@ -357,13 +368,13 @@ private function importPrimitive($scope, BasePrimitive $prm) private function addErrorLabel($name, $errorType, $label) { $this->get($name)->setErrorLabel($errorType, $label); - + return $this; } - + private function getErrorLabel($name, $errorType) { return $this->get($name)->getErrorLabel($errorType); } + } -?> \ No newline at end of file diff --git a/core/Form/PlainForm.class.php b/core/Form/PlainForm.class.php index 303ec94c9d..d3de6ec03f 100644 --- a/core/Form/PlainForm.class.php +++ b/core/Form/PlainForm.class.php @@ -18,7 +18,7 @@ abstract class PlainForm { protected $primitives = array(); - + /** * @return Form **/ @@ -26,10 +26,10 @@ public function clean() { foreach ($this->primitives as $prm) $prm->clean(); - + return $this; } - + public function exists($name) { return isset($this->primitives[$name]); @@ -42,7 +42,7 @@ public function primitiveExists($name) { return $this->exists($name); } - + /** * @throws WrongArgumentException * @return Form @@ -50,27 +50,27 @@ public function primitiveExists($name) public function add(BasePrimitive $prm) { $name = $prm->getName(); - + Assert::isFalse( isset($this->primitives[$name]), - 'i am already exists!' + "primitive '{$name}' already exists!" ); - + $this->primitives[$name] = $prm; - + return $this; } - + /** * @return Form **/ public function set(BasePrimitive $prm) { $this->primitives[$prm->getName()] = $prm; - + return $this; } - + /** * @throws MissingElementException * @return Form @@ -81,12 +81,12 @@ public function drop($name) throw new MissingElementException( "can not drop inexistent primitive '{$name}'" ); - + unset($this->primitives[$name]); - + return $this; } - + /** * @throws MissingElementException * @return BasePrimitive @@ -95,69 +95,69 @@ public function get($name) { if (isset($this->primitives[$name])) return $this->primitives[$name]; - + throw new MissingElementException("knows nothing about '{$name}'"); } - + public function getValue($name) { return $this->get($name)->getValue(); } - + public function setValue($name, $value) { $this->get($name)->setValue($value); - + return $this; } - + public function getRawValue($name) { return $this->get($name)->getRawValue(); } - + public function getActualValue($name) { return $this->get($name)->getActualValue(); } - + public function getSafeValue($name) { return $this->get($name)->getSafeValue(); } - + public function getChoiceValue($name) { Assert::isTrue(($prm = $this->get($name)) instanceof ListedPrimitive); - + return $prm->getChoiceValue(); } - + public function getActualChoiceValue($name) { Assert::isTrue(($prm = $this->get($name)) instanceof ListedPrimitive); - + return $prm->getActualChoiceValue(); } - + public function getDisplayValue($name) { - $primitive = $this->get($name); - - if ($primitive instanceof FiltrablePrimitive) - return $primitive->getDisplayValue(); + $prm = $this->get($name); + + if ($prm instanceof FiltrablePrimitive) + return $prm->getDisplayValue(); else - return $primitive->getActualValue(); + return $prm->getActualValue(); } - + public function getPrimitiveNames() { return array_keys($this->primitives); } - + public function getPrimitiveList() { return $this->primitives; } + } -?> \ No newline at end of file diff --git a/core/Form/Primitives/BasePrimitive.class.php b/core/Form/Primitives/BasePrimitive.class.php index 52008dc7a7..75da31c1cd 100644 --- a/core/Form/Primitives/BasePrimitive.class.php +++ b/core/Form/Primitives/BasePrimitive.class.php @@ -11,7 +11,7 @@ /** * Parent of every Primitive. - * + * * @ingroup Primitives * @ingroup Module **/ @@ -64,19 +64,32 @@ public function __construct($name) { $this->name = $name; } - + + /** + * @return BasePrimitive + **/ + public function clean() + { + $this->raw = null; + $this->value = null; + $this->imported = false; + $this->dropError(); + + return $this; + } + public function getName() { return $this->name; } - + /** * @return BasePrimitive **/ public function setName($name) { $this->name = $name; - + return $this; } @@ -84,169 +97,142 @@ public function getDefault() { return $this->default; } - + /** * @return BasePrimitive **/ public function setDefault($default) { $this->default = $default; - + + return $this; + } + + /** + * @return BasePrimitive + **/ + public function setValue($value) + { + $this->value = $value; + return $this; } - + public function getValue() { return $this->value; } - + + /** + * @return BasePrimitive + * + * usually, you should not use this method + **/ + public function setRawValue($raw) + { + $this->raw = $raw; + + return $this; + } + public function getRawValue() { return $this->raw; } - + public function getActualValue() { if (null !== $this->value) return $this->value; elseif ($this->imported) return $this->raw; - + return $this->default; } - + public function getSafeValue() { if ($this->imported) return $this->value; - + return $this->default; } - - /** - * @return BasePrimitive - **/ - public function setValue($value) + + public function exportValue() { - $this->value = $value; - - return $this; + return $this->value; } - + /** * @return BasePrimitive **/ public function dropValue() { $this->value = null; - - return $this; - } - - /** - * @return BasePrimitive - * - * usually, you should not use this method - **/ - public function setRawValue($raw) - { - $this->raw = $raw; - - return $this; - } - - public function isRequired() - { - return $this->required; - } - - /** - * @return BasePrimitive - **/ - public function setRequired($really = false) - { - $this->required = (true === $really ? true : false); - + return $this; } - + /** * @return BasePrimitive **/ public function required() { $this->required = true; - + return $this; } - + /** * @return BasePrimitive **/ public function optional() { $this->required = false; - + return $this; } - - public function isImported() - { - return $this->imported; - } - + /** * @return BasePrimitive **/ - public function clean() + public function setRequired($really = false) { - $this->raw = null; - $this->value = null; - $this->imported = false; - $this->dropError(); - + $this->required = ($really === true); + return $this; } - - public function importValue($value) - { - return $this->import(array($this->getName() => $value)); - } - - public function exportValue() - { - return $this->value; - } - public function getError() + public function isRequired() { - return $this->error | $this->customError; + return $this->required; } - /** - * @return BasePrimitive - **/ - public function setError($error) - { - Assert::isPositiveInteger($error); - $this->error = $error; - $this->customError = $error; + public function import($scope) + { + if (isset($scope[$this->name])) + { + $this->setRawValue( + $scope[$this->name] + ); + $this->imported = true; + } return $this; } - /** - * @return BasePrimitive - **/ - public function dropError() + public function importValue($value) { - $this->error = null; - $this->customError = null; + return $this->import(array($this->getName() => $value)); + } - return $this; + public function isImported() + { + return $this->imported; } + /** * @alias dropError * @return BasePrimitive @@ -262,7 +248,7 @@ public function markGood() */ public function markWrong() { - return $this->setError(static::WRONG); + return $this->setError(self::WRONG); } /** @@ -271,7 +257,26 @@ public function markWrong() */ public function markMissing() { - return $this->setError(static::MISSING); + return $this->setError(self::MISSING); + } + + + /** + * @return BasePrimitive + **/ + public function setError($error) + { + Assert::isPositiveInteger($error); + + $this->error = $error; + $this->customError = $error; + + return $this; + } + + public function getError() + { + return $this->error | $this->customError; } /** @@ -282,6 +287,18 @@ public function getCustomError() return $this->getError(); } + /** + * @return BasePrimitive + **/ + public function dropError() + { + $this->error = null; + $this->customError = null; + + return $this; + } + + /** * @param null $val * @return BasePrimitive @@ -334,12 +351,12 @@ public function setErrorLabel($type, $val) */ public function setWrongLabel($val) { - return $this->setErrorLabel(BasePrimitive::WRONG, $val); + return $this->setErrorLabel(self::WRONG, $val); } public function setMissingLabel($val) { - return $this->setErrorLabel(BasePrimitive::MISSING, $val); + return $this->setErrorLabel(self::MISSING, $val); } /** @@ -360,12 +377,11 @@ public function getActualErrorLabel() { if( ($error = $this->getError()) - && $error !==null + && $error !== null ) { return $this->getErrorLabel($error); } - return null; } @@ -423,18 +439,4 @@ public function getErrorDescriptions() return $this->errorDescriptions; } - - public function import($scope) - { - if (isset($scope[$this->name])) - { - $this->setRawValue( - $scope[$this->name] - ); - $this->imported = true; - } - - return $this; - } } -?> \ No newline at end of file diff --git a/core/Form/Primitives/PrimitiveAlias.class.php b/core/Form/Primitives/PrimitiveAlias.class.php index b1fe1279a8..e35be259e4 100644 --- a/core/Form/Primitives/PrimitiveAlias.class.php +++ b/core/Form/Primitives/PrimitiveAlias.class.php @@ -15,140 +15,134 @@ final class PrimitiveAlias extends BasePrimitive { private $primitive = null; - + public function __construct($name, BasePrimitive $prm) { $this->name = $name; $this->primitive = $prm; } - + + /** + * @return PrimitiveAlias + **/ + public function clean() + { + $this->primitive->clean(); + + return $this; + } + public function getInner() { return $this->primitive; } - + public function getName() { return $this->name; } - + public function getDefault() { return $this->primitive->getDefault(); } - + /** * @return PrimitiveAlias **/ public function setDefault($default) { $this->primitive->setDefault($default); - + return $this; } - + + /** + * @return PrimitiveAlias + **/ + public function setValue($value) + { + $this->primitive->setValue($value); + + return $this; + } + public function getValue() { return $this->primitive->getValue(); } - + + /** + * usually, you should not use this method + * + * @return PrimitiveAlias + **/ + public function setRawValue($raw) + { + $this->primitive->setRawValue($raw); + + return $this; + } + public function getRawValue() { return $this->primitive->getRawValue(); } - + /** * @deprecated by getFormValue **/ public function getActualValue() { - if (null !== $this->primitive->getValue()) - return $this->primitive->getValue(); - elseif ($this->primitive->isImported()) - return $this->primitive->getRawValue(); - - return $this->primitive->getDefault(); + return $this->primitive->getActualValue(); } - + public function getSafeValue() { - if ($this->primitive->isImported()) - return $this->primitive->getValue(); - - return $this->primitive->getDefault(); + return $this->primitive->getSafeValue(); } - + public function getFormValue() { if (!$this->primitive->isImported()) { if ($this->primitive->getValue() === null) return null; - + return $this->primitive->exportValue(); } - + return $this->primitive->getRawValue(); } - - /** - * @return PrimitiveAlias - **/ - public function setValue($value) + + public function exportValue() { - $this->primitive->setValue($value); - - return $this; + return $this->primitive->exportValue(); } - + /** * @return PrimitiveAlias **/ public function dropValue() { $this->primitive->dropValue(); - - return $this; - } - - /** - * @return PrimitiveAlias - **/ - public function setRawValue($raw) - { - $this->primitive->setRawValue($raw); - + return $this; } - + + public function isImported() { return $this->primitive->isImported(); } - - /** - * @return PrimitiveAlias - **/ - public function clean() - { - $this->primitive->clean(); - - return $this; - } - + public function importValue($value) { return $this->primitive->importValue($value); } - - public function exportValue() - { - return $this->primitive->exportValue(); - } public function import($scope) { - if (array_key_exists($this->name, $scope)) - { + if (array_key_exists($this->name, $scope)) { $result = $this->primitive->import( array( @@ -163,12 +157,10 @@ public function import($scope) $this->setError($error); } - return $result; } - return null; } + } -?> \ No newline at end of file diff --git a/core/Form/RegulatedForm.class.php b/core/Form/RegulatedForm.class.php index 05e49995bd..9ebb87b27a 100644 --- a/core/Form/RegulatedForm.class.php +++ b/core/Form/RegulatedForm.class.php @@ -29,7 +29,7 @@ public function addRule($name, LogicalObject $rule) setForm($this) ); } - + /** * @throws MissingElementException * @return Form @@ -44,7 +44,7 @@ public function dropRuleByName($name) return $this->drop($name); } - + public function ruleExists($name) { return ( @@ -52,24 +52,20 @@ public function ruleExists($name) && ($this->get($name) instanceof PrimitiveRule) ); } - + /** * @return Form **/ public function checkRules() { - $primitives = $this->getPrimitiveList(); - foreach($primitives as $prm) { + foreach($this->getPrimitiveList() as $prm) if( $prm instanceof PrimitiveRule && !$prm->import(null) - ) { + ) $prm->markWrong(); - } - } - return $this; } + } -?> \ No newline at end of file