1<?php
2
3
4namespace ComboStrap;
5
6
7/**
8 * @deprecated use conditional length instead
9 */
10class ConditionalValue
11{
12
13    const CANONICAL = "conditional";
14    /**
15     * @var string
16     */
17    private $value;
18    /**
19     * @var string
20     */
21    private $breakpoint;
22
23    private static $breakpoints = [
24        "xs" => 0,
25        "sm" => 576,
26        "md" => 768,
27        "lg" => 992,
28        "xl" => 1200,
29        "xxl" => 1400
30    ];
31
32    /**
33     * ConditionalValue constructor.
34     * @throws ExceptionBadSyntax
35     */
36    public function __construct($value)
37    {
38        $lastIndex = strrpos($value, "-");
39        if ($lastIndex === false) {
40            $this->breakpoint = null;
41            $this->value = $value;
42            return;
43        }
44        $breakpoint = substr($value, $lastIndex + 1);
45        if (array_key_exists($breakpoint, self::$breakpoints)) {
46            $this->breakpoint = $breakpoint;
47            $this->value = substr($value, 0, $lastIndex);
48            return;
49        }
50        // Old the breakpoints may be in the middle
51        $parts = explode("-", $value);
52        $valueFromParts = [];
53        foreach ($parts as $key => $part) {
54            if (array_key_exists($part, self::$breakpoints)) {
55                $this->breakpoint = $part;
56            } else {
57                $valueFromParts[] = $part;
58            }
59        }
60        if ($this->breakpoint === null) {
61            $this->breakpoint = null;
62            $this->value = $value;
63            return;
64        }
65        $this->value = implode("-", $valueFromParts);
66        LogUtility::warning("The breakpoint conditional value format ($value) will be deprecated in the next releases. It should be written ($this->value-$this->breakpoint)");
67
68    }
69
70    /**
71     * @throws ExceptionBadSyntax
72     */
73    public static function createFrom($value): ConditionalValue
74    {
75        return new ConditionalValue($value);
76    }
77
78    /**
79     * @throws ExceptionBadArgument
80     */
81    public static function checkValidBreakpoint(string $breakpoint)
82    {
83        if (!array_key_exists($breakpoint, self::$breakpoints)) {
84            throw new ExceptionBadArgument("$breakpoint is not a valid breakpoint value");
85        }
86    }
87
88    public function getBreakpoint(): ?string
89    {
90        return $this->breakpoint;
91    }
92
93    public function getValue()
94    {
95        return $this->value;
96    }
97
98    public function getBreakpointSize(): int
99    {
100        return self::$breakpoints[$this->breakpoint];
101    }
102
103
104}
105