public function testTranslation() { \Locale::setDefault('pt_BR'); $value = I18n::t('hello_user', ['user' => 'Roberto']); // en_US $this->assertEquals($value, "Olá Roberto"); }
/** * {@inheritDoc} */ public function check($element, $schema = null, $path = null, $i = null) { // Verify minimum if (isset($schema->exclusiveMinimum)) { if (isset($schema->minimum)) { $errorMsg = I18n::t("constraints.number.minimum", ['value' => $schema->minimum]); if ($schema->exclusiveMinimum && $element <= $schema->minimum) { $this->addError($path, $errorMsg, 'exclusiveMinimum', array('minimum' => $schema->minimum)); } else { if ($element < $schema->minimum) { $this->addError($path, $errorMsg, 'minimum', array('minimum' => $schema->minimum)); } } } else { $errorMsg = I18n::t("constraints.number.exclusive_minimum"); $this->addError($path, $errorMsg, 'missingMinimum'); } } else { if (isset($schema->minimum) && $element < $schema->minimum) { $errorMsg = I18n::t("constraints.number.minimum", ['value' => $schema->minimum]); $this->addError($path, $errorMsg, 'minimum', array('minimum' => $schema->minimum)); } } // Verify maximum if (isset($schema->exclusiveMaximum)) { if (isset($schema->maximum)) { $errorMsg = I18n::t("constraints.number.maximum", ['value' => $schema->maximum]); if ($schema->exclusiveMaximum && $element >= $schema->maximum) { $this->addError($path, $errorMsg, 'exclusiveMaximum', array('maximum' => $schema->maximum)); } else { if ($element > $schema->maximum) { $this->addError($path, $errorMsg, 'maximum', array('maximum' => $schema->maximum)); } } } else { $this->addError($path, "Use of exclusiveMaximum requires presence of maximum", 'missingMaximum'); } } else { if (isset($schema->maximum) && $element > $schema->maximum) { $errorMsg = I18n::t("constraints.number.maximum", ['value' => $schema->maximum]); $this->addError($path, $errorMsg, 'maximum', array('maximum' => $schema->maximum)); } } // Verify divisibleBy - Draft v3 if (isset($schema->divisibleBy) && $this->fmod($element, $schema->divisibleBy) != 0) { $errorMsg = I18n::t("constraints.number.divisible_by", ['value' => $schema->divisibleBy]); $this->addError($path, $errorMsg, 'divisibleBy', array('divisibleBy' => $schema->divisibleBy)); } // Verify multipleOf - Draft v4 if (isset($schema->multipleOf) && $this->fmod($element, $schema->multipleOf) != 0) { $errorMsg = I18n::t("constraints.number.multiple_of", ['value' => $schema->multipleOf]); $this->addError($path, $errorMsg, 'multipleOf', array('multipleOf' => $schema->multipleOf)); } $this->checkFormat($element, $schema, $path, $i); }
/** * {@inheritDoc} */ public function check($element, $schema = null, $path = null, $i = null) { // Verify maxLength if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) { $errorMsg = I18n::t("constraints.string.max_length", ['maxLength' => $schema->maxLength]); $this->addError($path, $errorMsg, 'maxLength', array('maxLength' => $schema->maxLength)); } //verify minLength if (isset($schema->minLength) && $this->strlen($element) < $schema->minLength) { $errorMsg = I18n::t("constraints.string.min_length", ['minLength' => $schema->minLength]); $this->addError($path, $errorMsg, 'minLength', array('minLength' => $schema->minLength)); } // Verify a regex pattern if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#', $element)) { $errorMsg = I18n::t("constraints.string.pattern", ['pattern' => $schema->pattern]); $this->addError($path, $errorMsg, 'pattern', array('pattern' => $schema->pattern)); } $this->checkFormat($element, $schema, $path, $i); }
/** * {@inheritDoc} */ public function check($element, $schema = null, $path = null, $i = null) { // Only validate enum if the attribute exists if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) { return; } foreach ($schema->enum as $enum) { $type = gettype($element); if ($type === gettype($enum)) { if ($type == "object") { if ($element == $enum) { return; } } else { if ($element === $enum) { return; } } } } $errorMsg = I18n::t("constraints.enum.element_does_not_belong", ['enum' => json_encode($schema->enum)]); $this->addError($path, $errorMsg, 'enum', array('enum' => $schema->enum)); }
/** * {@inheritDoc} */ public function check($value = null, $schema = null, $path = null, $i = null) { $type = isset($schema->type) ? $schema->type : null; $isValid = true; if (is_array($type)) { // @TODO refactor $validatedOneType = false; $errors = array(); foreach ($type as $tp) { $validator = new static($this->checkMode); $subSchema = new \stdClass(); $subSchema->type = $tp; $validator->check($value, $subSchema, $path, null); $error = $validator->getErrors(); if (!count($error)) { $validatedOneType = true; break; } $errors = $error; } if (!$validatedOneType) { $this->addErrors($errors); return; } } elseif (is_object($type)) { $this->checkUndefined($value, $type, $path); } else { $isValid = $this->validateType($value, $type); } if ($isValid === false) { if (!isset(self::$wording[$type])) { throw new StandardUnexpectedValueException(sprintf("No wording for %s available, expected wordings are: [%s]", var_export($type, true), implode(', ', array_filter(self::$wording)))); } $errorMsg = I18n::t("constraints.type.required", ['actual' => ucwords(gettype($value)), 'required' => self::$wording[$type]]); $this->addError($path, $errorMsg, 'type'); } }
/** * {@inheritDoc} */ public function check($element, $schema = null, $path = null, $i = null) { if (!isset($schema->format)) { return; } switch ($schema->format) { case 'date': if (!($date = $this->validateDateTime($element, 'Y-m-d'))) { $errorMsg = I18n::t("constraints.format.date", ['date' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'time': if (!$this->validateDateTime($element, 'H:i:s')) { $errorMsg = I18n::t("constraints.format.time", ['time' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'date-time': if (null === Rfc3339::createFromString($element)) { $errorMsg = I18n::t("constraints.format.datetime", ['datetime' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'utc-millisec': if (!$this->validateDateTime($element, 'U')) { $errorMsg = I18n::t("constraints.format.utcmilli", ['utcmilli' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'regex': if (!$this->validateRegex($element)) { $errorMsg = I18n::t("constraints.format.regex", ['regex' => json_encode($element)]); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'color': if (!$this->validateColor($element)) { $errorMsg = I18n::t("constraints.format.color"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'style': if (!$this->validateStyle($element)) { $errorMsg = I18n::t("constraints.format.style"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'phone': if (!$this->validatePhone($element)) { $errorMsg = I18n::t("constraints.format.phone"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'uri': if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { $errorMsg = I18n::t("constraints.format.url"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'email': if (null === filter_var($element, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE)) { $errorMsg = I18n::t("constraints.format.email"); $this->addError($path, "Invalid email", 'format', array('format' => $schema->format)); } break; case 'ip-address': case 'ipv4': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4)) { $errorMsg = I18n::t("constraints.format.ipv4"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'ipv6': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6)) { $errorMsg = I18n::t("constraints.format.ipv6"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; case 'host-name': case 'hostname': if (!$this->validateHostname($element)) { $errorMsg = I18n::t("constraints.format.hostname"); $this->addError($path, $errorMsg, 'format', array('format' => $schema->format)); } break; default: // Empty as it should be: // The value of this keyword is called a format attribute. It MUST be a string. // A format attribute can generally only validate a given set of instance types. // If the type of the instance to validate is not in this set, validation for // this format attribute and instance SHOULD succeed. // http://json-schema.org/latest/json-schema-validation.html#anchor105 break; } }
/** * Validate dependencies * * @param mixed $value * @param mixed $dependencies * @param string $path * @param string $i */ protected function validateDependencies($value, $dependencies, $path, $i = "") { foreach ($dependencies as $key => $dependency) { if (property_exists($value, $key)) { if (is_string($dependency)) { // Draft 3 string is allowed - e.g. "dependencies": {"bar": "foo"} if (!property_exists($value, $dependency)) { $errorMsg = I18n::t('constraints.undefined.depends_on', ['key' => $key, 'dependency' => $dependency]); $this->addError($path, $errorMsg, 'dependencies'); } } else { if (is_array($dependency)) { // Draft 4 must be an array - e.g. "dependencies": {"bar": ["foo"]} foreach ($dependency as $d) { if (!property_exists($value, $d)) { $errorMsg = I18n::t('constraints.undefined.depends_on', ['key' => $key, 'dependency' => $d]); $this->addError($path, $errorMsg, 'dependencies'); } } } else { if (is_object($dependency)) { // Schema - e.g. "dependencies": {"bar": {"properties": {"foo": {...}}}} $this->checkUndefined($value, $dependency, $path, $i); } } } } } }
/** * Validates the items * * @param array $value * @param \stdClass $schema * @param string $path * @param string $i */ protected function validateItems($value, $schema = null, $path = null, $i = null) { if (is_object($schema->items)) { // just one type definition for the whole array foreach ($value as $k => $v) { $initErrors = $this->getErrors(); // First check if its defined in "items" $this->checkUndefined($v, $schema->items, $path, $k); // Recheck with "additionalItems" if the first test fails if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) { $secondErrors = $this->getErrors(); $this->checkUndefined($v, $schema->additionalItems, $path, $k); } // Reset errors if needed if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) { $this->errors = $secondErrors; } else { if (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) { $this->errors = $initErrors; } } } } else { // Defined item type definitions foreach ($value as $k => $v) { if (array_key_exists($k, $schema->items)) { $this->checkUndefined($v, $schema->items[$k], $path, $k); } else { // Additional items if (property_exists($schema, 'additionalItems')) { if ($schema->additionalItems !== false) { $this->checkUndefined($v, $schema->additionalItems, $path, $k); } else { $errorMsg = I18n::t("constraints.collection.additional_items", ['item' => $i, 'index' => $k]); $this->addError($path, $errorMsg, 'additionalItems', array('additionalItems' => $schema->additionalItems)); } } else { // Should be valid against an empty schema $this->checkUndefined($v, new \stdClass(), $path, $k); } } } // Treat when we have more schema definitions than values, not for empty arrays if (count($value) > 0) { for ($k = count($value); $k < count($schema->items); $k++) { $this->checkUndefined(new UndefinedConstraint(), $schema->items[$k], $path, $k); } } } }
/** * validating minimum and maximum property constraints (if present) against an element * * @param \stdClass $element Element to validate * @param \stdClass $objectDefinition ObjectConstraint definition * @param string $path Path to test? */ protected function validateMinMaxConstraint($element, $objectDefinition, $path) { // Verify minimum number of properties if (isset($objectDefinition->minProperties) && !is_object($objectDefinition->minProperties)) { if (count(get_object_vars($element)) < $objectDefinition->minProperties) { $errorMsg = I18n::t("constraints.object.min_properties", ['count' => $objectDefinition->minProperties]); $this->addError($path, "Must contain a minimum of " . $objectDefinition->minProperties . " properties", 'minProperties', array('minProperties' => $objectDefinition->minProperties)); } } // Verify maximum number of properties if (isset($objectDefinition->maxProperties) && !is_object($objectDefinition->maxProperties)) { if (count(get_object_vars($element)) > $objectDefinition->maxProperties) { $errorMsg = I18n::t("constraints.object.max_properties", ['count' => $objectDefinition->maxProperties]); $this->addError($path, $errorMsg, 'maxProperties', array('maxProperties' => $objectDefinition->maxProperties)); } } }