Ejemplo n.º 1
0
include_once './bootstrap.php';
$restServer = new RestServer();
try {
    $restServer->setStatus(200);
    $resource = $restServer->getResource();
    $verb = $restServer->getVerb();
    $id = $restServer->getId();
    $serverData = $restServer->getServerData();
    $config = array('DB_DNS' => 'mysql:host=localhost;port=3306;dbname=PHPAdvClassFall2015', 'DB_USER' => 'root', 'DB_PASSWORD' => '');
    $db = new PDO($config['DB_DNS'], $config['DB_USER'], $config['DB_PASSWORD']);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    if ('address' === $resource) {
        $resourceData = new AddressResoruce();
        if ('GET' === $verb) {
            if (NULL === $id) {
                $restServer->setData($resourceData->getAll());
            } else {
                $restServer->setData($resourceData->get($id));
            }
        }
        if ('POST' === $verb) {
            if ($resourceData->post($serverData)) {
                $restServer->setMessage('Address Added');
                $restServer->setStatus(201);
            } else {
                throw new Exception('Address could not be added');
            }
        }
        if ('PUT' === $verb) {
            if (NULL === $id) {
                throw new InvalidArgumentException('Address ID ' . $id . ' was not found');
Ejemplo n.º 2
0
            } else {
                $results = $corps->put($data, $id);
                $RestServer->setMessage($id . ' updated');
            }
        }
        if ($verb === 'POST') {
            if ($corps->post($data)) {
                $RestServer->setMessage('Corporation  Added');
                $RestServer->setStatus(201);
            } else {
                throw new Exception('Corporation not added');
            }
        }
        if ($verb === 'DELETE') {
            if ($id === NULL) {
                throw new InvalidArgumentException('No ID');
            } else {
                if ($corps->delete($id)) {
                    $RestServer->setMessage($id . ' deleted');
                } else {
                    throw new InvalidArgumentException($id . ' not deleted');
                }
            }
        }
        $RestServer->setData($results);
    }
} catch (Exception $ex) {
    $RestServer->setErrors($ex->getMessage());
    $RestServer->setStatus(500);
}
$RestServer->outputReponse();
Ejemplo n.º 3
0
        if ($verb === 'PUT') {
            if ($id === NULL) {
                throw new InvalidArgumentException('Corporation ID ' . $id . ' was not found');
            } else {
                $dataResults = $resourceCorps->put($getData, $id);
            }
        }
        if ($verb === 'POST') {
            if ($resourceCorps->post($getData)) {
                $restServer->setMessage('New Corporation Information Added');
                $restServer->setStatus(201);
            } else {
                throw new Exception('Corporation could not be added');
            }
        }
        if ($verb === 'DELETE') {
            if ($id === NULL) {
                throw new InvalidArgumentException('Id required');
            } else {
                if ($resourceCorps->delete($id)) {
                    $restServer->setMessage($id . ' Has been deleted');
                }
            }
        }
        $restServer->setData($dataResults);
    }
} catch (Exception $ex) {
    $restServer->setErrors($ex->getMessage());
    $restServer->setStatus(500);
}
$restServer->outputResponse();