Search the Community
Showing results for tags 'pattern'.
-
XSD <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:simpleType name="MgmtCodetyp"> <xsd:restriction base="xsd:string"> <xsd:pattern value="[A-Z][A-Z]([A-Z]|[0-9])"></xsd:pattern> </xsd:restriction> </xsd:simpleType> <xsd:element name="MgmtCode" type="MgmtCodetyp"></xsd:element> </xsd:schema> XML <MgmtCode>AGF</MgmtCode> PHP Code <?php $file = "data.xml"; function libxml_display_error($error) { $return = "\n"; switch ($error->level) { case LIBXML_ERR_ERROR: $return .= "[ERROR] "; break; case LIBXML_ERR_FATAL: $return .= "[FATAL] "; break; default: $return .= "[uNKNOWN] "; } $return .= trim($error->message); $return .= " [line: $error->line]\n"; return $return; } function libxml_display_errors() { $errors = libxml_get_errors(); foreach ($errors as $error) { if ($error -> level != LIBXML_ERR_WARNING) { print libxml_display_error($error); } } libxml_clear_errors(); } // Enable user error handling libxml_use_internal_errors(true); $xml = new DOMDocument; $xml -> load("kkk.xml"); if (!$xml -> schemaValidate("kkk.xsd")) { libxml_display_errors(); } else { print "no error\n"; } ?> OUTPUT [ERROR] Element 'MgmtCode': [facet 'pattern'] The value 'AGF' is not accepted by the pattern '[A-Z][A-Z]([A-Z]|[0-9])'. [line: 1] [ERROR] Element 'MgmtCode': 'AGF' is not a valid value of the atomic type 'MgmtCodetyp'. [line: 1] ANALYSIS The XML is a perfectly valid xml. I don't understand why it failed the XSD.
-
Let's suppose we have some objects hierarchy built via composition. Is there any pattern to register business logic errors within inner objects and pass them to the upper objects ? I am just tired of all these addErrors, getErrors on each level and I feel that I do something incorrectly. I know about exceptions, but I have no mind how to use them in such case. Business logic error is technically not a critical situation, which should result in interruption of the program execution. Actually interruption is not supposed at all, because we need to register the error and just move on. Thanks in advance to share your wisdom and knowledge ) Small snippet to illustrate the problem: <?php class ErrorContainer { private $errors = array(); function addError($error) { if (!is_array($error)) { $this->errors[] = $error;} else { $this->errors = array_merge($this->errors, $error); } } function getErrors() { return $this->errors[]; } function hasErrors() { return !empty($this->errors); } } class Processor extends ErrorContainer { function process($account_id, $orders) { $account = new Account(); if (!$account->loadById($account_id)) { $this->addErrors($account->getErrors);} foreach ($orders as $order_id) { $order = new Order(); if (!$order->loadById($order_id)) { $this->addErrors($order->getErrors);} } } return $this->hasErrors(); } class Account extends ErrorContainer { function loadById($account_id) { $account = select_from_database($account_id); if (!$account) { $this->addError("Account is missing"); } if (!$account['active']) { $this->addError("Account is inactive"); } // and so on, some checks may add errors in a cycle return $this->hasErrors(); } } class Order extends ErrorContainer {} // very similar to Account, but has its own checks //Usage: $errors = array(); $items = load_items_from_xml($xml); foreach ($items as $item) { $processor = new Processor(); if (!$processor->process($item['account_id'], $item['orders'])) { $errors = array_merge($errors, $processor->getErrors()); } }
- 3 replies
-
- error
- business logic
-
(and 1 more)
Tagged with: