[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Type / Bitmask / JsConfirmation.php
1 <?php
2 namespace TYPO3\CMS\Core\Type\Bitmask;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Type\Enumeration;
18 use TYPO3\CMS\Core\Type\Exception;
19
20 /**
21 * A class providing constants for bitwise operations on javascript confirmation popups
22 */
23 class JsConfirmation extends Enumeration
24 {
25 /**
26 * @var int
27 */
28 const TYPE_CHANGE = 0b00000001;
29
30 /**
31 * @var int
32 */
33 const COPY_MOVE_PASTE = 0b00000010;
34
35 /**
36 * @var int
37 */
38 const DELETE = 0b00000100;
39
40 /**
41 * @var int
42 */
43 const FE_EDIT = 0b00001000;
44
45 /**
46 * @var int
47 */
48 const OTHER = 0b10000000;
49
50 /**
51 * @var int
52 */
53 const ALL = 255;
54
55 protected static $allowedValues = self::TYPE_CHANGE | self::COPY_MOVE_PASTE | self::DELETE | self::FE_EDIT | self::OTHER;
56
57 /**
58 * Returns TRUE if a given value matches the internal value
59 *
60 * @param JsConfirmation $value Value to check
61 * @return bool
62 */
63 public function matches(JsConfirmation $value)
64 {
65 $value = (int)(string)$value;
66 $thisValue = (int)$this->value;
67
68 return ($value & $thisValue) == $thisValue;
69 }
70
71 /**
72 * Set the Enumeration value to the associated enumeration value by a loose comparison.
73 * The value, that is used as the enumeration value, will be of the same type like defined in the enumeration
74 *
75 * @param mixed $value
76 * @throws Exception\InvalidEnumerationValueException
77 */
78 protected function setValue($value)
79 {
80 if ($value < 255) {
81 if (($value & self::$allowedValues) !== $value) {
82 throw new Exception\InvalidEnumerationValueException(
83 sprintf('Invalid value %s for %s', $value, __CLASS__),
84 1457175152
85 );
86 }
87 $this->value = $value;
88 } else {
89 parent::setValue($value);
90 }
91 }
92
93 /**
94 * Check if the value on this enum is a valid value for the enum
95 *
96 * @param mixed $value
97 * @return bool
98 */
99 protected function isValid($value)
100 {
101 if ($value < 255) {
102 return ($value & self::$allowedValues) === $value;
103 }
104
105 $value = (string)$value;
106 foreach (static::$enumConstants[get_class($this)] as $constantValue) {
107 if ($value === (string)$constantValue) {
108 return true;
109 }
110 }
111 return false;
112 }
113 }