Esempio n. 1
0
 /**
  * Check that the spec matches and overlay help messaged.
  *
  * The resulting SpecResult instance should have more user-friendly
  * messages. This allows one to use Specs for validation on a website or
  * even an API.
  *
  * @param array $input
  *
  * @return SpecResult
  */
 public function check(array $input)
 {
     $result = $this->spec->check($input);
     return new SpecResult($result->getMissing(), Arr::walkCopy($result->getFailed(), function ($key, $value, &$array, $path) {
         $array[$key] = Std::coalesce(Std::firstBias(Arr::dotGet($this->messages, Std::nonempty($path, $key)) !== null, [Arr::dotGet($this->messages, Std::nonempty($path, $key))], null), Std::firstBias($value instanceof AbstractConstraint, function () use($value) {
             return $value->getDescription();
         }, null), Std::firstBias(is_array($value), function () use($value) {
             return array_map(function (AbstractConstraint $item) {
                 return $item->getDescription();
             }, $value);
         }, $value));
     }, true, '', false), $result->getStatus());
 }
Esempio n. 2
0
 /**
  * Render the Node.
  *
  * @throws CoreException
  * @throws InvalidAttributesException
  * @return string
  */
 public function render()
 {
     $result = $this->spec->check($this->attributes);
     if ($result->failed()) {
         throw new InvalidAttributesException($result);
     }
     $attributes = $this->renderAttributes();
     if (strlen($attributes)) {
         $attributes = ' ' . $attributes;
     }
     if ($this->selfClosing) {
         return vsprintf('<%s%s/>', [$this->tagName, $attributes]);
     }
     return vsprintf('<%s%s>%s</%s>', [$this->tagName, $attributes, $this->renderContent(), $this->tagName]);
 }
Esempio n. 3
0
 public function testCheckWithInvalidConstraint()
 {
     $this->setExpectedException(CoreException::class, 'Unexpected constraint type: array.');
     $spec = new Spec(['somefield' => [['invalid'], 'wow', 1337]]);
     $spec->check(['somefield' => 'wowo']);
 }