function BuildProperty($name, $desc, $type, $object) { $model = new Properties(); $model->name = $name; $model->description = $desc; // If APIS_addressmodel keep only addressmodel and make it Addressmodel // If Checkin_openicheckin keep only checkin and make it Checkin $model->type = $type; $pos = strpos($model->type, '_'); if ($pos !== false) { $model->type = substr($model->type, $pos + 1); } $model->type = str_replace("openi", "", $model->type); $model->type = ucfirst($model->type); $model->object = $object; $model->save(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Properties::find(); $query->joinWith(['createdBy']); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['createdBy.username'] = ['asc' => ['user.username' => SORT_ASC], 'desc' => ['user.username' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'object' => $this->object, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'type', $this->type]); return $dataProvider; }
/** * Finds the Properties model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Properties the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Properties::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Publishes an API Swagger. * If publish is successful, the browser will be redirected to the 'swagger/index?url=$model->name' page. * @param integer $id * @return mixed */ public function actionPublish($id) { $api = $this->findModel($id); $api->published = 1; $api->save(); $apiName = preg_replace('/\\s+/', '', $api->name); $change = new NotifAPIHelper(); $followersNotified = $change->apiChangedPublished($id); $basePathPart = Url::canonical(); $basePathPart = explode('swagger', $basePathPart); $basePathPart = explode('hydra', $basePathPart[0]); $basePathPart = explode('raml', $basePathPart[0]); $basePathPart = explode('wadl', $basePathPart[0]); $basePath = $basePathPart[0] . $apiName . '/'; $objects = Objects::findAll(['api' => $id]); $swaggerJSON = new BuildSwaggerAnnotationsOnly(); $swaggerJSON->BuildJSON(); foreach ($objects as $object) { $object->methods = explode(',', $object->methods); // If this Resource has Methods if ($object->methods !== ['']) { $swaggerJSON->BuildResource($api->version, '1.2', $basePath, $object->name); foreach ($object->methods as $methodName) { // $methodParts[method_name, path] $tempMethodParts = explode(' ', $methodName); foreach ($tempMethodParts as $key => $methodPart) { if ($key === 0) { $methodParts[$key] = $methodPart; $methodParts[1] = ''; } else { $methodParts[1] .= $methodPart; } } $upper_method = strtoupper($methodParts[0]); // $pathParts = [object, ({id}, connection)] $pathParts = explode('/', $methodParts[1]); $numOfPathParts = count($pathParts); $swaggerJSON->BuildAPI($methodParts[1], NULL); $summary = ''; $nickname = ''; $returnType = ''; switch ($upper_method) { case 'GET': switch ($numOfPathParts) { case 1: $summary = 'Retrieve a list of ' . $pathParts[0]; $nickname = 'Get_all_' . $pathParts[0]; $returnType = 'list_' . $pathParts[0]; break; case 2: $summary = 'Retrieve one ' . $pathParts[0]; $nickname = 'Get_one_' . $pathParts[0]; $returnType = $pathParts[0]; break; case 3: $summary = 'Retrieve a list of ' . $pathParts[0] . ' ' . $pathParts[2]; $nickname = 'Get_all_' . $pathParts[2]; $returnType = 'list_' . $pathParts[2]; } break; case 'POST': switch ($numOfPathParts) { case 1: $summary = 'Create a new ' . $pathParts[0] . ' object'; $nickname = 'Post_one_' . $pathParts[0]; $returnType = $pathParts[0]; break; case 3: $summary = 'Create a new ' . $pathParts[2] . 'as a cconection of ' . $pathParts[0]; $nickname = 'Post_one_' . $pathParts[2]; $returnType = $pathParts[2] . '_post'; } break; case 'PUT': switch ($numOfPathParts) { case 2: $summary = 'Change a particular ' . $pathParts[0]; $nickname = 'Put_' . $pathParts[0]; $returnType = $pathParts[0]; } break; case 'DELETE': switch ($numOfPathParts) { case 1: $summary = 'Delete all ' . $pathParts[0]; $nickname = 'Delete_all_' . $pathParts[0]; $returnType = 'list_' . $pathParts[0]; break; case 2: $summary = 'Delete one ' . $pathParts[0]; $nickname = 'Delete_one_' . $pathParts[0]; $returnType = 'boolean'; break; case 3: $summary = 'Delete all ' . $pathParts[2] . ' connections of ' . $pathParts[0]; $nickname = 'Delete_all_' . $pathParts[0]; $returnType = 'boolean'; } break; } $swaggerJSON->BuildOperation($upper_method, $summary, '', $returnType, $nickname); if ($numOfPathParts > 1) { $swaggerJSON->BuildParameter('id', 'Primary key of resource', true, 'integer', 'body'); } if ($upper_method === 'POST' or $upper_method === 'PUT') { $swaggerJSON->BuildParameter($pathParts[0], 'Model of resource', true, $pathParts[0] . '_post_put', 'query'); } $swaggerJSON->CloseOperation(); $swaggerJSON->CloseAPI(); } $swaggerJSON->CloseResource(); // ALL THE DIFFERENT MODEL TYPES THAT NEED TO BE IMPLEMENTED // Meta // $pathParts[0] + Meta // $pathParts[0] . '_post_put' // $pathParts[2] . '_post' // 'list_' . $pathParts[0] // 'list_' . $pathParts[2] // THE MODELS FOR THE CONNECTIONS HAVE TO BE BUILT WHEN THAT PARTICULAR OBJECT COMES IN $swaggerJSON->BuildModel('Meta'); $swaggerJSON->BuildProperty('previous', 'Uri of the previous page relative to the current page settings.', 'string'); $swaggerJSON->BuildProperty('next', 'Uri of the next page relative to the current page settings.', 'string'); $swaggerJSON->BuildProperty('total_count', 'Total items count for all the collection.', 'integer'); $swaggerJSON->BuildProperty('offset', 'Specify the offset to start displaying element on a page.', 'integer'); $swaggerJSON->BuildProperty('limit', 'Specify the number of element to display per page.', 'integer'); $swaggerJSON->CloseModel(); $properties = Properties::findAll(['object' => $object->id]); // $pathParts[0] + Meta $swaggerJSON->BuildModel($object->name); foreach ($properties as $property) { if ($property->type === 'byte') { $swaggerJSON->BuildProperty($property->name, $property->description, 'string', '', 'byte'); } else { if ($property->type === 'date') { $swaggerJSON->BuildProperty($property->name, $property->description, 'string', '', 'date'); } else { if ($property->type === 'dateTime') { $swaggerJSON->BuildProperty($property->name, $property->description, 'string', '', 'date-format'); } else { if ($property->type === 'long') { $swaggerJSON->BuildProperty($property->name, $property->description, 'integer', '', 'int64'); } else { switch ($property->type) { case 'Integer': case 'String': $property->type = strtolower($property->type); } $swaggerJSON->BuildProperty($property->name, $property->description, $property->type); } } } } } $swaggerJSON->BuildProperty('meta', '', 'Meta'); $swaggerJSON->CloseModel(); // $pathParts[0] . '_post_put' $swaggerJSON->BuildModel($object->name . '_post_put'); foreach ($properties as $property) { if ($property->name !== 'id') { if ($property->type === 'byte') { $swaggerJSON->BuildProperty($property->name, $property->description, 'string', '', 'byte'); } else { if ($property->type === 'date') { $swaggerJSON->BuildProperty($property->name, $property->description, 'string', '', 'date'); } else { if ($property->type === 'dateTime') { $swaggerJSON->BuildProperty($property->name, $property->description, 'string', '', 'date-format'); } else { if ($property->type === 'long') { $swaggerJSON->BuildProperty($property->name, $property->description, 'integer', '', 'int64'); } else { switch ($property->type) { case 'Integer': case 'String': $property->type = strtolower($property->type); } $swaggerJSON->BuildProperty($property->name, $property->description, $property->type); } } } } } } $swaggerJSON->CloseModel(); // 'list_' . $pathParts[0] $swaggerJSON->BuildModel('Objects_' . $object->name); $swaggerJSON->BuildProperty($object->name, '', 'array', $object->name); $swaggerJSON->CloseModel(); $swaggerJSON->BuildModel('list_' . $object->name); $swaggerJSON->BuildProperty('meta', '', 'Meta'); $swaggerJSON->BuildProperty('objects', '', 'Objects_' . $object->name); $swaggerJSON->CloseModel(); } } $swaggerJSON->CloseJSON(); $file = new FileManipulation(); $file->setFilename(ucfirst($apiName) . '/' . $apiName . '.php'); $file->makeDirectory(ucfirst($apiName) . '/'); $file->write_file($swaggerJSON->getSwaggerJSON()); // Actually Publish the API $_file = Yii::getAlias('@apisDirectory') . '/' . ucfirst($apiName); $swagger = new Swagger($_file); $basePathPart = explode('/apis/', $basePathPart[0]); $writeFiles = new FileManipulation(); $writeFiles->setFilename(ucfirst($apiName) . '/api-docs.json'); $writeFiles->write_file($swagger->getResourceList(array('output' => 'json', 'basePath' => $basePathPart[0] . '/api-docs/' . ucfirst($apiName)))); foreach ($swagger->registry as $api_name => $api_resource) { $writeFiles->setFilename(ucfirst($apiName) . '/' . $api_name); $writeFiles->write_file($swagger->getResource($api_name, array('output' => 'json'))); } return $apiName; }
public function getProperty() { return $this->hasOne(Properties::className(), ['id' => 'property_id']); }
private function saveProperties($model) { if ($properties = Yii::$app->request->post('ExclusivesProperties', false)) { foreach ($properties as $property) { if ($propertyName = Properties::find()->where(['name' => $property['name']])->one()) { if ($propertyVal = ExclusivesProperties::find()->where(['property_id' => $propertyName['id'], 'exclusive_id' => $model->id])->one()) { $propertyVal->value = $property['value']; $propertyVal->save(); } else { $propertyVal = new ExclusivesProperties(); $propertyVal->exclusive_id = $model->id; $propertyVal->property_id = $propertyName->id; $propertyVal->value = $property['value']; $propertyVal->save(); } } else { $propertyName = new Properties(); $propertyName->name = $property['name']; $propertyName->save(); $propertyVal = new ExclusivesProperties(); $propertyVal->exclusive_id = $model->id; $propertyVal->property_id = $propertyName->id; $propertyVal->value = $property['value']; $propertyVal->save(); } } } }
/** * Build a Property * * @param string $name * @param object $value * @param integer $objectId * * @return boolean */ private function BuildProperty($name, $value, $objectId) { $property = new Properties(); $property->name = $name; $property->description = property_exists($value, 'description') ? $value->description : ''; $property->object = $objectId; // Check if there is a type definition, cases have been based on https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#dataTypeFormat // There is also a possibility for an array type, which is a 1-* reference for another Object // Another case is that of $ref as an 1-1 reference to another Object if (property_exists($value, 'type')) { switch ($value->type) { case 'integer': if (property_exists($value, 'format')) { switch ($value->format) { case 'int32': $property->type = 'integer'; break; case 'int64': $property->type = 'long'; break; } } break; case 'number': if (property_exists($value, 'format')) { switch ($value->format) { case 'float': $property->type = 'float'; break; case 'double': $property->type = 'double'; break; } } break; case 'string': $property->type = 'string'; if (property_exists($value, 'format')) { switch ($value->format) { case 'byte': $property->type = 'byte'; break; case 'date': $property->type = 'date'; break; case 'date-time': $property->type = 'dateTime'; break; case 'password': $property->type = 'password'; break; } $property->type = 'byte'; } break; case 'boolean': $property->type = 'boolean'; break; case 'array': if (property_exists($value, 'items')) { $items = $value->items; if (property_exists($items, '$ref')) { // Get the last item out of the array produced from the $ref string exploded at '/', with capitalized first letter $property->type = '[' . ucfirst(end(explode('/', $items->{'$ref'}))) . ']'; } elseif (property_exists($items, 'type')) { if (property_exists($items, 'xml') and property_exists($items->xml, 'name')) { $property->type = '[' . $items->xml->name . ' (' . $items->type . ')]'; } else { $property->type = '[' . $items->type . ']'; } } } break; default: $property->type = 'string'; } } if (property_exists($value, '$ref')) { // Get the last item out of the array produced from the $ref string exploded at '/', with capitalized first letter $property->type = ucfirst(end(explode('/', $value->{'$ref'}))); } return $property->save(); }
/** * @return \yii\db\ActiveQuery */ public function getProperties() { return $this->hasMany(Properties::className(), ['updated_by' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getProperties() { return $this->hasMany(Properties::className(), ['object' => 'id']); }
/** * Duplicates an Object model under this API. * If duplication is successful, the browser will be redirected to the 'view' page. * If the Object has inheritance then retrieve all the Properties and Methods of the parent. * @param integer $id * @return mixed */ public function actionDuplicate($id) { $model = new Objects(); $apiModel = $this->findAPIModel($id); if ($model->load(Yii::$app->request->post()) && $model->inherited != '') { $parentModel = $this->findModel($model->inherited); $model->inherited = $parentModel->id; $model->api = $id; $model->description = $parentModel->description; $model->privacy = $parentModel->privacy; $model->methods = $parentModel->methods; if ($model->save()) { // Link all CBS that the parent model had $cbss = ObjectCBS::find()->where(['object' => $model->inherited])->all(); if ($cbss) { foreach ($cbss as $cbs) { $objectCbs = new ObjectCBS(); $objectCbs->object = $model->id; $objectCbs->cbs = $cbs->cbs; $objectCbs->save(); } } $properties = Properties::findAll(['object' => $parentModel->id]); foreach ($properties as $property) { $prop = new Properties(); $prop->name = $property->name; $prop->description = $property->description; $prop->type = $property->type; $prop->object = $model->id; $prop->save(); } $change = new NotifAPIHelper(); $followersNotified = $change->apiChangedObjectsNumber($id); // Elastic Search Update $esu = new ElasticSearchPut(); $esu->setApi($apiModel); $esu->MakeJSON(); $esu->InsertUpdate(); return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('duplicate', ['model' => $model, 'api' => $apiModel]); }