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]); }
/** * Convert the JSON response into a RPSL object. * * @param array $item The "object" array from the the response. * @return ObjectInterface */ protected function createObject($item) { $type = $item['type']; $class = __NAMESPACE__ . '\\RPSL\\' . str_replace(' ', '', ucwords(str_replace('-', ' ', $type))); if (isset($item['primary-key'])) { $pk = $item['primary-key']['attribute'][0]; } else { $pk = ['name' => null, 'value' => '']; } if (class_exists($class)) { $object = new $class($pk['value']); } else { $object = new Dummy($type, $pk['name']); } foreach ($item['attributes']['attribute'] as $value) { try { if (count($value) < 3) { $attr_val = $value['value']; } elseif ($value['name'] === 'source') { $attr_val = $value['value']; } else { $attr_val = new AttributeValue($value['value']); if (isset($value['comment'])) { $attr_val->setComment($value['comment']); } if (isset($value['referenced-type'])) { $attr_val->setType($value['referenced-type']); } if (isset($value['link']) and $value['link']['type'] === 'locator') { $attr_val->setLink($value['link']['href']); } } $object->getAttribute($value['name'])->addValue($attr_val); } catch (\Exception $e) { // skip over attributes that are present in the response but do // not conform to the current definitions continue; } } return $object; }
public function testObjectToString() { $bar = new AttributeValue('bar'); $bar->setComment('testing a value object'); $obj = new TestObject(); $obj->addAttribute('bar', $bar)->addAttribute('abc', 'x')->addAttribute('abc', 'y')->addAttribute('abc', 'z')->addAttribute('num', 1)->addAttribute('source', 'test'); $string = trim((string) $obj); $lines = explode(\PHP_EOL, $string); $this->assertCount(7, $lines); $title = array_shift($lines); $this->assertNotFalse(strpos($title, 'TestObject')); $getData = function ($str) { preg_match('/^\\s+(\\S+)\\s+(.+)$/', $str, $match); array_shift($match); return $match; }; $this->assertEquals(['bar', 'bar # testing a value object'], call_user_func($getData, $lines[0])); $this->assertEquals(['abc', 'x'], call_user_func($getData, $lines[1])); $this->assertEquals(['abc', 'y'], call_user_func($getData, $lines[2])); $this->assertEquals(['abc', 'z'], call_user_func($getData, $lines[3])); $this->assertEquals(['num', '1'], call_user_func($getData, $lines[4])); $this->assertEquals(['source', 'test'], call_user_func($getData, $lines[5])); }