/** * Constructor for this ApiFieldMap. * * @param string $request_field * @param integer $flags //a combination of flags of this field * @param string $description * @param string $model_accessor * //eg. provide: "CountryByCountryCode" and it will become * "getCountryByCountryCode" and "setCountryByCountryCode" * * @return \sfAltumoPlugin\Api\ApiFieldMap */ public function __construct($request_field, $flags = null, $description = null, $model_accessor = null) { if (!is_string($request_field)) { throw new \Exception('Request field must be a string.'); } $this->setRequestField($request_field); if (!is_null($flags)) { $this->setFlags($flags); } if (!is_null($description)) { $this->setDescription($description); } else { $description = \Altumo\String\String::formatTitleCase(str_replace('_', ' ', $request_field)); $this->setDescription($description); } if (!is_null($model_accessor)) { $this->setModelAccessor($model_accessor); } else { $accessor_suffix = \Altumo\String\String::formatCamelCase($request_field); $this->setModelAccessor($accessor_suffix); } }
/** * Constructor for this ApiWriteOperation. * * * @param \sfAltumoPlugin\Api\ApiRequest $request * * @param \sfAltumoPlugin\Api\ApiResponse $response * * @param string $body_name * //the plural name of the container of the results. * eg. system_events * * @param function $modify_result( &$model, &$result ) * //Used in both Automatic and Manual Modes. The function that turns the * array of model objects into an array of \stdClass objects (the final * result). Must return an array of \stdClass objects. * In Manual mode, $result is an empty \stdClass that you modify. * Because $result is required to be passed by reference, any * modifications to $result will be the result that is returned from the * API. Also, the $model is a \stdClass (or can be). * In Automatic mode, $result will have to be constructed with * the modify_result callback. You should be able to share the * modify_result callback with GET, POST and PUT. Also, in auto, the * $model object is guaranteed to be a model object. * * @param function $before_save( &$model, &$request_object, &$response, * $remote_id ) * //Used in Automatic Mode. The function to call that is invoked just * just before the model is saved (after all of the accessor have been * called). You can attached an Error to the response with this remote_id * to prevent this model from being saved and output an appropriate error * message. * * @param \ModelCriteria $query * //The model Query that is used to look up an existing record for * PUT operations. If null, it will just use the default one from the * model. * * @param string $model_name * //Used in Automatic Mode. The model's class name. If null, it tries to * guess based off of the $body_name. * eg. * system_events * becomes * SystemEvent * * @param array $field_maps * //Used in Automatic Mode. An array of ApiFieldMap objects that describes * each of the fields' relationship with a the $model_name model. * Defaults to a required field. If null, description and setter_method * are generated according to the naming convention, unless overridden. * eg. * //legend: [ field_key, required, description, setter_method ] * $field_maps = array( * new \sfAltumoPlugin\Api\ApiFieldMap( 'system_event' ), * new \sfAltumoPlugin\Api\ApiFieldMap( 'remote_url', true, * 'Remote URL' ), * new \sfAltumoPlugin\Api\ApiFieldMap( 'enabled', false, null, * 'Enabled' ) //will become setEnabled and getEnabled * ); * * @param boolean * //Determines if this ApiWriteOperation is a POST (create) or a PUT * (update). If true, this is a PUT. Default false (POST). * * @param integer $mode * //Whether this is \sfAltumoPlugin\Api\ApiWriteOperation::MODE_AUTOMATIC * or \sfAltumoPlugin\Api\ApiWriteOperation::MODE_AUTOMATIC. Defaults to * automatic. * * @param function $process_objects_manually( &$response, &$objects ){ * //Used in Manual Mode. The function to call in order to perform a manual * write operation. Must return an array of saved Models or stdObjects. * $objects is an array of stdObjects that is the request body. * * @param function $before_setters( &$model, &$request_object, &$response ) * //Used in Automatic Mode. The function to call that is invoked just * after the model is created (before all of the accessor have been * called). This will only be invoked if this is a create (POST). * * @param function $after_save( $new_model, $request_object, $response, $remote_id, $update ) * //Used in Automatic Mode. The function to call that is invoked just after the model is saved. * Any exception thrown will cause the entire operation to be rolled-back. * * @throws \Exception * //if $field_maps is not an array * * @return \sfAltumoPlugin\Api\ApiWriteOperation */ public function __construct($request, $response, $body_name, $field_maps = array(), $update = false, $modify_result = null, $before_save = null, $query = null, $model_name = null, $mode = self::MODE_AUTOMATIC, $process_objects_manually = null, $before_setters = null, $after_save = null) { //request $this->setRequest($request); //response and body name $response_body = new \sfAltumoPlugin\Api\ApiResponseBody(array(), $body_name); $response->setResponseBody($response_body); $this->setResponse($response); //field maps if (!is_array($field_maps)) { throw new \Exception('Field maps must be an array of ApiFieldMap objects.'); } $this->setFieldMaps($field_maps); //update $this->setUpdate($update); //modify result callback if (is_callable($modify_result)) { $this->setModifyResult($modify_result); } //before save if (is_callable($before_save)) { $this->setBeforeSave($before_save); } // after save if (is_callable($after_save)) { $this->setAfterSave($after_save); } //query $this->setQuery($query); //model name if (is_null($model_name)) { $plural_model = \Altumo\String\String::formatCamelCase($body_name); $this->setModelName(substr($plural_model, 0, strlen($plural_model) - 1)); } else { $this->setModelName($model_name); } //mode $this->setMode($mode); //manually process objects if (is_callable($process_objects_manually)) { $this->setProcessObjectsManually($process_objects_manually); } //before save if (is_callable($before_setters)) { $this->setBeforeSetters($before_setters); } }