federator/php/api/v1/dummy.php

108 lines
2.4 KiB
PHP
Raw Normal View History

2024-07-15 20:46:44 +02:00
<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Sascha Nitsch (grumpydeveloper)
**/
namespace Federator\Api\V1;
/**
* dummy api class for functional poc
*/
class Dummy implements \Federator\Api\V1
{
/**
* \Federator\Main instance
*
* @var \Federator\Main $main
*/
2024-07-15 20:46:44 +02:00
private $main;
/**
* internal message to output
*
* @var Array<string, mixed> $message
*/
2024-07-15 20:46:44 +02:00
private $message = [];
/**
* constructor
*
2024-07-17 21:37:02 +02:00
* @param \Federator\Main $main main instance
2024-07-15 20:46:44 +02:00
*/
public function __construct(\Federator\Main $main)
{
$this->main = $main;
}
/**
* run given url path
*
* @param array<string> $paths path array split by /
2024-07-15 20:46:44 +02:00
* @return bool true on success
*/
public function exec($paths) : bool
{
$method = $_SERVER["REQUEST_METHOD"];
switch ($method) {
case 'GET':
switch (sizeof($paths)) {
case 3:
if ($paths[2] === 'moo') {
return $this->getDummy();
}
}
break;
case 'POST':
switch (sizeof($paths)) {
case 3:
if ($paths[2] === 'moo') {
return $this->postDummy();
}
break;
}
}
$this->main->setResponseCode(404);
return false;
}
/**
* get function for "/v1/dummy/moo"
*
* @return bool
2024-07-15 20:46:44 +02:00
*/
public function getDummy()
2024-07-15 20:46:44 +02:00
{
$this->message = [
'r1' => ' (__) ',
'r2' => ' `------(oo) ',
'r3' => ' || __ (__) ',
'r4' => ' ||w || ',
'r5' => ' '
];
return true;
}
/**
* post function for /v1/dummy/moo"
*
* @return bool
2024-07-15 20:46:44 +02:00
*/
public function postDummy()
2024-07-15 20:46:44 +02:00
{
return $this->getDummy();
}
/**
* get internal represenation as json string
*
2024-07-15 20:46:44 +02:00
* @return string json string
*/
public function toJson()
2024-07-15 20:46:44 +02:00
{
return json_encode($this->message, JSON_PRETTY_PRINT) . "\n";
}
}