public function postRemap()
    {
        $oUri = Factory::service('Uri');
        //  Test that there's not an explicit method defined for this
        $sMethod = 'post' . ucfirst($oUri->segment(4));
        if (method_exists($this, $sMethod)) {
            return $this->$sMethod();
        }

        $oInput     = Factory::service('Input');
        $oHttpCodes = Factory::service('HttpCodes');
        $oItemModel = Factory::model(
            static::CONFIG_MODEL_NAME,
            static::CONFIG_MODEL_PROVIDER
        );
        //  Validate fields
        $aFields  = $oItemModel->describeFields();
        $aValid   = [];
        $aInvalid = [];
        foreach ($aFields as $oField) {
            if (in_array($oField->key, static::CONFIG_POST_IGNORE_FIELDS)) {
                continue;
            }
            $aValid[] = $oField->key;
        }

        $aPost = $oInput->post();
        foreach ($aPost as $sKey => $sValue) {
            if (!in_array($sKey, $aValid)) {
                $aInvalid[] = $sKey;
            }
        }

        if (!empty($aInvalid)) {
            throw new ApiException(
                'The following arguments are invalid: ' . implode(', ', $aInvalid),
                $oHttpCodes::STATUS_BAD_REQUEST
            );
        }

        $iItemId = (int) $oUri->segment(4);
        if ($iItemId) {
            $oItem = $oItemModel->getById($iItemId);
            if (empty($oItem)) {
                throw new ApiException(
                    'Item does not exist',
                    $oHttpCodes::STATUS_NOT_FOUND
                );
            } elseif (!$oItemModel->update($iItemId, $aPost)) {
                throw new ApiException(
                    'Failed to update item. ' . $oItemModel->lastError(),
                    $oHttpCodes::STATUS_INTERNAL_SERVER_ERROR
                );
            } elseif (classUses($oItemModel, 'Nails\Common\Traits\Caching')) {
                $oItemModel->disableCache();
            }
            $oItem = $oItemModel->getById($iItemId);
            if (classUses($oItemModel, 'Nails\Common\Traits\Caching')) {
                $oItemModel->enableCache();
            }
        } else {
            $oItem = $oItemModel->create($aPost, true);
        }

        $oResponse = Factory::factory('ApiResponse', 'nails/module-api');
        $oResponse->setData($this->formatObject($oItem));
        return $oResponse;
    }
public function postRemap()
    {
        $oUri = Factory::service('Uri');
        //  Test that there's not an explicit method defined for this
        $sMethod = 'post' . ucfirst($oUri->segment(4));
        if (method_exists($this, $sMethod)) {
            return $this->$sMethod();
        }

        $oInput     = Factory::service('Input');
        $oHttpCodes = Factory::service('HttpCodes');
        $oItemModel = Factory::model(
            static::CONFIG_MODEL_NAME,
            static::CONFIG_MODEL_PROVIDER
        );
        //  Validate fields
        $aFields  = $oItemModel->describeFields();
        $aValid   = [];
        $aInvalid = [];
        foreach ($aFields as $oField) {
            if (in_array($oField->key, static::CONFIG_POST_IGNORE_FIELDS)) {
                continue;
            }
            $aValid[] = $oField->key;
        }

        $aPost = $oInput->post();
        foreach ($aPost as $sKey => $sValue) {
            if (!in_array($sKey, $aValid)) {
                $aInvalid[] = $sKey;
            }
        }

        if (!empty($aInvalid)) {
            throw new ApiException(
                'The following arguments are invalid: ' . implode(', ', $aInvalid),
                $oHttpCodes::STATUS_BAD_REQUEST
            );
        }

        $iItemId = (int) $oUri->segment(4);
        if ($iItemId) {
            $oItem = $oItemModel->getById($iItemId);
            if (empty($oItem)) {
                throw new ApiException(
                    'Item does not exist',
                    $oHttpCodes::STATUS_NOT_FOUND
                );
            } elseif (!$oItemModel->update($iItemId, $aPost)) {
                throw new ApiException(
                    'Failed to update item. ' . $oItemModel->lastError(),
                    $oHttpCodes::STATUS_INTERNAL_SERVER_ERROR
                );
            } elseif (classUses($oItemModel, 'Nails\Common\Traits\Caching')) {
                $oItemModel->disableCache();
            }
            $oItem = $oItemModel->getById($iItemId);
            if (classUses($oItemModel, 'Nails\Common\Traits\Caching')) {
                $oItemModel->enableCache();
            }
        } else {
            $oItem = $oItemModel->create($aPost, true);
        }

        $oResponse = Factory::factory('ApiResponse', 'nails/module-api');
        $oResponse->setData($this->formatObject($oItem));
        return $oResponse;
    }
public function postRemap ( ) { $ oUri = Factory :: service ( 'Uri' ) ; //  Test that there's not an explicit method defined for this $ sMethod = 'post' . ucfirst ( $ oUri -> segment ( 4 ) ) ; if ( method_exists ( $ this , $ sMethod ) ) { return $ this -> $ sMethod ( ) ; } $ oInput = Factory :: service ( 'Input' ) ; $ oHttpCodes = Factory :: service ( 'HttpCodes' ) ; $ oItemModel = Factory :: model ( static :: CONFIG_MODEL_NAME , static :: CONFIG_MODEL_PROVIDER ) ; //  Validate fields $ aFields = $ oItemModel -> describeFields ( ) ; $ aValid = [ ] ; $ aInvalid = [ ] ; foreach ( $ aFields as $ oField ) { if ( in_array ( $ oField -> key , static :: CONFIG_POST_IGNORE_FIELDS ) ) { continue ; } $ aValid [ ] = $ oField -> key ; } $ aPost = $ oInput -> post ( ) ; foreach ( $ aPost as $ sKey => $ sValue ) { if ( ! in_array ( $ sKey , $ aValid ) ) { $ aInvalid [ ] = $ sKey ; } } if ( ! empty ( $ aInvalid ) ) { throw new ApiException ( 'The following arguments are invalid: ' . implode ( ', ' , $ aInvalid ) , $ oHttpCodes :: STATUS_BAD_REQUEST ) ; } $ iItemId = ( int ) $ oUri -> segment ( 4 ) ; if ( $ iItemId ) { $ oItem = $ oItemModel -> getById ( $ iItemId ) ; if ( empty ( $ oItem ) ) { throw new ApiException ( 'Item does not exist' , $ oHttpCodes :: STATUS_NOT_FOUND ) ; } elseif ( ! $ oItemModel -> update ( $ iItemId , $ aPost ) ) { throw new ApiException ( 'Failed to update item. ' . $ oItemModel -> lastError ( ) , $ oHttpCodes :: STATUS_INTERNAL_SERVER_ERROR ) ; } elseif ( classUses ( $ oItemModel , 'Nails\Common\Traits\Caching' ) ) { $ oItemModel -> disableCache ( ) ; } $ oItem = $ oItemModel -> getById ( $ iItemId ) ; if ( classUses ( $ oItemModel , 'Nails\Common\Traits\Caching' ) ) { $ oItemModel -> enableCache ( ) ; } } else { $ oItem = $ oItemModel -> create ( $ aPost , true ) ; } $ oResponse = Factory :: factory ( 'ApiResponse' , 'nails/module-api' ) ; $ oResponse -> setData ( $ this -> formatObject ( $ oItem ) ) ; return $ oResponse ; }
Creates a new item

@return array
Creates a new item