blob: cc04a67dde62f17d14bdb807f9ef3c9d5a1a3780 [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 {
37 $this->value = $value;
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 /**
63 * Set the characters to exclude from being escaped.
64 *
65 * @param string $characters
66 *
67 * @return $this
68 */
69 public function ignore($characters)
70 {
71 $this->ignore = $characters;
72
73 return $this;
74 }
75
76 /**
77 * Prepare the value to be escaped for use in a distinguished name.
78 *
79 * @return $this
80 */
81 public function dn()
82 {
83 $this->flags = LDAP_ESCAPE_DN;
84
85 return $this;
86 }
87
88 /**
89 * Prepare the value to be escaped for use in a filter.
90 *
91 * @return $this
92 */
93 public function filter()
94 {
95 $this->flags = LDAP_ESCAPE_FILTER;
96
97 return $this;
98 }
99
100 /**
101 * Prepare the value to be escaped for use in a distinguished name and filter.
102 *
103 * @return $this
104 */
105 public function both()
106 {
107 $this->flags = LDAP_ESCAPE_FILTER + LDAP_ESCAPE_DN;
108
109 return $this;
110 }
111}