Ejemplo n.º 1
0
 /**
  * 
  * @param MString $dictString
  * 
  * @return MDictionary
  */
 public static function parseString(MString $dictString)
 {
     $arr = $dictString->componentsSeparatedByString(S("|"));
     $dict = new MMutableDictionary();
     foreach ($arr->toArray() as $dictKeyPair) {
         $dictKeyPairArr = $dictKeyPair->componentsSeparatedByString(S(":"));
         $key = $dictKeyPairArr->objectAtIndex(0)->urlDecodedString();
         $value = $dictKeyPairArr->objectAtIndex(1)->urlDecodedString();
         $dict->setObjectForKey($key, $value);
     }
     return $dict;
 }
Ejemplo n.º 2
0
 /**
  * @internal
  *
  * @return bool
  */
 protected function _setValueForField(MApplicationControllerField $field, $value)
 {
     $object = null;
     try {
         if ($field->type() == MApplicationControllerField::StringType) {
             $object = S($value);
         } else {
             if ($field->type() == MApplicationControllerField::IntegerType) {
                 $object = MNumber::parseInt($value);
             } else {
                 if ($field->type() == MApplicationControllerField::FloatType) {
                     $object = MNumber::parseFloat($value);
                 } else {
                     if ($field->type() == MApplicationControllerField::BooleanType) {
                         $object = MNumber::parseBool($value);
                     } else {
                         if ($field->type() == MApplicationControllerField::DateType) {
                             $object = MDate::parse($value);
                         } else {
                             if ($field->type() == MApplicationControllerField::BinaryType) {
                                 $object = MData::parseBase64String(S($value));
                             } else {
                                 if ($field->type() == MApplicationControllerField::ArrayType) {
                                     $object = new MMutableArray();
                                     foreach ($value as $v) {
                                         $object->addObject(S($v));
                                     }
                                 } else {
                                     if ($field->type() == MApplicationControllerField::DictionaryType) {
                                         $object = new MMutableDictionary();
                                         foreach ($value as $k => $v) {
                                             $object->setObjectForKey(S($k), S($v));
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } catch (Exception $e) {
         return false;
     }
     if (!$this->fieldValues) {
         $this->fieldValues = new MMutableDictionary();
     }
     if ($object) {
         $this->fieldValues->setObjectForKey($field, $object);
         return true;
     } else {
         return false;
     }
 }
 /**
  * @internal
  *
  * Used internally to parse the Model from it's model file
  *
  * @return void
  */
 protected function parse()
 {
     $xmlModels = simplexml_load_file($this->modelFile()->path()->stringValue());
     if (!$this->version()) {
         $this->version = S((string) $xmlModels->{'current-version'});
     }
     $model = null;
     foreach ($xmlModels->model as $m) {
         if (S((string) $m['version'])->equals($this->version())) {
             $model = $m;
         }
     }
     if ($model != null) {
         $relationshipsToLink = new MMutableDictionary();
         foreach ($model->entity as $entity) {
             $newEntity = new MEntityDescription(S((string) $entity['name']), S((string) $entity['plural']), S((string) $entity['class']));
             foreach ($entity->property as $property) {
                 $newProperty = new MEntityDescriptionProperty($newEntity, S((string) $property['name']));
                 if ((string) $property['type'] != null) {
                     $newProperty->setType((string) $property['type']);
                 }
                 if ((string) $property['defaultValue'] != null) {
                     $defaultValue = null;
                     if ($newProperty->type() == MEntityDescriptionProperty::StringType) {
                         $defaultValue = new MString((string) $property['defaultValue']);
                     } else {
                         if ($newProperty->type() == MEntityDescriptionProperty::IntegerType) {
                             $defaultValue = MNumber::parseInt((string) $property['defaultValue']);
                         } else {
                             if ($newProperty->type() == MEntityDescriptionProperty::FloatType) {
                                 $defaultValue = MNumber::parseFloat((string) $property['defaultValue']);
                             } else {
                                 if ($newProperty->type() == MEntityDescriptionProperty::BooleanType) {
                                     $defaultValue = MNumber::parseBool((string) $property['defaultValue']);
                                 } else {
                                     if ($newProperty->type() == MEntityDescriptionProperty::DateType) {
                                         $defaultValue = MDate::parseString((string) $property['defaultValue']);
                                     } else {
                                         if ($newProperty->type() == MEntityDescriptionProperty::BinaryType) {
                                             // BinaryType's defaultValue is ignored, always null
                                         } else {
                                             throw new MModelParseErrorException($this->modelFile(), Sf("Invalid data type '%s'", $newProperty->type()));
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     $newProperty->setDefaultValue($defaultValue);
                 }
             }
             foreach ($entity->relationship as $relationship) {
                 $newRelationship = new MEntityDescriptionRelationship($newEntity, S((string) $relationship['name']), S((string) $relationship['type']));
                 $newRelationship->setSingular(S((string) $relationship['singular']));
                 if (strtolower((string) $relationship['to']) == "many") {
                     $newRelationship->setTo(MEntityDescriptionRelationship::ToMany);
                 } else {
                     $newRelationship->setTo(MEntityDescriptionRelationship::ToOne);
                 }
                 if ($relationship['inverse'] != null) {
                     $relationshipsToLink->setObjectForKey($newRelationship, S((string) $relationship['inverse']));
                 }
             }
             $this->addEntity($newEntity);
         }
         // Link relationships to their respective inverse relationships
         foreach ($relationshipsToLink->allKeys()->toArray() as $relationship) {
             $inverse = $relationshipsToLink->objectForKey($relationship);
             $inverseEntity = $this->entityWithName($relationship->type());
             if ($inverseEntity) {
                 $inverseRelationship = $inverseEntity->attributeWithName($inverse);
                 if ($inverseRelationship) {
                     $relationship->setInverseRelationship($inverseRelationship);
                 } else {
                     throw new MModelParseErrorException($this->modelFile(), Sf("Could not find the relationship named '%s' in entity '%s'", $inverse, $relationship->type()));
                 }
             } else {
                 throw new MModelParseErrorException($this->modelFile(), Sf("[%s->%s] Inverse relationship's entity named '%s' not defined in this model version", $relationship->entity()->name(), $relationship->name(), $relationship->type()));
             }
         }
     } else {
         throw new MModelVersionNotFoundException($this->modelFile());
     }
 }
Ejemplo n.º 4
0
 /**
  * @internal
  *
  * @return void
  */
 protected function createDatabaseStructure()
 {
     $this->connection()->beginTransaction();
     $model = $this->persistentStoreCoordinator()->model();
     $tables = new MMutableDictionary();
     $relationshipTables = new MMutableDictionary();
     foreach ($model->entities()->toArray() as $entity) {
         $fields = new MMutableArray();
         $fields->addObject(S("`objectID` INT NOT NULL AUTO_INCREMENT"));
         foreach ($entity->attributes()->toArray() as $attribute) {
             if ($attribute instanceof MEntityDescriptionProperty) {
                 $type = "MEDIUMTEXT";
                 if ($attribute->type() == MEntityDescriptionProperty::IntegerType) {
                     $type = "INT";
                 } else {
                     if ($attribute->type() == MEntityDescriptionProperty::FloatType) {
                         $type = "FLOAT";
                     } else {
                         if ($attribute->type() == MEntityDescriptionProperty::BooleanType) {
                             $type = "TINYINT";
                         } else {
                             if ($attribute->type() == MEntityDescriptionProperty::DateType) {
                                 $type = "INT";
                             } else {
                                 if ($attribute->type() == MEntityDescriptionProperty::BinaryType) {
                                     $type = "LONGBLOB";
                                 }
                             }
                         }
                     }
                 }
                 $fields->addObject(Sf("`%s` %s", $attribute->name(), $type));
             } else {
                 if ($attribute instanceof MEntityDescriptionRelationship) {
                     $tableName = $attribute->tableName();
                     if ($relationshipTables->objectForKey($tableName) == null) {
                         $relationshipFields = new MMutableArray();
                         $relationshipFields->addObject(S("`objectID` INT NOT NULL AUTO_INCREMENT"));
                         $relationshipFields->addObject(Sf("`%s` INT NOT NULL", $attribute->columnName()));
                         $relationshipFields->addObject(Sf("`%s` INT NOT NULL", $attribute->inverseColumnName()));
                         $relationshipTables->setObjectForKey($tableName, $relationshipFields);
                     }
                 }
             }
         }
         $tableName = $entity->plural();
         if ($tables->objectForKey($tableName) == null) {
             $tables->setObjectForKey($tableName, $fields);
         } else {
             throw new MPersistentStoreException(Sf("Duplicate entity name ('%s')!", $entity->name()));
         }
     }
     foreach ($tables->allKeys()->toArray() as $tableName) {
         $tableQuery = $this->createTableQuery($tableName, $tables->objectForKey($tableName), S("objectID"));
         $this->connection()->exec($tableQuery->stringValue());
     }
     foreach ($relationshipTables->allKeys()->toArray() as $tableName) {
         $tableQuery = $this->createTableQuery($tableName, $relationshipTables->objectForKey($tableName), S("objectID"));
         $this->connection()->exec($tableQuery->stringValue());
     }
     $tableQuery = $this->createTableQuery(S("Z_METADATA"), A(S("`id` INT NOT NULL AUTO_INCREMENT"), S("`key` MEDIUMTEXT"), S("`value` MEDIUMTEXT")), S("id"));
     $this->connection()->exec($tableQuery);
     $statement = $this->connection()->prepare("INSERT INTO `Z_METADATA` (`key`, `value`) VALUES ('version', ?)");
     $statement->execute(array($this->persistentStoreCoordinator()->model()->version()->stringValue()));
     $success = $this->connection()->commit();
     if (!$success) {
         throw new MPersistentStoreException(S("Could not create database structure!"));
     }
     if ($this->delegate() != null) {
         $this->delegate()->didCreatePersistentStore($this);
     }
 }