54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Smilia\Core;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class Prices {
|
|
|
|
public static function groups( array $groups ): array {
|
|
$urls = Content::service_urls();
|
|
$result = array();
|
|
foreach ( $groups as $group ) {
|
|
$title = self::plain( (string) ( $group['title'] ?? '' ) );
|
|
$items = array();
|
|
foreach ( (array) ( $group['items'] ?? array() ) as $item ) {
|
|
$name = self::plain( (string) ( $item['name'] ?? '' ) );
|
|
if ( '' === $name ) {
|
|
continue;
|
|
}
|
|
$raw = self::plain( (string) ( $item['price'] ?? '' ) );
|
|
$items[] = array(
|
|
'name' => $name,
|
|
'price' => self::format( $raw ),
|
|
'amount' => ctype_digit( preg_replace( '/\s/', '', $raw ) ) && '' !== $raw ? (int) preg_replace( '/\s/', '', $raw ) : null,
|
|
);
|
|
}
|
|
if ( '' === $title || ! $items ) {
|
|
continue;
|
|
}
|
|
$service = sanitize_title( (string) ( $group['service'] ?? '' ) );
|
|
$result[] = array(
|
|
'title' => $title,
|
|
'service' => $service,
|
|
'url' => $urls[ $service ] ?? '',
|
|
'items' => $items,
|
|
);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
private static function plain( string $value ): string {
|
|
return trim( html_entity_decode( wp_strip_all_tags( $value ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) );
|
|
}
|
|
|
|
public static function format( string $price ): string {
|
|
$price = self::plain( $price );
|
|
$digits = preg_replace( '/[^\d]/', '', $price );
|
|
if ( '' === $price || $digits !== preg_replace( '/\s/', '', $price ) ) {
|
|
return $price;
|
|
}
|
|
return sprintf( __( 'от %s zł', 'smilia-core' ), number_format_i18n( (int) $digits ) );
|
|
}
|
|
}
|