Skip to content

Миграция ошибки из Формы в Примитив #101

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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
14 changes: 14 additions & 0 deletions .travis/install_extensions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/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


212 changes: 45 additions & 167 deletions core/Form/Form.class.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/****************************************************************************
* Copyright (C) 2004-2009 by Konstantin V. Arkhipov, Anton E. Lebedevich *
* *
* *
* 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. *
* *
* 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. *
* *
****************************************************************************/

/**
Expand All @@ -19,12 +19,8 @@
**/
final class Form extends RegulatedForm
{
const WRONG = 0x0001;
const MISSING = 0x0002;

private $errors = array();
private $labels = array();
private $describedLabels = array();
const WRONG = BasePrimitive::WRONG;
const MISSING = BasePrimitive::MISSING;

private $proto = null;

Expand All @@ -40,22 +36,26 @@ public static function create()

public function getErrors()
{
return array_merge($this->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;
Copy link
Contributor

Choose a reason for hiding this comment

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

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

}

Expand Down Expand Up @@ -87,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;
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -189,70 +172,31 @@ 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;
}

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

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

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

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

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

$this->get($name)->setErrorDescription($errorType, $description);

return $this;
}

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

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

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
}
?>
Loading