Zend_Validate-InArray.xml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.in_array">
  4. <title>InArray</title>
  5. <para>
  6. <classname>Zend_Validate_InArray</classname> allows you to validate if a given value is
  7. contained within an array. It is also able to validate multidimensional arrays.
  8. </para>
  9. <sect3 id="zend.validate.set.in_array.options">
  10. <title>Supported options for Zend_Validate_InArray</title>
  11. <para>
  12. The following options are supported for <classname>Zend_Validate_InArray</classname>:
  13. </para>
  14. <itemizedlist>
  15. <listitem>
  16. <para>
  17. <emphasis><property>haystack</property></emphasis>: Sets the haystack for the
  18. validation.
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. <emphasis><property>recursive</property></emphasis>: Defines if the validation
  24. should be done recursive. This option defaults to <constant>FALSE</constant>.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <emphasis><property>strict</property></emphasis>: Defines if the validation
  30. should be done strict. This option defaults to <constant>FALSE</constant>.
  31. </para>
  32. </listitem>
  33. </itemizedlist>
  34. </sect3>
  35. <sect3 id="zend.validate.set.in_array.basic">
  36. <title>Simple array validation</title>
  37. <para>
  38. The simplest way, is just to give the array which should be searched against at
  39. initiation:
  40. </para>
  41. <programlisting language="php"><![CDATA[
  42. $validator = new Zend_Validate_InArray(array('key' => 'value',
  43. 'otherkey' => 'othervalue'));
  44. if ($validator->isValid('value')) {
  45. // value found
  46. } else {
  47. // no value found
  48. }
  49. ]]></programlisting>
  50. <para>
  51. This will behave exactly like <acronym>PHP</acronym>'s
  52. <methodname>in_array()</methodname> method.
  53. </para>
  54. <note>
  55. <para>
  56. Per default this validation is not strict nor can it validate multidimensional
  57. arrays.
  58. </para>
  59. </note>
  60. <para>
  61. Of course you can give the array to validate against also afterwards by using the
  62. <methodname>setHaystack()</methodname> method. <methodname>getHaystack()</methodname>
  63. returns the actual set haystack array.
  64. </para>
  65. <programlisting language="php"><![CDATA[
  66. $validator = new Zend_Validate_InArray();
  67. $validator->setHaystack(array('key' => 'value', 'otherkey' => 'othervalue'));
  68. if ($validator->isValid('value')) {
  69. // value found
  70. } else {
  71. // no value found
  72. }
  73. ]]></programlisting>
  74. </sect3>
  75. <sect3 id="zend.validate.set.in_array.strict">
  76. <title>Strict array validation</title>
  77. <para>
  78. As mentioned before you can also do a strict validation within the array. Per default
  79. there would be no difference between the integer value <emphasis>0</emphasis> and the
  80. string <emphasis>"0"</emphasis>. When doing a strict validation this difference will
  81. also be validated and only same types are accepted.
  82. </para>
  83. <para>
  84. A strict validation can also be done by using two different ways. At initiation and by
  85. using a method. At initiation you have to give an array with the following structure:
  86. </para>
  87. <programlisting language="php"><![CDATA[
  88. $validator = new Zend_Validate_InArray(
  89. array(
  90. 'haystack' => array('key' => 'value', 'otherkey' => 'othervalue'),
  91. 'strict' => true
  92. )
  93. );
  94. if ($validator->isValid('value')) {
  95. // value found
  96. } else {
  97. // no value found
  98. }
  99. ]]></programlisting>
  100. <para>
  101. The <emphasis>haystack</emphasis> key contains your array to validate against. And by
  102. setting the <emphasis>strict</emphasis> key to <constant>TRUE</constant>, the validation
  103. is done by using a strict type check.
  104. </para>
  105. <para>
  106. Of course you can also use the <methodname>setStrict()</methodname> method to change
  107. this setting afterwards and <methodname>getStrict()</methodname> to get the actual set
  108. state.
  109. </para>
  110. <note>
  111. <para>
  112. Note that the <emphasis>strict</emphasis> setting is per default
  113. <constant>FALSE</constant>.
  114. </para>
  115. </note>
  116. </sect3>
  117. <sect3 id="zend.validate.set.in_array.recursive">
  118. <title>Recursive array validation</title>
  119. <para>
  120. In addition to <acronym>PHP</acronym>'s <methodname>in_array()</methodname> method
  121. this validator can also be used to validate multidimensional arrays.
  122. </para>
  123. <para>
  124. To validate multidimensional arrays you have to set the <emphasis>recursive</emphasis>
  125. option.
  126. </para>
  127. <programlisting language="php"><![CDATA[
  128. $validator = new Zend_Validate_InArray(
  129. array(
  130. 'haystack' => array(
  131. 'firstDimension' => array('key' => 'value',
  132. 'otherkey' => 'othervalue'),
  133. 'secondDimension' => array('some' => 'real',
  134. 'different' => 'key')),
  135. 'recursive' => true
  136. )
  137. );
  138. if ($validator->isValid('value')) {
  139. // value found
  140. } else {
  141. // no value found
  142. }
  143. ]]></programlisting>
  144. <para>
  145. Your array will then be validated recursive to see if the given value is contained.
  146. Additionally you could use <methodname>setRecursive()</methodname> to set this option
  147. afterwards and <methodname>getRecursive()</methodname> to retrieve it.
  148. </para>
  149. <programlisting language="php"><![CDATA[
  150. $validator = new Zend_Validate_InArray(
  151. array(
  152. 'firstDimension' => array('key' => 'value',
  153. 'otherkey' => 'othervalue'),
  154. 'secondDimension' => array('some' => 'real',
  155. 'different' => 'key')
  156. )
  157. );
  158. $validator->setRecursive(true);
  159. if ($validator->isValid('value')) {
  160. // value found
  161. } else {
  162. // no value found
  163. }
  164. ]]></programlisting>
  165. <note>
  166. <title>Default setting for recursion</title>
  167. <para>
  168. Per default the recursive validation is turned off.
  169. </para>
  170. </note>
  171. <note>
  172. <title>Option keys within the haystack</title>
  173. <para>
  174. When you are using the keys '<property>haystack</property>',
  175. '<property>strict</property>' or '<property>recursive</property>' within your
  176. haystack, then you must wrap the <property>haystack</property> key.
  177. </para>
  178. </note>
  179. </sect3>
  180. </sect2>
  181. <!--
  182. vim:se ts=4 sw=4 et:
  183. -->