Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Models\Attributes; |
| 4 | |
| 5 | class EscapedValue |
| 6 | { |
| 7 | /** |
| 8 | * The value to be escaped. |
| 9 | * |
| 10 | * @var string |
| 11 | */ |
| 12 | protected $value; |
| 13 | |
| 14 | /** |
| 15 | * The characters to ignore when escaping. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $ignore; |
| 20 | |
| 21 | /** |
| 22 | * The escape flags. |
| 23 | * |
| 24 | * @var int |
| 25 | */ |
| 26 | protected $flags; |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | * |
| 31 | * @param string $value |
| 32 | * @param string $ignore |
| 33 | * @param int $flags |
| 34 | */ |
| 35 | public function __construct($value, $ignore = '', $flags = 0) |
| 36 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 37 | $this->value = (string) $value; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 38 | $this->ignore = $ignore; |
| 39 | $this->flags = $flags; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the escaped value. |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | public function __toString() |
| 48 | { |
| 49 | return (string) $this->get(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the escaped value. |
| 54 | * |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public function get() |
| 58 | { |
| 59 | return ldap_escape($this->value, $this->ignore, $this->flags); |
| 60 | } |
| 61 | |
| 62 | /** |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 63 | * Get the raw (unescaped) value. |
| 64 | * |
| 65 | * @return mixed |
| 66 | */ |
| 67 | public function raw() |
| 68 | { |
| 69 | return $this->value; |
| 70 | } |
| 71 | |
| 72 | /** |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 73 | * Set the characters to exclude from being escaped. |
| 74 | * |
| 75 | * @param string $characters |
| 76 | * |
| 77 | * @return $this |
| 78 | */ |
| 79 | public function ignore($characters) |
| 80 | { |
| 81 | $this->ignore = $characters; |
| 82 | |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Prepare the value to be escaped for use in a distinguished name. |
| 88 | * |
| 89 | * @return $this |
| 90 | */ |
| 91 | public function dn() |
| 92 | { |
| 93 | $this->flags = LDAP_ESCAPE_DN; |
| 94 | |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Prepare the value to be escaped for use in a filter. |
| 100 | * |
| 101 | * @return $this |
| 102 | */ |
| 103 | public function filter() |
| 104 | { |
| 105 | $this->flags = LDAP_ESCAPE_FILTER; |
| 106 | |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Prepare the value to be escaped for use in a distinguished name and filter. |
| 112 | * |
| 113 | * @return $this |
| 114 | */ |
| 115 | public function both() |
| 116 | { |
| 117 | $this->flags = LDAP_ESCAPE_FILTER + LDAP_ESCAPE_DN; |
| 118 | |
| 119 | return $this; |
| 120 | } |
| 121 | } |