/** * @param mixed $value * @param string Field $field * @param string $message */ public function __construct($value, Field $field, $message = null) { $this->value = $value; $this->field = $field; $message = sprintf('Failed to encode [%s] for field [%s]. Detail: %s', is_scalar($this->value) ? $this->value : StringUtils::varToString($this->value), $this->field->getName(), $message); parent::__construct($message); }
/** * @param string $slug * @throws InvalidArgumentException */ protected function __construct($slug) { if (!is_string($slug)) { throw new InvalidArgumentException(sprintf('String expected but got [%s].', StringUtils::varToString($slug))); } if (!SlugUtils::isValid($slug, true) || !SlugUtils::containsDate($slug)) { throw new InvalidArgumentException(sprintf('The value [%s] is not a valid dated slug.', $slug)); } $this->slug = $slug; }
/** * @param string $string * @throws InvalidArgumentException */ protected function __construct($string) { if (!is_string($string)) { throw new InvalidArgumentException(sprintf('String expected but got [%s].', StringUtils::varToString($string))); } $this->string = trim((string) $string); if (empty($this->string)) { throw new InvalidArgumentException('String cannot be empty.'); } }
/** * @param string $slug * @throws \InvalidArgumentException */ protected function __construct($slug) { @trigger_error(sprintf('"%s" is deprecated. Use "Gdbots\\Pbj\\WellKnown\\DatedSlugIdentifier" from "gdbots/pbj" 1.1.x or later instead.', __CLASS__), E_USER_DEPRECATED); if (!is_string($slug)) { throw new \InvalidArgumentException(sprintf('String expected but got [%s].', StringUtils::varToString($slug))); } if (!SlugUtils::isValid($slug, true) || !SlugUtils::containsDate($slug)) { throw new \InvalidArgumentException(sprintf('The value [%s] is not a valid dated slug.', $slug)); } $this->slug = $slug; }
/** * @param string $string * @throws \InvalidArgumentException */ protected function __construct($string) { @trigger_error(sprintf('"%s" is deprecated. Use "Gdbots\\Pbj\\WellKnown\\StringIdentifier" from "gdbots/pbj" 1.1.x or later instead.', __CLASS__), E_USER_DEPRECATED); if (!is_string($string)) { throw new \InvalidArgumentException(sprintf('String expected but got [%s].', StringUtils::varToString($string))); } $this->string = trim((string) $string); if (empty($this->string)) { throw new \InvalidArgumentException('String cannot be empty.'); } }
public function testAddInvalidTypeToMap() { // todo: refactor, this test is weird and confusing $shouldWork = MapsMessage::create(); $shouldFail = clone $shouldWork; /* * some int types won't fail because they're all ints of course, just different ranges. * e.g. an Int is also all other unsigned ints (except BigInt but that's a class so we're fine) */ $allInts = ['TinyInt', 'SmallInt', 'MediumInt', 'Int', 'SignedTinyInt', 'SignedSmallInt', 'SignedMediumInt', 'SignedInt', 'Timestamp']; $allStrings = ['Binary', 'Blob', 'MediumBlob', 'MediumText', 'String', 'Text']; foreach ($shouldWork::getAllTypes() as $type => $class) { foreach ($this->getTypeValues() as $k => $v) { $thrown = false; if ($type == $k) { if (is_array($v)) { $shouldWork->addToMap($type, 'test1', $v[0]); $shouldWork->addToMap($type, 'test2', $v[1]); } else { $shouldWork->addToMap($type, 'test1', $v); } continue; } try { if (is_array($v)) { $shouldFail->addToMap($type, 'test1', $v[0]); $shouldFail->addToMap($type, 'test2', $v[1]); } else { $shouldFail->addToMap($type, 'test1', $v); } switch ($type) { case 'Binary': if (in_array($k, $allStrings)) { continue 2; } break; case 'Blob': if (in_array($k, $allStrings)) { continue 2; } break; case 'Decimal': if (in_array($k, ['Float'])) { continue 2; } break; case 'Date': if (in_array($k, ['DateTime'])) { continue 2; } break; case 'DateTime': if (in_array($k, ['Date'])) { continue 2; } break; case 'Float': if (in_array($k, ['Decimal'])) { continue 2; } break; case 'Identifier': if (in_array($k, ['TimeUuid', 'Uuid'])) { continue 2; } break; case 'MediumBlob': if (in_array($k, $allStrings)) { continue 2; } break; case 'MediumText': if (in_array($k, $allStrings)) { continue 2; } break; case 'String': if (in_array($k, $allStrings)) { continue 2; } break; case 'Text': if (in_array($k, $allStrings)) { continue 2; } break; case 'Timestamp': if (in_array($k, $allInts)) { continue 2; } break; case 'Uuid': if (in_array($k, ['Identifier', 'TimeUuid'])) { continue 2; } break; default: } if (false !== strpos($type, 'Int') && in_array($k, $allInts)) { continue; } } catch (\Exception $e) { $thrown = true; } if (!$thrown) { if (is_array($v)) { $this->fail(sprintf('[%s] accepted an invalid/mismatched [%s] value', $type, StringUtils::varToString($v[0]))); $this->fail(sprintf('[%s] accepted an invalid/mismatched [%s] value', $type, StringUtils::varToString($v[1]))); } else { $this->fail(sprintf('[%s] accepted an invalid/mismatched [%s] value', $type, StringUtils::varToString($v))); } } } } //echo json_encode($shouldWork, JSON_PRETTY_PRINT); }