/** * Important call directly after the creation of this class. * * @param string $objectKey * @param Object $definition */ public function configure($objectKey, $definition) { $this->objectKey = \Jarves\Objects::normalizeObjectKey($objectKey); $this->definition = $definition; foreach ($this->definition->getFields() as $field) { if ($field->isPrimaryKey()) { $this->primaryKeys[] = $field->getId(); } } }
/** * @param \Jarves\Configuration\Object $object * @param \SimpleXMLElement $objectTable * * @return \SimpleXMLElement * * @throws \Jarves\Exceptions\ModelBuildException */ protected function getXmlTable(Object $object, \SimpleXMLElement $objectTable = null) { if (!$objectTable) { $objectTable = new \SimpleXMLElement('<table />'); //simplexml_load_string('<database></database>'); } if (!$object->getTable()) { throw new ModelBuildException(sprintf('The object `%s` has no table defined', $object->getId())); } $objectTable['name'] = $object->getTable(); $objectTable['phpName'] = ucfirst($object->getId()); if ($object->isCrossRef()) { $objectTable['isCrossRef'] = 'true'; } $columnsDefined = array(); if (!$object->getFields()) { throw new ModelBuildException(sprintf('The object `%s` has no fields defined', $object->getId())); } foreach ($object->getFields() as $field) { if ($columns = $field->getFieldType()->getColumns()) { foreach ($columns as $column) { $name = Tools::camelcase2Underscore($column->getName()); //column exist? $eColumns = $objectTable->xpath('column[@name =\'' . $name . '\']'); if ($eColumns) { $newCol = current($eColumns); if ($newCol['custom'] == true) { continue; } } else { $newCol = $objectTable->addChild('column'); } $columnsDefined[] = $name; $this->setupColumnAttributes($column, $newCol); if ($field->isRequired()) { $newCol['required'] = 'true'; } if ($field->isPrimaryKey()) { $newCol['primaryKey'] = 'true'; } if ($field->isAutoIncrement()) { $newCol['autoIncrement'] = 'true'; } } } } if ($relations = $object->getRelations()) { foreach ($relations as $relation) { $this->addRelation($object, $relation, $objectTable); } } if ($object->isNested()) { $behaviors = $objectTable->xpath('behavior[@name=\'nested_set\']'); if ($behaviors) { $behavior = current($behaviors); } else { $behavior = $objectTable->addChild('behavior'); } if (!$behavior['custom']) { $behavior['name'] = 'nested_set'; $parameters = ['left_column' => 'lft', 'right_column' => 'rgt', 'level_column' => 'lvl']; if ($object->getNestedRootAsObject()) { $parameters['use_scope'] = 'true'; $parameters['scope_column'] = Tools::camelcase2Underscore($object->getNestedRootObjectField()); } foreach ($parameters as $k => $v) { $parameter = $behavior->addChild('parameter'); $parameter['name'] = $k; $parameter['value'] = $v; } } } if ($object['workspace']) { $behaviors = $objectTable->xpath('behavior[@name=\'Jarves\\Propel\\Behavior\\WorkspaceBehavior\']'); if ($behaviors) { $behavior = current($behaviors); } else { $behavior = $objectTable->addChild('behavior'); } $behavior['name'] = 'Jarves\\Propel\\Behavior\\WorkspaceBehavior'; } $vendors = $objectTable->xpath('vendor[@type=\'mysql\']'); if ($vendors) { foreach ($vendors as $k => $v) { unset($vendors[$k][0]); } } $vendor = $objectTable->addChild('vendor'); $vendor['type'] = 'mysql'; $params = $vendor->xpath('parameter[@name=\'Charset\']'); if ($params) { $param = current($params); } else { $param = $vendor->addChild('parameter'); } $param['name'] = 'Charset'; $param['value'] = 'utf8'; return $objectTable; // $dom = new \DOMDocument; // $dom->preserveWhiteSpace = false; // $dom->loadXML($xml->asXML()); // $dom->formatOutput = true; // // $xml = $dom->saveXML(); // $prefix = '<?xml version="1.0"? >'; // if (0 === strpos($xml, $prefix)) { // $xml = substr($xml, strlen($prefix)); // } // // return trim($xml); }
/** * @param bool $withoutObjectCheck * * @throws ObjectNotFoundException */ public function initialize($withoutObjectCheck = false) { if ($this->objectDefinition) { return; } if (!$this->getObject()) { return; } $this->objectDefinition = $this->objects->getDefinition($this->getObject()); if (!$this->objectDefinition && $this->getObject() && !$withoutObjectCheck) { throw new ObjectNotFoundException("Can not find object '" . $this->getObject() . "'"); } if ($this->objectDefinition) { if ($apiControllerDefinition = $this->objectDefinition->getApiControllerDefinition()) { $path = $this->jarves->resolvePath($apiControllerDefinition); $definitionContent = file_get_contents($path); $yaml = new Parser(); $parsedDefinition = $yaml->parse($definitionContent); if ($parsedDefinition) { foreach ($parsedDefinition as $key => $val) { $setter = 'set' . ucfirst($key); if (method_exists($this, $setter)) { $this->{$setter}($val); } } } } $this->asNested = $this->objectDefinition->getNested(); if (!$this->table) { $this->table = $this->objectDefinition->getTable(); } if (!$this->fields) { $this->fields = []; foreach ($this->objectDefinition->getFields() as $field) { if (!$field->isAutoIncrement()) { $this->fields[] = $field; } } } if (!$this->columns) { foreach ($this->fields as $field) { if ($field->isPrimaryKey() || $field->isAutoIncrement()) { continue; } if ('object' !== $field->getType()) { $this->columns[$field->getId()] = $field; } } if ($labelField = $this->objectDefinition->getLabelField()) { $field = $this->objectDefinition->getField($labelField); $this->columns[$field->getId()] = $field; } } //we need to call it, no matter if it's already defined, because of multiLanguage field. $this->prepareFieldItem($this->fields); $this->translateFields($this->fields); $this->translateFields($this->columns); if (!isset($this->titleField)) { $this->titleField = $this->objectDefinition->getLabel(); } } else { //resolve shortcuts if ($this->fields) { $this->prepareFieldDefinition($this->fields); $this->convertToFieldObjects($this->fields); $this->prepareFieldItem($this->fields); $this->translateFields($this->fields); } if ($this->columns) { $this->prepareFieldDefinition($this->columns); $this->convertToFieldObjects($this->columns); $this->translateFields($this->columns); } } $this->ensureLanguageField(); $this->fields = $this->toIdIndex($this->fields); $this->columns = $this->toIdIndex($this->columns); if ($this->addMultipleFields) { $this->prepareFieldDefinition($this->addMultipleFields); $this->convertToFieldObjects($this->addMultipleFields); $this->translateFields($this->addMultipleFields); } if ($this->addMultipleFixedFields) { $this->prepareFieldDefinition($this->addMultipleFixedFields); $this->convertToFieldObjects($this->addMultipleFixedFields); $this->translateFields($this->addMultipleFixedFields); } if (is_string($this->primary)) { $this->primary = explode(',', str_replace(' ', '', $this->primary)); } if (!$this->order || count($this->order) == 0) { /* compatibility */ $this->orderByDirection = strtolower($this->orderByDirection) == 'asc' ? 'asc' : 'desc'; if ($this->orderBy) { $this->order = array($this->orderBy => $this->orderByDirection); } } if ((!$this->order || count($this->order) == 0) && $this->columns) { reset($this->columns); $field = current($this->columns); if ($field instanceof Field) { $this->order[$field->getId()] = 'asc'; } } //normalize order array if (count($this->order) > 0 && is_numeric(key($this->order))) { $newOrder = array(); foreach ($this->order as $order) { $newOrder[$order['field']] = $order['direction']; } $this->order = $newOrder; } $this->filterFields = array(); if ($this->filter) { foreach ($this->filter as $key => $val) { if (is_numeric($key)) { //no special definition $fieldKey = $val; $field = $this->fields[$val]; } else { $field = $val; $fieldKey = $key; } $this->prepareFieldItem($field); $this->filterFields[$fieldKey] = $field; } } if (!$this->primary) { $this->primary = array(); if ($this->objectDefinition) { foreach ($this->objectDefinition->getPrimaryKeys() as $sfield) { $this->primary[] = $sfield->getId(); } } } $this->translate($this->nestedRootAddLabel); $this->translate($this->newLabel); }
public function testObjectItemArray() { $xml = ' <object id="Item"> <label>title</label> <table>test_item</table> <labelField>title</labelField> <nested>false</nested> <multiLanguage>false</multiLanguage> <workspace>true</workspace> <domainDepended>false</domainDepended> <treeFixedIcon>false</treeFixedIcon> <fields> <field id="id" type="number" primaryKey="true" autoIncrement="true"> </field> <field id="title" type="text"> </field> <field id="category" type="object"> <object>test/itemCategory</object> <objectRelation>nToM</objectRelation> </field> <field id="oneCategory" type="object"> <object>test/itemCategory</object> <objectRelation>nTo1</objectRelation> </field> </fields> </object>'; $object = new Object($xml, $this->getJarves()); $array = $object->toArray(); $this->assertEquals('Item', $object->getId()); $this->assertEquals('title', $object->getLabel()); $this->assertEquals('test_item', $object->getTable()); $this->assertTrue($object->getWorkspace()); $this->assertCount(4, $object->getFields()); $this->assertEquals('Item', $array['id']); $this->assertEquals('title', $array['label']); $this->assertEquals('test_item', $array['table']); $this->assertTrue($array['workspace']); $this->assertCount(4, $array['fields']); }
/** * Maybe in v1.1 * * @param $pattern * @param Object $object */ public function setupRelationRoutes($pattern, Object $object) { $objectName = $object->getBundle()->getBundleName() . '/' . lcfirst($object->getId()); $pattern = $pattern . '/{pk}/'; foreach ($object->getFields() as $field) { if ('object' === $field->getType()) { $foreignObject = $this->jarves->getObjects()->getDefinition($field->getObject()); if (!$foreignObject) { continue; } $this->setupRoutes($object->getBundle(), $object->getFinalApiController(), $pattern . lcfirst($field->getObjectRelationName() ?: $field->getId()), $objectName, $foreignObject, $object, $field); } } }