dealnews/data-mapper-api

There is no license information available for the latest version (2.2.0) of this package.

API wrapper around data-mapper library

2.2.0 2024-12-31 00:13 UTC

This package is auto-updated.

Last update: 2024-12-31 00:19:49 UTC


README

A simple API that can be installed in other projects that exposes the Data Mapper library in said project to other services.

Table of Contents

Requirements

Installation

composer require php-libraries/data-mapper-api

Usage

Before using this library, you will need a \DealNews\DataMapper\Repository object that maps your data mappers to object names if you have not already created one.

$repo = new \DealNews\DataMapper\Repository();
$repo->addMapper("Example", new \DealNews\Example\Mapper());

You will also need a router of some sort to route API requests to the appropriate code/action. For most cases, you can just use the PageMill Router. To help with building routes for the PageMill Router, this library comes with some pre-defined routes and a helper method to make sure input data is properly formatted for the API endpoint.

Routing for just one specific endpoint (if you're only interested in using a portion of the provided endpoints/actions from this library):

$api = new \DealNews\DataMapperAPI\API();

$routes = [
    $api->getRoute('get_object_route')
];

$router = new \PageMill\Router\Router($routes);

$route = $router->match();

if (!empty($route)) {
    $api->executeAction($route['action'], $route['tokens'], 'https://example.com', $repo);
}

Routing for all provided endpoints:

$api = new \DealNews\DataMapperAPI\API();

$router = new \PageMill\Router\Router($api->getAllRoutes());

$route = $router->match();

if (!empty($route)) {
    $api->executeAction($route['action'], $route['tokens'], 'https://example.com', $repo);
}

By default, all endpoint paths have a prefix of /api. However, you can change this by passing a different prefix to either getRoute() or getAllRoutes() methods. Example:

$api = new \DealNews\DataMapperAPI\API();

$router = new \PageMill\Router\Router($api->getAllRoutes('/different-api-path-prefix'));

$route = $router->match();

if (!empty($route)) {
    $api->executeAction($route['action'], $route['tokens'], 'https://example.com', $repo);
}

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting merge requests for this project.