szurubooru/src/Models/Model_Post.php

57 lines
1.2 KiB
PHP
Raw Normal View History

2013-10-15 20:22:52 +02:00
<?php
class Model_Post extends RedBean_SimpleModel
{
public static function locate($key, $disallowNumeric = false)
{
if (is_numeric($key) and !$disallowNumeric)
{
$post = R::findOne('post', 'id = ?', [$key]);
if (!$post)
throw new SimpleException('Invalid post ID "' . $key . '"');
}
else
{
$post = R::findOne('post', 'name = ?', [$key]);
if (!$post)
throw new SimpleException('Invalid post name "' . $key . '"');
}
return $post;
}
2013-10-15 20:22:52 +02:00
public static function validateSafety($safety)
{
$safety = intval($safety);
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleException('Invalid safety type "' . $safety . '"');
return $safety;
}
public static function validateTag($tag)
{
$tag = trim($tag);
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
throw new SimpleException('Invalid tag "' . $tag . '"');
return $tag;
}
public static function validateTags($tags)
{
$tags = trim($tags);
$tags = preg_split('/[,;\s]+/', $tags);
$tags = array_filter($tags, function($x) { return $x != ''; });
$tags = array_unique($tags);
foreach ($tags as $key => $tag)
$tags[$key] = self::validateTag($tag);
if (empty($tags))
throw new SimpleException('No tags set');
return $tags;
}
}