/** * * * @return MNumber */ public static function parse($number) { $num = $number; if ($number instanceof MString) { $num = (string) $number; } if (is_numeric($num)) { if (is_float($num)) { return MNumber::parseFloat($num); } else { if (is_int($num)) { return MNumber::parseInt($num); } else { if (is_string($num)) { return MNumber::parseInt(intval($num)); } } } } else { if (is_bool($num)) { return MNumber::parseBool($num); } else { if (is_string($num)) { return MNumber::parseInt($num); } } } }
/** * @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()); } }