Zend_Validate-InArray.xml 5.8 KB

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