/** * @param CallbackBag $callbackBag * @return mixed * @throws \Exception */ public function callback(CallbackBag $callbackBag) { if (!$callbackBag->argumentExists(0)) { throw new \InvalidArgumentException($this->getPrefix() . ' accessor needs at least a value!'); } Hint::validateOrException($callbackBag->getName(), $callbackBag->getArgument(0), $callbackBag->getHint(), $callbackBag->getNullable()); $this->propertyDefault($callbackBag); $this->updateProperty($callbackBag); return $callbackBag->getObject(); }
public function testValidate() { // bool $this->assertTrue(Hint::validate(false)); $this->assertTrue(Hint::validate(false, Hint::BOOL)); $this->assertTrue(Hint::validate(null, Hint::BOOL)); $this->assertFalse(Hint::validate(1, Hint::BOOL)); $this->assertFalse(Hint::validate(null, Hint::BOOL, false)); // int $this->assertTrue(Hint::validate(1)); $this->assertTrue(Hint::validate(1, Hint::INT)); $this->assertTrue(Hint::validate(null, Hint::INT)); $this->assertFalse(Hint::validate(1.0, Hint::INT)); $this->assertFalse(Hint::validate(null, Hint::INT, false)); // float $this->assertTrue(Hint::validate(1.0)); $this->assertTrue(Hint::validate(1.0, Hint::FLOAT)); $this->assertTrue(Hint::validate(null, Hint::FLOAT)); $this->assertFalse(Hint::validate(1, Hint::FLOAT)); $this->assertFalse(Hint::validate(null, Hint::FLOAT, false)); // numeric $this->assertTrue(Hint::validate(1.0)); $this->assertTrue(Hint::validate(1.0, Hint::NUMERIC)); $this->assertTrue(Hint::validate(null, Hint::NUMERIC)); $this->assertFalse(Hint::validate('test', Hint::NUMERIC)); $this->assertFalse(Hint::validate(null, Hint::NUMERIC, false)); // string $this->assertTrue(Hint::validate('test')); $this->assertTrue(Hint::validate('test', Hint::STRING)); $this->assertTrue(Hint::validate(null, Hint::STRING)); $this->assertFalse(Hint::validate(1, Hint::STRING)); $this->assertFalse(Hint::validate(null, Hint::STRING, false)); // array $this->assertTrue(Hint::validate(array())); $this->assertTrue(Hint::validate(array(), Hint::ARR)); $this->assertTrue(Hint::validate(null, Hint::ARR, true)); $this->assertFalse(Hint::validate('test', Hint::ARR)); $this->assertFalse(Hint::validate(null, Hint::ARR)); // object $this->assertTrue(Hint::validate(new \stdClass())); $this->assertTrue(Hint::validate(new \stdClass(), '\\stdClass')); $this->assertTrue(Hint::validate(null, '\\stdClass', true)); $this->assertFalse(Hint::validate('test', '\\stdClass')); $this->assertFalse(Hint::validate(null, '\\stdClass')); // many objects $this->assertTrue(Hint::validate(array(new \stdClass()), '\\stdClass[]')); $this->assertTrue(Hint::validate(array(new \stdClass(), new \stdClass()), '\\stdClass[]')); $this->assertTrue(Hint::validate(array(new \stdClass(), null), '\\stdClass[]', true)); $this->assertFalse(Hint::validate(array(new \stdClass(), null, 'test'), '\\stdClass[]', true)); }