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