Exemple #1
0
 /**
  * Post simple validation.
  *
  * @param   bool   $verdict             Verdict.
  * @param   bool   &$value              Value.
  * @param   \Hoa\Xyl\Interpreter\Html\Concrete  $element    Element that
  *                                                          requires it.
  * @param   bool   $postVerification    Whether we run post-verification or
  *                                      not.
  * @return  bool
  */
 public static function postValidation($verdict, &$value, Concrete $element, $postVerification = true)
 {
     // Order is important.
     $validates = [];
     if (true === $element->abstract->attributeExists('validate')) {
         $validates['@'] = $element->abstract->readAttribute('validate');
     }
     $validates = array_merge($validates, $element->abstract->readCustomAttributes('validate'));
     if (empty($validates)) {
         if (true === $postVerification) {
             static::postVerification($verdict, $element);
         }
         return $verdict;
     }
     // Order is not important.
     $errors = $element->abstract->readCustomAttributesAsList('error');
     if (true === $element->abstract->attributeExists('error')) {
         $errors['@'] = $element->abstract->readAttributeAsList('error');
     }
     if (ctype_digit($value)) {
         $value = (int) $value;
     } elseif (is_numeric($value)) {
         $value = (double) $value;
     }
     $decision = true;
     foreach ($validates as $name => $realdom) {
         $praspel = Praspel::interpret('@requires i: ' . $realdom . ';');
         $clause = $praspel->getClause('requires');
         $variable = $clause['i'];
         $decision = $variable->predicate($value);
         if ('@' === $name) {
             $decision = $verdict && $decision;
         }
         if (true === $decision) {
             unset($errors[$name]);
             continue;
         }
         if (!isset($errors[$name])) {
             continue;
         }
         $handle = $element->xpath('//__current_ns:error[@id="' . implode('" or @id="', $errors[$name]) . '"]');
         foreach ($handle as $error) {
             $element->getConcreteElement($error)->setVisibility(true);
         }
         unset($errors[$name]);
         break;
     }
     $verdict = $decision;
     if (true === $postVerification) {
         static::postVerification($verdict, $element, isset($errors['@']));
     }
     return $verdict;
 }
 /**
  * Invoke the encapsulation shunter.
  *
  * @param   \Hoa\Praspel\Preambler\Handler  $preambler    Preambler.
  * @return  void
  * @throws  \Hoa\Praspel\Exception\Preambler
  */
 public function __invoke(Handler $preambler)
 {
     $callable = $preambler->__getCallable();
     $reflection = $callable->getReflection();
     $registry = Praspel::getRegistry();
     if ($reflection instanceof \ReflectionClass) {
         $_object = $reflection->newInstance();
         $preambler->__setCallable(xcallable($_object, '__construct'));
     } elseif (!$reflection instanceof \ReflectionMethod) {
         throw new Praspel\Exception\Preambler('The callable must be a class and a (dynamic) method name.', 0);
     } else {
         $callback = $callable->getValidCallback();
         if (!is_object($callback[0])) {
             $reflectionClass = $reflection->getDeclaringClass();
             $_reflectionClass = $reflectionClass;
             while (null === ($constructor = $_reflectionClass->getConstructor()) && false !== ($_reflectionClass = $_reflectionClass->getParentClass())) {
             }
             if (null === $constructor) {
                 $_object = $reflectionClass->newInstance();
             } else {
                 $className = $_reflectionClass->getName();
                 $id = $className . '::__construct';
                 if (!isset($registry[$id])) {
                     $registry[$id] = Praspel::interpret(Praspel::extractFromComment($constructor->getDocComment()), $className);
                 }
                 $assertionChecker = $this->getAssertionChecker();
                 if (null === $assertionChecker) {
                     $assertionChecker = '\\Hoa\\Praspel\\AssertionChecker';
                 }
                 $arguments = $assertionChecker::generateData($registry[$id]);
                 $_object = $reflectionClass->newInstanceArgs($arguments);
             }
             $preambler->__setCallable(xcallable($_object, $callback[1]));
         }
     }
     $reflectionObject = $preambler->__getReflectionObject($object);
     $className = $reflectionObject->getName();
     $properties = $reflectionObject->getProperties();
     foreach ($properties as $property) {
         $propertyName = $property->getName();
         $id = $className . '::$' . $propertyName;
         if (false === isset($registry[$id])) {
             $registry[$id] = Praspel::interpret(Praspel::extractFromComment($property->getDocComment()), $className);
         }
         $specification = $registry[$id];
         if (false === $specification->clauseExists('invariant')) {
             throw new Praspel\Exception\Preambler('Cannot generate a value from %s because it has no ' . '@invariant clause.', 1, $id);
         }
         $preambler->{$propertyName} = $specification->getClause('invariant')->getVariable($propertyName)->sample();
     }
     return;
 }
Exemple #3
0
 /**
  * Set a value to a specific offset of the current attribute.
  *
  * @access  public
  * @param   int    $offset    Offset.
  * @param   mixed  $value     Value.
  * @return  bool
  * @throw   \Hoa\Model\Exception
  */
 public function offsetSet($offset, $value)
 {
     if (false === $this->isValidationEnabled()) {
         $this->{$this->__currentAccess}[$offset] = $value;
         return null;
     }
     $oldOffset = false !== $this->_offsetExists($offset) ? $this->{$this->__currentAccess}[$offset] : null;
     $this->{$this->__currentAccess}[$offset] = $value;
     $name = substr($this->__currentAccess, 1);
     $attribute =& $this->getAttribute($name);
     if (false !== $attribute['comment']) {
         if (null === $attribute['contract']) {
             $attribute['contract'] = \Hoa\Praspel::interpret($attribute['comment']);
         }
         $verdict = $attribute['contract']->getClause('invariant')->getVariable($name)->predicate($this->{$this->__currentAccess});
     } else {
         $verdict = true;
     }
     if (false === $verdict) {
         if (null !== $oldOffset) {
             $this->{$this->__currentAccess}[$offset] = $oldOffset;
         } else {
             unset($this->{$this->__currentAccess}[$offset]);
         }
         throw new Exception('Try to set the %s attribute with an invalid data.', 2, $name);
     }
     if (null !== ($validator = $attribute['validator']) && false === $this->{$validator}($value)) {
         if (null !== $oldOffset) {
             $this->{$this->__currentAccess}[$offset] = $oldOffset;
         }
         throw new Exception('Try to set the %s attribute with an invalid data.', 3, $name);
     }
     return $this->__currentAccess = null;
 }