friendica-github/src/Model/Verb.php

76 lines
1.4 KiB
PHP
Raw Normal View History

2020-05-09 15:38:40 +00:00
<?php
2024-08-24 15:27:00 +02:00
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2020-05-09 15:38:40 +00:00
namespace Friendica\Model;
2020-11-19 19:34:48 +00:00
use Friendica\Database\Database;
2020-05-09 15:38:40 +00:00
use Friendica\Database\DBA;
class Verb
{
static $verbs = [];
2020-05-09 15:38:40 +00:00
/**
* Insert a verb record and return its id
*
* @param string $verb
*
* @return integer verb id
* @throws \Exception
*/
2022-06-23 10:56:37 +02:00
public static function getID(string $verb): int
2020-05-09 15:38:40 +00:00
{
if (empty($verb)) {
return 0;
}
$id = array_search($verb, self::$verbs);
if ($id !== false) {
return $id;
}
2020-05-09 15:38:40 +00:00
$verb_record = DBA::selectFirst('verb', ['id'], ['name' => $verb]);
if (DBA::isResult($verb_record)) {
self::$verbs[$verb_record['id']] = $verb;
2020-05-09 15:38:40 +00:00
return $verb_record['id'];
}
2020-11-19 19:34:48 +00:00
DBA::insert('verb', ['name' => $verb], Database::INSERT_IGNORE);
2020-05-09 15:38:40 +00:00
$id = DBA::lastInsertId();
self::$verbs[$id] = $verb;
return $id;
2020-05-09 15:38:40 +00:00
}
2020-05-19 20:32:15 +00:00
/**
* Return verb name for the given ID
*
* @param integer $id
* @return string verb
*/
2022-06-23 10:56:37 +02:00
public static function getByID(int $id): string
2020-05-19 20:32:15 +00:00
{
if (empty($id)) {
return '';
}
if (!empty(self::$verbs[$id])) {
return self::$verbs[$id];
}
2020-05-19 20:32:15 +00:00
$verb_record = DBA::selectFirst('verb', ['name'], ['id' => $id]);
if (!DBA::isResult($verb_record)) {
return '';
}
self::$verbs[$id] = $verb_record['name'];
2020-05-19 20:32:15 +00:00
return $verb_record['name'];
}
2020-05-09 15:38:40 +00:00
}