WIP refactoring newsLib - late additions

This commit is contained in:
marko
2025-05-11 14:16:00 +02:00
parent a8b3e6c78f
commit 91c0b3ba7a

View File

@@ -0,0 +1,88 @@
<?php
namespace cwsvJudo\News;
function swap(mixed &$x, mixed &$y) : void{
$tmp=$x;
$x=$y;
$y=$tmp;
}
function filter_integer_range(mixed $value, ?int $min = null, ?int $max = null) : ?int {
$options = ["options"=>["flags"=> FILTER_NULL_ON_FAILURE]];
if (!is_null($min) && !is_null($max)){
if( $min > $max ){
swap($min, $max);
}
}
if(!is_null($min)){
$options["options"]["min_range"] = $min;
}
if(!is_null($max)){
$options["options"]["max_range"] = $max;
}
return filter_var(
$value,
FILTER_VALIDATE_INT,
$options,
);
}
function filter_integer(mixed $value) : ?int {
return filter_integer_range($value);
}
function filter_id(mixed $value, int $min = 0) : ?int {
return filter_integer_range($value, $min, null);
}
function filter_url(mixed $value){
return filter_var(
$value,
FILTER_VALIDATE_URL,
["options" => ["flags" => FILTER_NULL_ON_FAILURE]]
);
}
class PromoImage {
public function __construct(array $data) {
$this->src = filter_url($data["src"]);
$this->height = filter_integer_range(
value: $data["height"],
min: 0,
max: null
);
$this->width = filter_integer_range($data["width"], 0, null);
}
public function to_json(){
return json_encode(
value: [
"src" => $this->src,
"width" => $this->width,
"height" => $this->height
]
);
}
private string $src;
private int $width;
private int $height;
}
// a single news entry
class Entry{
// private
private int $id;
private \DateTime $date;
private string $title;
private string $content;
private string $author;
private PromoImage $promo;
}