## Apache Zeta Components Manual :: File Source for question_dialog_regex.php

## Source for file question_dialog_regex.php

Documentation is available at question_dialog_regex.php

1. <?php
2. /**
3. * File containing the ezcConsoleQuestionDialogRegexValidator class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package ConsoleTools
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * Regex validator for ezcConsoleQuestionDialog
30. * Validator class for ezcConsoleQuestionDialog objects that validates by
31. * matching a certain regular expression.
32. *
33. * @package ConsoleTools
34. * @version //autogen//
35. *
36. * @property string $pattern
37. * The pattern to use for validation. Delimiters and modifiers
38. * included.
39. * @property mixed $default
40. * A default value if no (an empty string) result given.
41. */
42. class ezcConsoleQuestionDialogRegexValidator implements ezcConsoleQuestionDialogValidator
43. {
45. /**
46. * Properties
47. *
48. * @var array(string=>mixed)
49. */
50. protected $properties = array(
51. "pattern" => null,
52. "default" => null,
53. );
55. /**
56. * Create a new question dialog regex validator.
57. * Create a new question dialog regex validator, which validates the result
58. * specified against a given regular expression. The delimiters and
59. * eventual modifiers must be included in the pattern. If not value is
60. * provided by the user a possibly set $default value is used instead.
61. *
62. *
63. * @param string $pattern Pattern to validate against.
64. * @param mixed $default Default value.
65. * @return void
66. */
67. public function __construct ( $pattern, $default = null )
68. {
69. $this->pattern = $pattern;
70. $this->default = $default;
71. }
73. /**
74. * Returns if the given result is valid.
75. * Returns if the result matches the regular expression.
76. *
77. * @param mixed $result The received result.
78. * @return bool If the result is valid.
79. */
80. public function validate ( $result )
81. {
82. if ( $result === "" )
83. {
84. return $this->default !== null;
85. }
86. return preg_match ( $this->pattern, $result ) > 0;
87. }
89. /**
90. * Returns a fixed version of the result, if possible.
91. * If no result was provided by the user, the default value will be
92. * returned, if set.
93. *
94. * @param mixed $result The received result.
95. * @return mixed The manipulated result.
96. */
97. public function fixup ( $result )
98. {
99. if ( $result === "" && $this->default !== null )
100. {
101. return $this->default;
102. }
103. return $result;
104. }
106. /**
107. * Returns a string representing valid results.
108. * Returns the string that will be displayed with the question to
109. * indicate valid results to the user and a possibly set default, if
110. * available.
111. *
112. * @return string
113. */
114. public function getResultString ()
115. {
116. return "(match {$this->pattern})

" . ( $this->default !== null ? " [{$this->default}]" : "" );
- }
- /**
- * Property read access.
- *
- * @param string $propertyName Name of the property.
- * @return mixed Value of the property or null.
- *
- * @throws ezcBasePropertyNotFoundException
- * If the the desired property is not found.
- * @ignore
- */
- public function __get( $propertyName )
- {
- if ( $this->__isset( $propertyName ) )
- {
- return $this-> properties [$propertyName];
- }
- throw new ezcBasePropertyNotFoundException( $propertyName );
- }
- /**
- * Property write access.
- *
- * @param string $propertyName Name of the property.
- * @param mixed $propertyValue The value for the property.
- *
- * @throws ezcBasePropertyNotFoundException
- * If a the value for the property options is not an instance of
- * @throws ezcBaseValueException
- * If a the value for a property is out of range.
- * @ignore
- */
- public function __set( $propertyName, $propertyValue )
- {
- switch ( $propertyName )
- {
- case "pattern":
- if ( is_string( $propertyValue ) === false || strlen( $propertyValue ) < 2 )
- {
- throw new ezcBaseValueException( $propertyName, $propertyValue, "string, length > 1" );
- }
- break;
- case "default":
- if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
- {
- throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
- }
- break;
- default:
- throw new ezcBasePropertyNotFoundException( $propertyName );
- }
- $this-> properties [$propertyName] = $propertyValue;
- }
- /**
- * Property isset access.
- *
- * @param string $propertyName Name of the property.
- * @return bool True is the property is set, otherwise false.
- * @ignore
- */
- public function __isset( $propertyName )
- {
- return array_key_exists( $propertyName, $this-> properties );
- }
- }
Documentation generated by phpDocumentor 1.4.3