/** * Add a regular expression check before saving the value. * * @param mixed $value Attribute value. * @return string Validated string value. * @throws InvalidValueException Value does not match pattern. */ protected function convert($value) { $value = (string) parent::convert($value); if (!preg_match($this->regexp, $value)) { $msg = sprintf('Invalid value for the [%s] attribute.', $this->name); throw new InvalidValueException($msg); } return $value; }
/** * Add a value comparison check before saving the value. * * @param mixed $value Attribute value. * @return string Validated string value. * @throws InvalidValueException Value not allowed. */ protected function convert($value) { $value = (string) parent::convert($value); if (!in_array($value, $this->values, true)) { $msg = sprintf('Value "%s" is not allowed for the [%s] attribute.', $value, $this->name); throw new InvalidValueException($msg); } return $value; }
public function testMultipleAttributeWithValueObject() { $attr = new Attribute('test', Attr::REQUIRED, Attr::MULTIPLE); $value = new AttributeValue('something'); $value->setType('example')->setLink('http://www.example.com/something'); $attr->addValue($value); $data = $attr->getValue(); $this->assertInternalType('array', $data); $this->assertCount(1, $data); $this->assertInternalType('object', $data[0]); }
public function testMultipleAttributeIgnoresArrayKeys() { $attr = new Attribute('foo', Attr::REQUIRED, Attr::MULTIPLE); $attr->setValue(['fizz' => 'buzz']); $this->assertSame(['buzz'], $attr->getValue()); }