/** * Method wich can map all fields of an record to an arbitary class by * calling the fitting setter methods if available * * @param \PSX\Data\RecordInterface $source * @param object $destination * @param array $rules */ public static function map(RecordInterface $source, $destination, array $rules) { if (!is_object($destination)) { throw new InvalidArgumentException('Destination must be an object'); } $data = $source->getRecordInfo()->getData(); foreach ($data as $key => $value) { // convert to camelcase if underscore is in name if (strpos($key, '_') !== false) { $key = implode('', array_map('ucfirst', explode('_', $key))); } $method = null; if (isset($rules[$key])) { if (is_string($rules[$key])) { $method = 'set' . ucfirst($rules[$key]); } elseif ($rules[$key] instanceof Rule) { $method = 'set' . ucfirst($rules[$key]->getName()); $value = $rules[$key]->getValue($value, $data); } } else { $method = 'set' . ucfirst($key); } if (is_callable(array($destination, $method))) { $destination->{$method}($value); } } }
/** * Generates an entity definition from the given $record class * * @param PSX\Data\RecordInterface $record */ public function addEntityType(RecordInterface $record) { $this->writer->startElement('EntityType'); $this->writer->writeAttribute('Name', $record->getRecordInfo()->getName()); $this->buildEntity(new ReflectionClass($record), true); $this->buildComplexTypes(); $this->writer->endElement(); }
/** * Merges data from two record into a new record. The right record * overwrites values from the left record * * @param \PSX\Data\RecordInterface $left * @param \PSX\Data\RecordInterface $right * @return \PSX\Data\RecordInterface */ public static function merge(RecordInterface $left, RecordInterface $right) { return new Object(array_merge($left->getRecordInfo()->getData(), $right->getRecordInfo()->getData())); }
protected function doDelete(RecordInterface $record, Version $version) { $table = $this->getTable(); $data = $table->get($record->getRecordInfo()->getField($table->getPrimaryKey())); if (empty($data)) { throw new StatusCode\NotFoundException('Record not found'); } $table->delete($record); return array('success' => true, 'message' => sprintf('%s successful deleted', ucfirst($table->getDisplayName()))); }
protected function assertRecord(RecordInterface $record) { // check available fields $this->assertTrue($record->getRecordInfo()->hasField('id')); $this->assertTrue($record->getRecordInfo()->hasField('title')); $this->assertTrue($record->getRecordInfo()->hasField('active')); $this->assertTrue($record->getRecordInfo()->hasField('disabled')); $this->assertTrue($record->getRecordInfo()->hasField('count')); $this->assertTrue($record->getRecordInfo()->hasField('rating')); $this->assertFalse($record->getRecordInfo()->hasField('foobar')); $this->assertFalse($record->getRecordInfo()->hasField('foo')); if ($this->canDetermineType()) { $this->assertEquals(1, $record->getId()); $this->assertEquals('foobar', $record->getTitle()); $this->assertTrue($record->getActive()); $this->assertFalse($record->getDisabled()); $this->assertEquals(12, $record->getCount()); $this->assertEquals(12.45, $record->getRating()); $this->assertInternalType('integer', $record->getId()); $this->assertInternalType('string', $record->getTitle()); $this->assertInternalType('boolean', $record->getActive()); $this->assertInternalType('boolean', $record->getDisabled()); $this->assertInternalType('integer', $record->getCount()); $this->assertInternalType('float', $record->getRating()); $this->assertInstanceOf('DateTime', $record->getDate()); } else { $this->assertEquals('1', $record->getId()); $this->assertEquals('foobar', $record->getTitle()); // the json reader returns the real php type the xml reader returns // the string true or false $this->assertTrue($record->getActive() === true || $record->getActive() === 'true'); $this->assertTrue($record->getDisabled() === false || $record->getDisabled() === 'false'); $this->assertEquals('12', $record->getCount()); $this->assertEquals('12.45', $record->getRating()); $this->assertEquals('2014-01-01T12:34:47+01:00', $record->getDate()); } if ($this->canImportComplexRecord()) { $this->assertInstanceOf('PSX\\Data\\Record\\Importer\\Test\\Person', $record->getPerson()); $this->assertEquals('Foo', $record->getPerson()->getTitle()); $this->assertEquals(true, is_array($record->getTags())); $this->assertEquals(3, count($record->getTags())); $this->assertEquals('bar', $record->getTags()[0]); $this->assertEquals('foo', $record->getTags()[1]); $this->assertEquals('test', $record->getTags()[2]); $this->assertEquals(true, is_array($record->getEntry())); $this->assertEquals(3, count($record->getEntry())); $this->assertEquals('bar', $record->getEntry()[0]->getTitle()); $this->assertEquals('foo', $record->getEntry()[1]->getTitle()); $this->assertEquals('test', $record->getEntry()[2]->getTitle()); foreach ($record->getEntry() as $entry) { $this->assertInstanceOf('PSX\\Data\\Record\\Importer\\Test\\Entry', $entry); } $this->assertInstanceOf('stdClass', $record->getToken()); $this->assertEquals('bar', $record->getToken()->value); $this->assertInstanceOf('PSX\\Url', $record->getUrl()); $this->assertEquals('http://google.com', $record->getUrl()->__toString()); } }