From 8361efe5fbf104ada1cb4d5ccfdbeec7a5984262 Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Wed, 16 May 2012 16:37:41 +0400 Subject: [PATCH 01/13] Migrate error from Form into Primitive --- test/core/FormPrimitivesDateTest.class.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/core/FormPrimitivesDateTest.class.php b/test/core/FormPrimitivesDateTest.class.php index 81646578e2..748adb85f5 100644 --- a/test/core/FormPrimitivesDateTest.class.php +++ b/test/core/FormPrimitivesDateTest.class.php @@ -123,25 +123,25 @@ protected function processInvalidBy($scope, $data) import($scope); $this->assertEquals( - $form->getValue('test'), - null + null, + $form->getValue('test') ); $this->assertEquals( - $form->getRawValue('test'), - $data + $data, + $form->getRawValue('test') ); $this->assertEquals( - $form->get('test')->isImported(), - true + true, + $form->get('test')->isImported() ); $this->assertEquals( - $form->getErrors(), array( 'test' => Form::WRONG, - ) + ), + $form->getErrors() ); } From b8b0592a5ac1ac6d67d40d45670caee09c5d2a37 Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Wed, 16 May 2012 16:39:15 +0400 Subject: [PATCH 02/13] Migrate error from Form into Primitive --- core/Form/Form.class.php | 110 +++++++++--------- core/Form/Primitives/BasePrimitive.class.php | 51 +++++++- core/Form/Primitives/PrimitiveAlias.class.php | 15 ++- 3 files changed, 113 insertions(+), 63 deletions(-) diff --git a/core/Form/Form.class.php b/core/Form/Form.class.php index b4c9077823..7716ba903f 100644 --- a/core/Form/Form.class.php +++ b/core/Form/Form.class.php @@ -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(); @@ -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 array_merge($errors, $this->violated); } 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; } @@ -87,7 +97,9 @@ public function getInnerErrors() **/ public function dropAllErrors() { - $this->errors = array(); + foreach ($this->primitives as $prm) + $prm->dropError(); + $this->violated = array(); return $this; @@ -122,7 +134,7 @@ public function disableImportFiltering() **/ public function markMissing($primitiveName) { - return $this->markCustom($primitiveName, Form::MISSING); + return $this->markCustom($primitiveName, BasePrimitive::MISSING); } /** @@ -132,16 +144,7 @@ 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); } /** @@ -149,8 +152,8 @@ public function markWrong($name) **/ public function markGood($primitiveName) { - if (isset($this->primitives[$primitiveName])) - unset($this->errors[$primitiveName]); + if($this->exists($primitiveName)) + $this->get($primitiveName)->dropError(); elseif (isset($this->rules[$primitiveName])) unset($this->violated[$primitiveName]); else @@ -166,11 +169,16 @@ 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; + if ($this->exists($name)) + $this->get($name)->setError($customMark); + elseif (isset($this->rules[$name])) + $this->violated[$name] = $customMark; + else + throw new MissingElementException( + $name.' does not match known primitives or rules' + ); return $this; } @@ -201,12 +209,10 @@ public function getTextualErrorFor($name) ) return $this->labels[$name][$this->violated[$name]]; elseif ( - isset( - $this->errors[$name], - $this->labels[$name][$this->errors[$name]] - ) + ($error = $this->getError($name) ) + && isset($this->labels[$name][$error]) ) - return $this->labels[$name][$this->errors[$name]]; + return $this->labels[$name][$error]; else return null; } @@ -221,12 +227,10 @@ public function getErrorDescriptionFor($name) ) return $this->describedLabels[$name][$this->violated[$name]]; elseif ( - isset( - $this->errors[$name], - $this->describedLabels[$name][$this->errors[$name]] - ) + ($error = $this->getError($name) ) + && isset($this->describedLabels[$name][$error]) ) - return $this->describedLabels[$name][$this->errors[$name]]; + return $this->describedLabels[$name][$error]; else return null; } @@ -421,27 +425,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 + && !($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); } 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; } diff --git a/core/Form/Primitives/BasePrimitive.class.php b/core/Form/Primitives/BasePrimitive.class.php index d7cbe2f8b3..5832175f3c 100644 --- a/core/Form/Primitives/BasePrimitive.class.php +++ b/core/Form/Primitives/BasePrimitive.class.php @@ -17,6 +17,9 @@ **/ abstract class BasePrimitive { + const WRONG = 0x0001; + const MISSING = 0x0002; + protected $name = null; protected $default = null; protected $value = null; @@ -25,8 +28,13 @@ abstract class BasePrimitive protected $imported = false; protected $raw = null; - - protected $customError = null; + + protected $error = null; + + /** + * @deprecated by error + */ + protected $customError = null; public function __construct($name) { @@ -171,6 +179,7 @@ public function clean() $this->raw = null; $this->value = null; $this->imported = false; + $this->dropError(); return $this; } @@ -184,13 +193,45 @@ public function exportValue() { return $this->value; } - + + public function getError() + { + return $this->error | $this->customError; + } + + /** + * @return BasePrimitive + **/ + public function setError($error) + { + Assert::isPositiveInteger($error); + + $this->error = $error; + $this->customError = $error; + + return $this; + } + + /** + * @return BasePrimitive + **/ + public function dropError() + { + $this->error = null; + $this->customError = null; + + return $this; + } + + /** + * @deprecated by getError + */ public function getCustomError() { - return $this->customError; + return $this->getError(); } - protected function import($scope) + public function import($scope) { if ( !empty($scope[$this->name]) diff --git a/core/Form/Primitives/PrimitiveAlias.class.php b/core/Form/Primitives/PrimitiveAlias.class.php index 33fe5cfbe2..0b8aeef58f 100644 --- a/core/Form/Primitives/PrimitiveAlias.class.php +++ b/core/Form/Primitives/PrimitiveAlias.class.php @@ -144,18 +144,23 @@ public function exportValue() { return $this->primitive->exportValue(); } - - public function getCustomError() + + public function getError() + { + return $this->primitive->getError(); + } + + public function setError($error) { - return $this->primitive->getCustomError(); + return $this->primitive->setError($error); } public function import($scope) { if (array_key_exists($this->name, $scope)) return - $this->primitive->import( - array($this->primitive->getName() => $scope[$this->name]) + $this->primitive->importValue( + $scope[$this->name] ); return null; From eaae5f412604dfba52e141a6d9f455dc0ba5a1ed Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Fri, 18 May 2012 22:06:52 +0400 Subject: [PATCH 03/13] add PrimitiveRule --- core/Form/Form.class.php | 102 +++------- core/Form/Primitive.class.php | 8 + core/Form/Primitives/BasePrimitive.class.php | 174 ++++++++++++++++++ core/Form/Primitives/PrimitiveAlias.class.php | 14 +- core/Form/Primitives/PrimitiveRule.class.php | 66 +++++++ core/Form/RegulatedForm.class.php | 37 ++-- 6 files changed, 290 insertions(+), 111 deletions(-) create mode 100644 core/Form/Primitives/PrimitiveRule.class.php diff --git a/core/Form/Form.class.php b/core/Form/Form.class.php index 7716ba903f..fbe2b93008 100644 --- a/core/Form/Form.class.php +++ b/core/Form/Form.class.php @@ -51,7 +51,7 @@ public function getErrors() if ($error = $prm->getError()) $errors[$name] = $error; - return array_merge($errors, $this->violated); + return $errors; } public function hasError($name) @@ -99,8 +99,6 @@ public function dropAllErrors() { foreach ($this->primitives as $prm) $prm->dropError(); - - $this->violated = array(); return $this; } @@ -152,15 +150,8 @@ public function markWrong($name) **/ public function markGood($primitiveName) { - if($this->exists($primitiveName)) - $this->get($primitiveName)->dropError(); - 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; } @@ -171,14 +162,7 @@ public function markGood($primitiveName) **/ public function markCustom($name, $customMark) { - if ($this->exists($name)) - $this->get($name)->setError($customMark); - elseif (isset($this->rules[$name])) - $this->violated[$name] = $customMark; - else - throw new MissingElementException( - $name.' does not match known primitives or rules' - ); + $this->get($name)->setError($customMark); return $this; } @@ -201,38 +185,12 @@ 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 ( - ($error = $this->getError($name) ) - && isset($this->labels[$name][$error]) - ) - return $this->labels[$name][$error]; - 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 ( - ($error = $this->getError($name) ) - && isset($this->describedLabels[$name][$error]) - ) - return $this->describedLabels[$name][$error]; - else - return null; + return $this->get($name)->getActualErrorDescription(); } /** @@ -240,16 +198,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->describedLabels[$name][$errorType] = $description; + $this->get($name)->setErrorDescription($errorType, $description); return $this; } @@ -259,7 +208,7 @@ 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); } /** @@ -267,7 +216,7 @@ public function addWrongLabel($primitiveName, $label) **/ public function addMissingLabel($primitiveName, $label) { - return $this->addErrorLabel($primitiveName, Form::MISSING, $label); + return $this->addErrorLabel($primitiveName, BasePrimitive::MISSING, $label); } /** @@ -280,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); } /** @@ -294,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; } @@ -393,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) { @@ -430,7 +386,7 @@ private function checkImportResult(BasePrimitive $prm, $result) if( $prm instanceof PrimitiveAlias - && !($result === null) + && $result !== null ) $this->markGood($prm->getInner()->getName()); @@ -462,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); } } ?> \ No newline at end of file diff --git a/core/Form/Primitive.class.php b/core/Form/Primitive.class.php index 0d94b18a1d..5c16419e1a 100644 --- a/core/Form/Primitive.class.php +++ b/core/Form/Primitive.class.php @@ -386,5 +386,13 @@ public static function enumList($name) { return new PrimitiveEnumList($name); } + + /** + * @return PrimitiveRule + **/ + public static function rule($name) + { + return new PrimitiveRule($name); + } } ?> \ No newline at end of file diff --git a/core/Form/Primitives/BasePrimitive.class.php b/core/Form/Primitives/BasePrimitive.class.php index 5832175f3c..a82912e0f4 100644 --- a/core/Form/Primitives/BasePrimitive.class.php +++ b/core/Form/Primitives/BasePrimitive.class.php @@ -31,6 +31,30 @@ abstract class BasePrimitive protected $error = null; + /** + * Label for presentation + * @var string + */ + protected $label = null; + + /** + * Description for presentation + * @var string + */ + protected $description = null; + + /** + * Error labels for each error type + * @var array of ('errorType' => 'label') + */ + protected $errorLabels = array(); + + /** + * Error description for each error type + * @var array of ('errorType' => 'label') + */ + protected $errorDescriptions = array(); + /** * @deprecated by error */ @@ -223,6 +247,33 @@ public function dropError() return $this; } + /** + * @alias dropError + * @return BasePrimitive + */ + public function markGood() + { + return $this->dropError(); + } + + /** + * @alias setError + * @return BasePrimitive + */ + public function markWrong() + { + return $this->setError(static::WRONG); + } + + /** + * @alias setError + * @return BasePrimitive + */ + public function markMissing() + { + return $this->setError(static::MISSING); + } + /** * @deprecated by getError */ @@ -230,6 +281,129 @@ public function getCustomError() { return $this->getError(); } + + /** + * @param null $val + * @return BasePrimitive + */ + public function setLabel($val=null) + { + $this->label = $val; + + return $this; + } + + /** + * @return null|string + */ + public function getLabel() + { + return $this->label; + } + + public function setDescription($val) + { + $this->description = $val; + + return $this; + } + + /** + * @return null|string + */ + public function getDescription() + { + return $this->description; + } + + /** + * @param $type + * @param $val + * @return BasePrimitive + */ + public function setErrorLabel($type, $val) + { + $this->errorLabels[$type] = $val; + + return $this; + } + + /** + * @param integer $type + * @return null|string + */ + public function getErrorLabel($type) + { + return (isset($this->errorLabels[$type])) + ? $this->errorLabels[$type] + : null; + } + + /** + * @return null|string + */ + public function getActualErrorLabel() + { + $type = $this->getError(); + + if($type === null) + return null; + + return $this->getErrorLabel($type); + } + + /** + * @return array + */ + public function getErrorLabels() + { + return $this->errorLabels; + } + + /** + * @param $type + * @param $val + * @return BasePrimitive + */ + public function setErrorDescription($type, $val) + { + $this->errorDescription[$type] = $val; + + return $this; + } + + /** + * @param integer $type + * @return null|string + */ + public function getErrorDescription($type) + { + return (isset($this->errorDescription[$type])) + ? $this->errorDescription[$type] + : null; + } + + /** + * @return null|string + */ + public function getActualErrorDescription() + { + $type = $this->getError(); + + if($type === null) + return null; + + return $this->getErrorDescription($type); + } + + /** + * @return array + */ + public function getErrorDescriptions() + { + return $this->errorDescriptions; + } + public function import($scope) { diff --git a/core/Form/Primitives/PrimitiveAlias.class.php b/core/Form/Primitives/PrimitiveAlias.class.php index 0b8aeef58f..7c2ee31b82 100644 --- a/core/Form/Primitives/PrimitiveAlias.class.php +++ b/core/Form/Primitives/PrimitiveAlias.class.php @@ -145,22 +145,12 @@ public function exportValue() return $this->primitive->exportValue(); } - public function getError() - { - return $this->primitive->getError(); - } - - public function setError($error) - { - return $this->primitive->setError($error); - } - public function import($scope) { if (array_key_exists($this->name, $scope)) return - $this->primitive->importValue( - $scope[$this->name] + $this->primitive->import( + array($this->primitive->getName() => $scope[$this->name]) ); return null; diff --git a/core/Form/Primitives/PrimitiveRule.class.php b/core/Form/Primitives/PrimitiveRule.class.php new file mode 100644 index 0000000000..ba63eb2e11 --- /dev/null +++ b/core/Form/Primitives/PrimitiveRule.class.php @@ -0,0 +1,66 @@ +form = clone $this->form; + $this->expression = clone $this->expression; + } + + /** + * @return PrimitiveRule + **/ + public function setForm(Form $form) + { + $this->form = $form; + + return $this; + } + + /** + * @return PrimitiveRule + **/ + public function setExpression(LogicalObject $exp) + { + $this->expression = $exp; + + return $this; + } + + public function import($scope) + { + Assert::isNotNull($this->form); + Assert::isNotNull($this->expression); + + $result = $this->expression->toBoolean($this->form); + + if(!$result) + $this->setError(BasePrimitive::WRONG); + + return $result; + } + } +?> \ No newline at end of file diff --git a/core/Form/RegulatedForm.class.php b/core/Form/RegulatedForm.class.php index dcf3ac9052..500c2b9b97 100644 --- a/core/Form/RegulatedForm.class.php +++ b/core/Form/RegulatedForm.class.php @@ -17,20 +17,17 @@ **/ abstract class RegulatedForm extends PlainForm { - protected $rules = array(); // forever - protected $violated = array(); // rules - /** * @throws WrongArgumentException * @return Form **/ public function addRule($name, LogicalObject $rule) { - Assert::isString($name); - - $this->rules[$name] = $rule; - - return $this; + return $this->add( + Primitive::rule($name) + ->setExpression($rule) + ->setForm($this) + ); } /** @@ -39,19 +36,18 @@ public function addRule($name, LogicalObject $rule) **/ public function dropRuleByName($name) { - if (isset($this->rules[$name])) { - unset($this->rules[$name]); - return $this; - } - - throw new MissingElementException( - "no such rule with '{$name}' name" + $rule = $this->get($name); + + Assert::isInstance($rule, 'PrimitiveRule', + 'primitive by "'.$name.'" must be instanceof PrimitiveRule! gived, "'.get_class($rule).'"' ); + + return $this->drop($name); } public function ruleExists($name) { - return isset($this->rules[$name]); + return ($this->exists($name) && ($this->get($name) instanceof PrimitiveRule)); } /** @@ -59,9 +55,12 @@ public function ruleExists($name) **/ public function checkRules() { - foreach ($this->rules as $name => $logicalObject) { - if (!$logicalObject->toBoolean($this)) - $this->violated[$name] = Form::WRONG; + $primitives = $this->getPrimitiveList(); + foreach($primitives as $prm) + { + if($prm instanceof PrimitiveRule) + if (!$prm->import(null)) + $prm->markWrong(); } return $this; From 42901143dfbe3cc84abe77c4e5f34e6d9193feab Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Mon, 21 May 2012 14:05:49 +0400 Subject: [PATCH 04/13] clean cache test crashes & some other --- core/Cache/PeclMemcached.class.php | 12 ++--- core/Form/Primitives/BasePrimitive.class.php | 14 +++++ core/Form/Primitives/PrimitiveAlias.class.php | 10 +++- test/core/AggregateCacheTest.class.php | 22 ++++---- test/core/PrimitiveLabelsTest.class.php | 54 +++++++++++++++++++ test/core/SequentialCacheTest.class.php | 6 +-- 6 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 test/core/PrimitiveLabelsTest.class.php diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index e2c41fc41d..098fc1e788 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -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 ) { $this->host = $host; diff --git a/core/Form/Primitives/BasePrimitive.class.php b/core/Form/Primitives/BasePrimitive.class.php index a82912e0f4..1478331198 100644 --- a/core/Form/Primitives/BasePrimitive.class.php +++ b/core/Form/Primitives/BasePrimitive.class.php @@ -328,6 +328,20 @@ public function setErrorLabel($type, $val) return $this; } + /** + * @param $val + * @return BasePrimitive + */ + public function setWrongLabel($val) + { + return $this->setErrorLabel(BasePrimitive::WRONG, $val); + } + + public function setMissingLabel($val) + { + return $this->setErrorLabel(BasePrimitive::MISSING, $val); + } + /** * @param integer $type * @return null|string diff --git a/core/Form/Primitives/PrimitiveAlias.class.php b/core/Form/Primitives/PrimitiveAlias.class.php index 7c2ee31b82..f25ad5cd75 100644 --- a/core/Form/Primitives/PrimitiveAlias.class.php +++ b/core/Form/Primitives/PrimitiveAlias.class.php @@ -148,11 +148,19 @@ public function exportValue() public function import($scope) { if (array_key_exists($this->name, $scope)) - return + { + $result = $this->primitive->import( array($this->primitive->getName() => $scope[$this->name]) ); + if($error = $this->primitive->getError()) + $this->setError($error); + + return $result; + } + + return null; } } diff --git a/test/core/AggregateCacheTest.class.php b/test/core/AggregateCacheTest.class.php index 00332b9c63..63803a0401 100644 --- a/test/core/AggregateCacheTest.class.php +++ b/test/core/AggregateCacheTest.class.php @@ -8,11 +8,11 @@ public function testAggregateCache() { return $this->doTestMemcached( AggregateCache::create()-> - addPeer('low', Memcached::create(), AggregateCache::LEVEL_LOW)-> - addPeer('normal1', Memcached::create())-> - addPeer('normal2', Memcached::create())-> - addPeer('normal3', Memcached::create())-> - addPeer('high', Memcached::create(), AggregateCache::LEVEL_HIGH)-> + addPeer('low', PeclMemcached::create(), AggregateCache::LEVEL_LOW)-> + addPeer('normal1', PeclMemcached::create())-> + addPeer('normal2', PeclMemcached::create())-> + addPeer('normal3', PeclMemcached::create())-> + addPeer('high', PeclMemcached::create(), AggregateCache::LEVEL_HIGH)-> setClassLevel('one', 0xb000) ); } @@ -33,11 +33,11 @@ public function testSimpleAggregateCache() { return $this->doTestMemcached( SimpleAggregateCache::create()-> - addPeer('low', Memcached::create(), AggregateCache::LEVEL_LOW)-> - addPeer('normal1', Memcached::create())-> - addPeer('normal2', Memcached::create())-> - addPeer('normal3', Memcached::create())-> - addPeer('high', Memcached::create(), AggregateCache::LEVEL_HIGH)-> + addPeer('low', PeclMemcached::create(), AggregateCache::LEVEL_LOW)-> + addPeer('normal1', PeclMemcached::create())-> + addPeer('normal2', PeclMemcached::create())-> + addPeer('normal3', PeclMemcached::create())-> + addPeer('high', PeclMemcached::create(), AggregateCache::LEVEL_HIGH)-> setClassLevel('one', 0xb000) ); } @@ -47,7 +47,7 @@ public function testCyclicAggregateCache() $this->doTestMemcached( CyclicAggregateCache::create()-> setSummaryWeight(42)-> - addPeer('first', Memcached::create(), 25)-> + addPeer('first', PeclMemcached::create(), 25)-> addPeer('second', PeclMemcached::create(), 1)-> addPeer('third', PeclMemcached::create(), 13) ); diff --git a/test/core/PrimitiveLabelsTest.class.php b/test/core/PrimitiveLabelsTest.class.php new file mode 100644 index 0000000000..7d1d86f3aa --- /dev/null +++ b/test/core/PrimitiveLabelsTest.class.php @@ -0,0 +1,54 @@ +setMax(3); + + $label = 'Some string label'; + $prm->setLabel($label); + $this->assertEquals($label, $prm->getLabel()); + + $description = 'Some string description'; + $prm->setDescription($description); + $this->assertEquals($description, $prm->getDescription()); + + $missingLabel = 'You must complete this field'; + $prm->setMissingLabel($missingLabel); + $prm->markMissing(); + $this->assertEquals($missingLabel, $prm->getActualErrorLabel()); + + $prm->clean(); + + $this->assertNull($prm->getActualErrorLabel()); + + $wrongLabel = 'The error in the field'; + $prm->setWrongLabel($wrongLabel); + $prm->markWrong(); + $this->assertEquals($wrongLabel, $prm->getActualErrorLabel()); + + $prm->clean(); + + $customErrorLabel = 'The custom error in the field'; + $customError = 25; + $prm->setErrorLabel($customError, $customErrorLabel); + $prm->setError($customError); + $this->assertEquals($customErrorLabel, $prm->getActualErrorLabel()); + + + $prm->import(array('someString'=>'abc')); + + + } + + } \ No newline at end of file diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php index 531ff17cd1..58952fd7ec 100644 --- a/test/core/SequentialCacheTest.class.php +++ b/test/core/SequentialCacheTest.class.php @@ -16,7 +16,7 @@ public function testMultiCacheAliveLast() $alifePeer = new PeclMemcached("127.0.0.1", "11211"); //some existing memcached $alifePeer->set('some_key', 'some_value'); - $deadPeer = new Memcached("165.42.42.42", "11211"); //some not existing memcache + $deadPeer = new PeclMemcached("165.42.42.42", "11211"); //some not existing memcache $slave1 = new PeclMemcached("35.143.65.241", "11211"); //some not existing memcache @@ -34,7 +34,7 @@ public function testMultiCacheAliveLast() public function testMultiCacheAliveFirst() { - $alifePeer = new Memcached("127.0.0.1", "11211"); //some existing memcached + $alifePeer = new PeclMemcached("127.0.0.1", "11211"); //some existing memcached $alifePeer->set('some_key', 'some_value'); $slave1 = new PeclMemcached("35.143.65.241", "11211"); //some not existing memcache @@ -54,7 +54,7 @@ public function testMultiCacheAliveOnly() CyclicAggregateCache::create()-> //some existing memcached setSummaryWeight(42)-> addPeer('first', new PeclMemcached("127.0.0.1", "11211"), 0)-> - addPeer('second', new Memcached("127.0.0.1", "11211"), 21); + addPeer('second', new PeclMemcached("127.0.0.1", "11211"), 21); $alifePeer->set('some_key', 'some_value'); From 9fef0ab990e99cad9e9e6673740396260719a2ba Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Mon, 21 May 2012 14:07:04 +0400 Subject: [PATCH 05/13] clean cache test crashes & some other --- test/core/PrimitiveLabelsTest.class.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/core/PrimitiveLabelsTest.class.php b/test/core/PrimitiveLabelsTest.class.php index 7d1d86f3aa..9b2b453191 100644 --- a/test/core/PrimitiveLabelsTest.class.php +++ b/test/core/PrimitiveLabelsTest.class.php @@ -46,9 +46,6 @@ public function testLabels() $this->assertEquals($customErrorLabel, $prm->getActualErrorLabel()); - $prm->import(array('someString'=>'abc')); - - } } \ No newline at end of file From c92ef773297befb37d170d9102caf81e0cedff23 Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Mon, 21 May 2012 16:09:46 +0400 Subject: [PATCH 06/13] clean --- core/Form/Form.class.php | 44 +++++++++---------- core/Form/Primitives/BasePrimitive.class.php | 31 +++++++------ core/Form/Primitives/PrimitiveAlias.class.php | 11 ++++- core/Form/Primitives/PrimitiveRule.class.php | 9 ++-- core/Form/RegulatedForm.class.php | 24 ++++++---- 5 files changed, 69 insertions(+), 50 deletions(-) diff --git a/core/Form/Form.class.php b/core/Form/Form.class.php index fbe2b93008..7f1f870b93 100644 --- a/core/Form/Form.class.php +++ b/core/Form/Form.class.php @@ -130,9 +130,9 @@ public function disableImportFiltering() /** * @return Form **/ - public function markMissing($primitiveName) + public function markMissing($name) { - return $this->markCustom($primitiveName, BasePrimitive::MISSING); + return $this->markCustom($name, BasePrimitive::MISSING); } /** @@ -148,9 +148,9 @@ public function markWrong($name) /** * @return Form **/ - public function markGood($primitiveName) + public function markGood($name) { - $this->get($primitiveName)->dropError(); + $this->get($name)->dropError(); return $this; } @@ -206,35 +206,35 @@ public function addErrorDescription($name, $errorType, $description) /** * @return Form **/ - public function addWrongLabel($primitiveName, $label) + public function addWrongLabel($name, $label) { - return $this->addErrorLabel($primitiveName, BasePrimitive::WRONG, $label); + return $this->addErrorLabel($name, BasePrimitive::WRONG, $label); } /** * @return Form **/ - public function addMissingLabel($primitiveName, $label) + public function addMissingLabel($name, $label) { - return $this->addErrorLabel($primitiveName, BasePrimitive::MISSING, $label); + return $this->addErrorLabel($name, BasePrimitive::MISSING, $label); } /** * @return Form **/ - public function addCustomLabel($primitiveName, $customMark, $label) + public function addCustomLabel($name, $customMark, $label) { - return $this->addErrorLabel($primitiveName, $customMark, $label); + return $this->addErrorLabel($name, $customMark, $label); } - public function getWrongLabel($primitiveName) + public function getWrongLabel($name) { - return $this->getErrorLabel($primitiveName, BasePrimitive::WRONG); + return $this->getErrorLabel($name, BasePrimitive::WRONG); } - public function getMissingLabel($primitiveName) + public function getMissingLabel($name) { - return $this->getErrorLabel($primitiveName, BasePrimitive::MISSING); + return $this->getErrorLabel($name, BasePrimitive::MISSING); } /** @@ -264,17 +264,17 @@ public function importMore($scope) /** * @return Form **/ - public function importOne($primitiveName, $scope) + public function importOne($name, $scope) { - return $this->importPrimitive($scope, $this->get($primitiveName)); + return $this->importPrimitive($scope, $this->get($name)); } /** * @return Form **/ - public function importValue($primitiveName, $value) + public function importValue($name, $value) { - $prm = $this->get($primitiveName); + $prm = $this->get($name); return $this->checkImportResult($prm, $prm->importValue($value)); } @@ -282,9 +282,9 @@ public function importValue($primitiveName, $value) /** * @return Form **/ - public function importOneMore($primitiveName, $scope) + public function importOneMore($name, $scope) { - $prm = $this->get($primitiveName); + $prm = $this->get($name); if (!$prm->isImported()) return $this->importPrimitive($scope, $prm); @@ -292,9 +292,9 @@ public function importOneMore($primitiveName, $scope) return $this; } - public function exportValue($primitiveName) + public function exportValue($name) { - return $this->get($primitiveName)->exportValue(); + return $this->get($name)->exportValue(); } public function export() diff --git a/core/Form/Primitives/BasePrimitive.class.php b/core/Form/Primitives/BasePrimitive.class.php index 1478331198..d24f42b7c2 100644 --- a/core/Form/Primitives/BasePrimitive.class.php +++ b/core/Form/Primitives/BasePrimitive.class.php @@ -51,7 +51,7 @@ abstract class BasePrimitive /** * Error description for each error type - * @var array of ('errorType' => 'label') + * @var array of ('errorType' => 'description') */ protected $errorDescriptions = array(); @@ -358,12 +358,15 @@ public function getErrorLabel($type) */ public function getActualErrorLabel() { - $type = $this->getError(); + if( + ($error = $this->getError()) + && $error !==null + ) { + return $this->getErrorLabel($error); + } - if($type === null) - return null; - return $this->getErrorLabel($type); + return null; } /** @@ -381,7 +384,7 @@ public function getErrorLabels() */ public function setErrorDescription($type, $val) { - $this->errorDescription[$type] = $val; + $this->errorDescriptions[$type] = $val; return $this; } @@ -392,8 +395,8 @@ public function setErrorDescription($type, $val) */ public function getErrorDescription($type) { - return (isset($this->errorDescription[$type])) - ? $this->errorDescription[$type] + return (isset($this->errorDescriptions[$type])) + ? $this->errorDescriptions[$type] : null; } @@ -402,12 +405,14 @@ public function getErrorDescription($type) */ public function getActualErrorDescription() { - $type = $this->getError(); - - if($type === null) - return null; + if( + ($error = $this->getError()) + && $error !==null + ) { + return $this->getErrorDescription($error); + } - return $this->getErrorDescription($type); + return null; } /** diff --git a/core/Form/Primitives/PrimitiveAlias.class.php b/core/Form/Primitives/PrimitiveAlias.class.php index f25ad5cd75..b1fe1279a8 100644 --- a/core/Form/Primitives/PrimitiveAlias.class.php +++ b/core/Form/Primitives/PrimitiveAlias.class.php @@ -151,11 +151,18 @@ public function import($scope) { $result = $this->primitive->import( - array($this->primitive->getName() => $scope[$this->name]) + array( + $this->primitive->getName() => $scope[$this->name] + ) ); - if($error = $this->primitive->getError()) + if( + ($error = $this->primitive->getError()) + && $error !== null + ) { $this->setError($error); + } + return $result; } diff --git a/core/Form/Primitives/PrimitiveRule.class.php b/core/Form/Primitives/PrimitiveRule.class.php index ba63eb2e11..400172d663 100644 --- a/core/Form/Primitives/PrimitiveRule.class.php +++ b/core/Form/Primitives/PrimitiveRule.class.php @@ -55,10 +55,11 @@ public function import($scope) Assert::isNotNull($this->form); Assert::isNotNull($this->expression); - $result = $this->expression->toBoolean($this->form); - - if(!$result) - $this->setError(BasePrimitive::WRONG); + if( + !($result = $this->expression->toBoolean($this->form)) + ) { + $this->markWrong(); + } return $result; } diff --git a/core/Form/RegulatedForm.class.php b/core/Form/RegulatedForm.class.php index 500c2b9b97..05e49995bd 100644 --- a/core/Form/RegulatedForm.class.php +++ b/core/Form/RegulatedForm.class.php @@ -24,9 +24,9 @@ abstract class RegulatedForm extends PlainForm public function addRule($name, LogicalObject $rule) { return $this->add( - Primitive::rule($name) - ->setExpression($rule) - ->setForm($this) + Primitive::rule($name)-> + setExpression($rule)-> + setForm($this) ); } @@ -47,7 +47,10 @@ public function dropRuleByName($name) public function ruleExists($name) { - return ($this->exists($name) && ($this->get($name) instanceof PrimitiveRule)); + return ( + $this->exists($name) + && ($this->get($name) instanceof PrimitiveRule) + ); } /** @@ -56,11 +59,14 @@ public function ruleExists($name) public function checkRules() { $primitives = $this->getPrimitiveList(); - foreach($primitives as $prm) - { - if($prm instanceof PrimitiveRule) - if (!$prm->import(null)) - $prm->markWrong(); + foreach($primitives as $prm) { + if( + $prm instanceof PrimitiveRule + && !$prm->import(null) + ) { + $prm->markWrong(); + } + } return $this; From b808948c49080fe07a4cee1d7f777720b0325997 Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Mon, 21 May 2012 18:14:10 +0400 Subject: [PATCH 07/13] add PrimitiveRuleTest --- test/core/PrimitiveRuleTest.class.php | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/core/PrimitiveRuleTest.class.php diff --git a/test/core/PrimitiveRuleTest.class.php b/test/core/PrimitiveRuleTest.class.php new file mode 100644 index 0000000000..8d3f7c9c97 --- /dev/null +++ b/test/core/PrimitiveRuleTest.class.php @@ -0,0 +1,72 @@ +add( + Primitive::string('pass') + )->add( + Primitive::string('repass') + ); + + $form->add( + Primitive::rule('correctPass')->setForm( + $form + )->setExpression( + Expression::eq( + FormField::create('pass'), + FormField::create('repass') + ) + ) + ); + + $scope = array( + 'pass' => '12345', + 'repass' => '1234', + ); + + $form->import($scope); + + $this->assertEmpty( + $form->getErrors() + ); + + $form->checkRules(); + + $this->assertNotNull($form->getError('correctPass')); + + $errors = $form->getErrors(); + + $this->assertTrue(isset($errors['correctPass'])); + $this->assertEquals(BasePrimitive::WRONG, $errors['correctPass']); + + $form->clean(); + + $this->assertEmpty($form->getErrors()); + + $scope = array( + 'pass' => '12345', + 'repass' => '12345', + ); + + $form->import($scope); + + $this->assertEmpty($form->getErrors()); + + $form->checkRules(); + + $this->assertEmpty($form->getErrors()); + + } + } +?> \ No newline at end of file From b50a2864ccd7e394a39ff9c92f0b9984f594ee81 Mon Sep 17 00:00:00 2001 From: Georgy Kutsurua Date: Tue, 22 May 2012 21:11:33 +0400 Subject: [PATCH 08/13] add in meta generation logic by label & description --- main/Base/LightMetaProperty.class.php | 58 +++++++++++++++------- meta/classes/MetaClassProperty.class.php | 63 ++++++++++++++++++++---- meta/classes/MetaConfiguration.class.php | 9 +++- meta/dtd/meta.dtd | 2 + meta/types/BasePropertyType.class.php | 52 ++++++++++++++++--- test/main/ProtoTest.class.php | 59 ++++++++++++++++++++++ test/meta/config.meta.xml | 2 +- 7 files changed, 207 insertions(+), 38 deletions(-) create mode 100644 test/main/ProtoTest.class.php diff --git a/main/Base/LightMetaProperty.class.php b/main/Base/LightMetaProperty.class.php index 548e33963a..2b589c57a7 100644 --- a/main/Base/LightMetaProperty.class.php +++ b/main/Base/LightMetaProperty.class.php @@ -46,32 +46,35 @@ class LightMetaProperty implements Stringable ) ); - private $name = null; - private $columnName = null; + private $name = null; + private $columnName = null; - private $type = null; - private $className = null; + private $type = null; + private $className = null; - private $size = null; + private $size = null; - private $min = null; - private $max = null; + private $min = null; + private $max = null; - private $required = false; - private $generic = false; - private $inner = false; + private $required = false; + private $generic = false; + private $inner = false; /// @see MetaRelation - private $relationId = null; + private $relationId = null; /// @see FetchStrategy - private $strategyId = null; + private $strategyId = null; - private $getter = null; - private $setter = null; - private $dropper = null; + private $getter = null; + private $setter = null; + private $dropper = null; - private $identifier = null; + private $identifier = null; + + private $label = null; + private $description = null; /** * @return LightMetaProperty @@ -89,10 +92,13 @@ public static function create() public static function fill( LightMetaProperty $property, $name, $columnName, $type, $className, $size, - $required, $generic, $inner, $relationId, $strategyId + $required, $generic, $inner, $relationId, $strategyId, $label=null, $description=null ) { $property->name = $name; + + $property->label = $label; + $property->description = $description; $methodSuffix = ucfirst($name); $property->getter = 'get'.$methodSuffix; @@ -309,6 +315,12 @@ public function makePrimitive($name) if ($this->required) $prm->required(); + + if($this->label) + $prm->setLabel($this->label); + + if($this->description) + $prm->setDescription($this->description); return $prm; } @@ -505,6 +517,18 @@ final public function toString() ? $this->strategyId : 'null' ) + .', ' + .( + $this->label + ? '\''.$this->label.'\'' + : 'null' + ) + .', ' + .( + $this->description + ? '\''.$this->description.'\'' + : 'null' + ) .')'; } diff --git a/meta/classes/MetaClassProperty.class.php b/meta/classes/MetaClassProperty.class.php index b8572961ca..a99f106ebd 100644 --- a/meta/classes/MetaClassProperty.class.php +++ b/meta/classes/MetaClassProperty.class.php @@ -14,20 +14,23 @@ **/ class MetaClassProperty { - private $class = null; + private $class = null; - private $name = null; - private $columnName = null; + private $name = null; + private $columnName = null; - private $type = null; - private $size = null; + private $type = null; + private $size = null; - private $required = false; - private $identifier = false; + private $required = false; + private $identifier = false; - private $relation = null; + private $relation = null; - private $strategy = null; + private $strategy = null; + + private $label = null; + private $description = null; public function __construct( $name, @@ -250,6 +253,44 @@ public function getFetchStrategyId() return null; } + + /** + * @param $label + * @return MetaClassProperty + */ + public function setLabel($label) + { + $this->label = $label; + + return $this; + } + + /** + * @return null|string + */ + public function getLabel() + { + return $this->label; + } + + /** + * @param $description + * @return MetaClassProperty + */ + public function setDescription($description) + { + $this->description = $description; + + return $this; + } + + /** + * @return null|string + */ + public function getDescription() + { + return $this->description; + } public function toMethods( MetaClass $class, @@ -423,7 +464,9 @@ public function toLightProperty(MetaClass $holder) $this->getType()->isGeneric(), $inner, $this->getRelationId(), - $this->getFetchStrategyId() + $this->getFetchStrategyId(), + $this->getLabel(), + $this->getDescription() ) ); } diff --git a/meta/classes/MetaConfiguration.class.php b/meta/classes/MetaConfiguration.class.php index 0da7c4c786..dc9f8b654e 100644 --- a/meta/classes/MetaConfiguration.class.php +++ b/meta/classes/MetaConfiguration.class.php @@ -786,7 +786,7 @@ private function addSource(SimpleXMLElement $source) /** * @return MetaClassProperty **/ - private function makeProperty($name, $type, MetaClass $class, $size) + private function makeProperty($name, $type, MetaClass $class, $size, $label=null, $description=null) { Assert::isFalse( strpos($name, '_'), @@ -823,6 +823,9 @@ private function makeProperty($name, $type, MetaClass $class, $size) 'size is required for "'.$property->getName().'"' ); } + + $property->setLabel($label); + $property->setDescription($description); return $property; } @@ -1120,7 +1123,9 @@ private function processClasses(SimpleXMLElement $xml, $metafile, $generate) (string) $xmlProperty['name'], (string) $xmlProperty['type'], $class, - (string) $xmlProperty['size'] + (string) $xmlProperty['size'], + (string) $xmlProperty['label'], + (string) $xmlProperty['description'] ); if (isset($xmlProperty['column'])) { diff --git a/meta/dtd/meta.dtd b/meta/dtd/meta.dtd index 0eaa67897f..1306495fee 100644 --- a/meta/dtd/meta.dtd +++ b/meta/dtd/meta.dtd @@ -38,6 +38,8 @@ required (true|false) "false" relation (OneToOne|OneToMany|ManyToMany) #IMPLIED fetch (lazy|cascade) #IMPLIED + label CDATA #IMPLIED + description CDATA #IMPLIED > diff --git a/meta/types/BasePropertyType.class.php b/meta/types/BasePropertyType.class.php index 04049505a9..8f3bfd79c8 100644 --- a/meta/types/BasePropertyType.class.php +++ b/meta/types/BasePropertyType.class.php @@ -66,9 +66,29 @@ public function toGetter( $name = $property->getName(); $methodName = 'get'.ucfirst($property->getName()); - + + $doc = '/**'."\n"; + $doced = null; + + if($label = $property->getLabel()) { + $doc .= ' * '.$label."\n"; + $doced = true; + } + + if($desc = $property->getDescription()) { + $doc.=" *\n"; + $doc.=' * '.$desc."\n"; + $doced = true; + } + + $doc .= "**/"; + + if($doced===null) + $doc = ''; + return <<{$name}; @@ -85,13 +105,31 @@ public function toSetter( { $name = $property->getName(); $methodName = 'set'.ucfirst($name); - + + $doc = '/**'."\n"; + $doced = false; + + if($label = $property->getLabel()) { + $doc .= ' * '.$label."\n"; + } + + if($desc = $property->getDescription()) { + $doc.=" *\n"; + $doc.=' * '.$desc."\n"; + } + + if($holder) { + $doc .= ' * @return '.$holder->getClass()->getName()."\n"; + } else { + $doc .= ' * @return '.$class->getName()."\n"; + } + + $doc .= "**/"; + if ($holder) { return <<getClass()->getName()} -**/ +{$doc} public function {$methodName}(\${$name}) { \$this->{$holder->getName()}->{$methodName}(\${$name}); @@ -103,9 +141,7 @@ public function {$methodName}(\${$name}) } else { return <<getName()} -**/ +{$doc} public function {$methodName}(\${$name}) { \$this->{$name} = \${$name}; diff --git a/test/main/ProtoTest.class.php b/test/main/ProtoTest.class.php new file mode 100644 index 0000000000..a07856f74b --- /dev/null +++ b/test/main/ProtoTest.class.php @@ -0,0 +1,59 @@ +makePrimitive($name); + + $this->assertEquals( + $size, + $prm->getMax() + ); + + $this->assertEquals( + $label, + $prm->getLabel() + ); + + $this->assertEquals( + $description, + $prm->getDescription() + ); + + $this->assertEquals( + $name, + $prm->getName() + ); + } + + } diff --git a/test/meta/config.meta.xml b/test/meta/config.meta.xml index ba98dc7840..0c09935ea4 100644 --- a/test/meta/config.meta.xml +++ b/test/meta/config.meta.xml @@ -7,7 +7,7 @@ - + From b508323107c31e266f1e308b29b5eef21d1f4d98 Mon Sep 17 00:00:00 2001 From: Georgiy Kutsurua Date: Thu, 20 Dec 2012 23:48:34 +0400 Subject: [PATCH 09/13] Add travis configuration files --- .travis.yml | 20 ++ .travis/install_extensions.sh | 14 ++ core/Form/Form.class.php | 212 ++++--------------- core/Form/Primitives/BasePrimitive.class.php | 36 ++-- test/config.inc.php.tpl | 16 +- 5 files changed, 102 insertions(+), 196 deletions(-) create mode 100644 .travis.yml create mode 100755 .travis/install_extensions.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..2910aea474 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,20 @@ +language: php +php: + - "5.3" + + +services: + - rabbitmq + - memcached + - mysql + - postgresql + - sqlite3 + +before_script: + - ./.travis/install_extensions.sh + - psql -c "DROP DATABASE IF EXISTS onphp;" -U postgres + - psql -c 'create database onphp;' -U postgres + - mysql -e "create database IF NOT EXISTS onphp;" -uroot; + + +script: ./test/runme.sh \ No newline at end of file diff --git a/.travis/install_extensions.sh b/.travis/install_extensions.sh new file mode 100755 index 0000000000..172e714031 --- /dev/null +++ b/.travis/install_extensions.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +EXT_PATH = `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` + +echo "Installing memcahe" +pecl install memcache +echo "extension=memcache.so" >> $(EXT_PATH) + + +echo "Installing memcahed" +pecl install memcached +echo "extension=memcached.so" >> $(EXT_PATH) + + diff --git a/core/Form/Form.class.php b/core/Form/Form.class.php index f94abc892e..b4f9813899 100644 --- a/core/Form/Form.class.php +++ b/core/Form/Form.class.php @@ -1,12 +1,12 @@ errors, $this->violated); + $errors = array(); + foreach($this->primitives as $name => $prm) { + if (null !== $prm->getError()) + $errors[$name] = $prm->getError(); + } + + return $errors; } public function hasError($name) { - return array_key_exists($name, $this->errors) - || array_key_exists($name, $this->violated); + return $this->get($name)->getError() !== null; } 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]; + if ($this->hasError($name)) { + return $this->get($name)->getError(); } + return null; } @@ -87,9 +87,10 @@ public function getInnerErrors() **/ public function dropAllErrors() { - $this->errors = array(); - $this->violated = array(); - + foreach($this->primitives as $name => $prm) { + $prm->dropError(); + } + return $this; } @@ -122,7 +123,7 @@ public function disableImportFiltering() **/ public function markMissing($primitiveName, $label = null) { - return $this->markCustom($primitiveName, Form::MISSING, $label); + return $this->markCustom($primitiveName, BasePrimitive::MISSING, $label); } /** @@ -132,17 +133,11 @@ public function markMissing($primitiveName, $label = null) **/ 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' - ); + $prm = $this->get($name); + $prm->markWrong(); if ($label !== null) - $this->addWrongLabel($name, $label); + $prm->setWrongLabel($label); return $this; } @@ -152,14 +147,7 @@ public function markWrong($name, $label = null) **/ 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' - ); + $this->get($primitiveName)->markGood(); return $this; } @@ -171,12 +159,7 @@ public function markGood($primitiveName) **/ public function markCustom($primitiveName, $customMark, $label = null) { - Assert::isInteger($customMark); - - $this->errors[$this->get($primitiveName)->getName()] = $customMark; - - if ($label !== null) - $this->addCustomLabel($primitiveName, $customMark, $label); + $this->get($primitiveName)->setErrorLabel($customMark, $label); return $this; } @@ -189,9 +172,9 @@ public function getTextualErrors() { $list = array(); - foreach (array_keys($this->labels) as $name) { + foreach ($this->primitives as $name => $prm) { if ($label = $this->getTextualErrorFor($name)) - $list[] = $label; + $list[$name] = $label; } return $list; @@ -199,42 +182,12 @@ 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(); } /** @@ -242,17 +195,8 @@ 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->describedLabels[$name][$errorType] = $description; - + $this->get($name)->setErrorDescription($errorType, $description); + return $this; } @@ -261,7 +205,7 @@ 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); } /** @@ -269,7 +213,7 @@ public function addWrongLabel($primitiveName, $label) **/ public function addMissingLabel($primitiveName, $label) { - return $this->addErrorLabel($primitiveName, Form::MISSING, $label); + return $this->addErrorLabel($primitiveName, BasePrimitive::MISSING, $label); } /** @@ -282,12 +226,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); } /** @@ -327,9 +271,9 @@ public function importOne($primitiveName, $scope) **/ public function importValue($primitiveName, $value) { - $prm = $this->get($primitiveName); - - return $this->checkImportResult($prm, $prm->importValue($value)); + $this->get($primitiveName)->importValue($value); + + return $this; } /** @@ -395,60 +339,8 @@ public function getProto() **/ private function importPrimitive($scope, BasePrimitive $prm) { - if (!$this->importFiltering) { - if ($prm instanceof FiltrablePrimitive) { - - $chain = $prm->getImportFilter(); - - $prm->dropImportFilters(); - - $result = $this->checkImportResult( - $prm, - $prm->import($scope) - ); - - $prm->setImportFilter($chain); - - return $result; - - } elseif ($prm instanceof PrimitiveForm) { - return $this->checkImportResult( - $prm, - $prm->unfilteredImport($scope) - ); - } - } - - return $this->checkImportResult($prm, $prm->import($scope)); - } - - /** - * @return Form - **/ - private function checkImportResult(BasePrimitive $prm, $result) - { - 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; - - } elseif (true === $result) { - unset($this->errors[$name]); - - } elseif ($error = $prm->getCustomError()) { - - $this->errors[$name] = $error; - - } else - $this->errors[$name] = self::WRONG; - + $prm->import($scope); + return $this; } @@ -464,28 +356,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); } } ?> \ No newline at end of file diff --git a/core/Form/Primitives/BasePrimitive.class.php b/core/Form/Primitives/BasePrimitive.class.php index d24f42b7c2..52008dc7a7 100644 --- a/core/Form/Primitives/BasePrimitive.class.php +++ b/core/Form/Primitives/BasePrimitive.class.php @@ -1,12 +1,12 @@ name]) - || ( - isset($scope[$this->name]) - && $scope[$this->name] !== '' - ) - ) { - $this->raw = $scope[$this->name]; - - return $this->imported = true; - } - - $this->clean(); - - return null; + if (isset($scope[$this->name])) + { + $this->setRawValue( + $scope[$this->name] + ); + $this->imported = true; + } + + return $this; } } ?> \ No newline at end of file diff --git a/test/config.inc.php.tpl b/test/config.inc.php.tpl index 683dee0bf6..cae04e0213 100644 --- a/test/config.inc.php.tpl +++ b/test/config.inc.php.tpl @@ -2,22 +2,22 @@ $dbs = array( 'PgSQL' => array( - 'user' => 'onphp', - 'pass' => 'onphp', + 'user' => 'postgres', + 'pass' => '', 'host' => '127.0.0.1', 'base' => 'onphp' ), 'MySQL' => array( - 'user' => 'onphp', - 'pass' => 'onphp', + 'user' => 'root', + 'pass' => '', 'host' => '127.0.0.1', 'base' => 'onphp' ), 'SQLitePDO' => array( - 'user' => 'onphp', - 'pass' => 'onphp', - 'host' => '127.0.0.1', - 'base' => 'onphp' + 'user' => '', + 'pass' => '', + 'host' => '', + 'base' => ':memory:' ), ); From eaec0a3669c894a4b4a598f595a0e68b1cc2411c Mon Sep 17 00:00:00 2001 From: Georgiy Kutsurua Date: Thu, 20 Dec 2012 23:54:53 +0400 Subject: [PATCH 10/13] Add travis configuration files --- .travis.yml | 2 +- .travis/install_extensions.sh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2910aea474..7ceffbc932 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ services: - memcached - mysql - postgresql - - sqlite3 + - sqlite before_script: - ./.travis/install_extensions.sh diff --git a/.travis/install_extensions.sh b/.travis/install_extensions.sh index 172e714031..11d2d8b2fc 100755 --- a/.travis/install_extensions.sh +++ b/.travis/install_extensions.sh @@ -1,14 +1,14 @@ #!/bin/bash -EXT_PATH = `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` +PHP_INI_PATH = `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` echo "Installing memcahe" pecl install memcache -echo "extension=memcache.so" >> $(EXT_PATH) +echo "extension=memcache.so" >> $(PHP_INI_PATH) echo "Installing memcahed" pecl install memcached -echo "extension=memcached.so" >> $(EXT_PATH) +echo "extension=memcached.so" >> $(PHP_INI_PATH) From 54a8c120dfa17dd7dd9525bb0ecc7383e323940d Mon Sep 17 00:00:00 2001 From: Georgiy Kutsurua Date: Fri, 21 Dec 2012 00:16:58 +0400 Subject: [PATCH 11/13] Add travis configuration files --- .travis/install_extensions.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis/install_extensions.sh b/.travis/install_extensions.sh index 11d2d8b2fc..3ec6f9fb32 100755 --- a/.travis/install_extensions.sh +++ b/.travis/install_extensions.sh @@ -1,14 +1,14 @@ #!/bin/bash -PHP_INI_PATH = `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` +PHP_INI_PATH=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||") echo "Installing memcahe" -pecl install memcache -echo "extension=memcache.so" >> $(PHP_INI_PATH) +printf "\n" | pecl install memcache +echo "extension=memcache.so" >> $PHP_INI_PATH echo "Installing memcahed" -pecl install memcached -echo "extension=memcached.so" >> $(PHP_INI_PATH) +printf "\n" | pecl install memcached +echo "extension=memcached.so" >> $PHP_INI_PATH From c2b7c4e8c09c9cdbd6ece40e985fd35d4a37a72c Mon Sep 17 00:00:00 2001 From: Georgiy Kutsurua Date: Fri, 21 Dec 2012 00:25:06 +0400 Subject: [PATCH 12/13] Add travis configuration files --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7ceffbc932..a300049e82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,10 +8,9 @@ services: - memcached - mysql - postgresql - - sqlite + before_script: - - ./.travis/install_extensions.sh - psql -c "DROP DATABASE IF EXISTS onphp;" -U postgres - psql -c 'create database onphp;' -U postgres - mysql -e "create database IF NOT EXISTS onphp;" -uroot; From a857e520ad25b4a259d5e1a58718ea70c1b2cb78 Mon Sep 17 00:00:00 2001 From: Georgiy Kutsurua Date: Tue, 25 Dec 2012 02:04:47 +0400 Subject: [PATCH 13/13] Form errors --- .travis.yml | 19 ------------------- .travis/install_extensions.sh | 14 -------------- core/Form/Form.class.php | 18 ++++-------------- 3 files changed, 4 insertions(+), 47 deletions(-) delete mode 100644 .travis.yml delete mode 100755 .travis/install_extensions.sh diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a300049e82..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,19 +0,0 @@ -language: php -php: - - "5.3" - - -services: - - rabbitmq - - memcached - - mysql - - postgresql - - -before_script: - - psql -c "DROP DATABASE IF EXISTS onphp;" -U postgres - - psql -c 'create database onphp;' -U postgres - - mysql -e "create database IF NOT EXISTS onphp;" -uroot; - - -script: ./test/runme.sh \ No newline at end of file diff --git a/.travis/install_extensions.sh b/.travis/install_extensions.sh deleted file mode 100755 index 3ec6f9fb32..0000000000 --- a/.travis/install_extensions.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -PHP_INI_PATH=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||") - -echo "Installing memcahe" -printf "\n" | pecl install memcache -echo "extension=memcache.so" >> $PHP_INI_PATH - - -echo "Installing memcahed" -printf "\n" | pecl install memcached -echo "extension=memcached.so" >> $PHP_INI_PATH - - diff --git a/core/Form/Form.class.php b/core/Form/Form.class.php index b4f9813899..bafb549ab9 100644 --- a/core/Form/Form.class.php +++ b/core/Form/Form.class.php @@ -52,11 +52,7 @@ public function hasError($name) public function getError($name) { - if ($this->hasError($name)) { - return $this->get($name)->getError(); - } - - return null; + return $this->get($name)->getError(); } public function getInnerErrors() @@ -65,17 +61,11 @@ public function getInnerErrors() foreach ($this->primitives as $name => $prm) { if ( - ( - ($prm instanceof PrimitiveFormsList) - || ($prm instanceof PrimitiveForm) - ) - && $prm->getValue() + $prm instanceof PrimitiveFormsList + || $prm instanceof PrimitiveForm ) { - if ($errors = $prm->getInnerErrors()) { + if ($errors = $prm->getInnerErrors()) $result[$name] = $errors; - } else { - unset($result[$name]); - } } }