blob: 2f611129727db313b4797f85d7348d79f10993c9 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Models\Attributes;
4
5class 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 Benkard1ba53812022-12-27 17:32:58 +010037 $this->value = (string) $value;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020038 $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 Benkard1ba53812022-12-27 17:32:58 +010063 * Get the raw (unescaped) value.
64 *
65 * @return mixed
66 */
67 public function raw()
68 {
69 return $this->value;
70 }
71
72 /**
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020073 * 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}