public function testBuildError() { $expectedErrors = ['lastname' => 'missing_key']; $mockSchema = $this->getMockBuilder(SchemaInterface::class)->getMock(); $mockSchema->expects($this->once())->method('validate')->will($this->returnValue(Error::unit($expectedErrors))); $defaults = ['username' => 'superuser', 'email' => '*****@*****.**']; $builder = new Builder($mockSchema); $result = $builder->firstname('clark')->build($defaults); $this->assertInstanceOf(Error::class, $result); $this->assertEquals($expectedErrors, $result->unwrap()); }
/** * Tells if a given array's items adhere to any of the property's allowed types. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { if (!is_array($value)) { return Error::unit([Error::NON_ARRAY]); } $errors = []; foreach ($value as $pos => $item) { $result = parent::validate($item); if ($result instanceof Error) { $errors[$pos] = $result->unwrap(); } } return empty($errors) ? Ok::unit() : Error::unit($errors); }
/** * Tells if a given value is a valid int. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return is_int($value) ? Ok::unit() : Error::unit([Error::NON_INT]); }
/** * If the schema has a property named ':any_name:', this method will validate all keys, * that have not been explicitly addressed by the schema. * * @param mixed[] $data * * @return ResultInterface */ protected function validateAnyValues(array $data) { $errors = []; foreach (array_diff_key($data, $this->properties) as $key => $value) { if (isset($this->properties[':any_name:'])) { if ($value === null) { if ($this->properties[':any_name:']->isRequired()) { $errors[$key] = [Error::MISSING_VALUE]; } continue; } $result = $this->properties[':any_name:']->validate($value); if ($result instanceof Error) { $errors[$key] = $result->unwrap(); } } else { $errors[$key] = [Error::UNEXPECTED_KEY]; } } return empty($errors) ? Ok::unit() : Error::unit($errors); }
/** * Tells if a given value is a valid float. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return is_float($value) ? Ok::unit() : Error::unit([Error::NON_FLOAT]); }
/** * Tells if a given value is a valid choice according to the property's definition. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return in_array($value, $this->choices) ? Ok::unit() : Error::unit([Error::INVALID_CHOICE]); }
/** * Tells if a given value adhere's to the property's value schema. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return is_array($value) ? $this->valueSchema->validate($value) : Error::unit([Error::NON_ARRAY]); }
/** * Tells if a given value is a fully qualified class name of an existing php class. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return class_exists($value) || interface_exists($value) ? Ok::unit() : Error::unit([Error::CLASS_NOT_EXISTS]); }
/** * Tells if a given value is a valid scalar. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return is_scalar($value) ? Ok::unit() : Error::unit([Error::NON_SCALAR]); }
/** * Tells if a given value is a valid string. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return is_string($value) ? Ok::unit() : Error::unit([Error::NON_STRING]); }
/** * Tells if a given value is a valid bool. * * @param mixed $value * * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned. */ public function validate($value) { return is_bool($value) ? Ok::unit() : Error::unit([Error::NON_BOOL]); }