2
0

Zend_Validate-InArray.xml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 16394 -->
  3. <!-- Reviewed: no -->
  4. <sect2 id="zend.validate.set.in_array">
  5. <title>InArray</title>
  6. <para>
  7. <classname>Zend_Validate_InArray</classname> erlaubt es zu prüfen ob ein gegebener Wert
  8. in einem Array enthalten ist. Er ist auch in der Lage mehrdimensionale Arrays zu prüfen.
  9. </para>
  10. <sect3 id="zend.validate.set.in_array.basic">
  11. <title>Einfache Array Prüfung</title>
  12. <para>
  13. Der einfachste Weg ist es, das Array welches durchsucht werden soll, bei der
  14. Initiierung anzugeben:
  15. </para>
  16. <programlisting language="php"><![CDATA[
  17. $validator = new Zend_Validate_InArray(array('key' => 'value', 'otherkey' => 'othervalue'));
  18. if ($validator->isValid('value')) {
  19. // Wert gefunden
  20. } else {
  21. // Wert nicht gefunden
  22. }
  23. ]]></programlisting>
  24. <para>
  25. Das verhält sich genauso wie <acronym>PHP</acronym>'s
  26. <methodname>in_array()</methodname> Methode.
  27. </para>
  28. <note>
  29. <para>
  30. Standardmäßig ist diese Prüfung nicht strikt noch kann Sie mehrdimensionale Arrays
  31. prüfen.
  32. </para>
  33. </note>
  34. <para>
  35. Natürlich kann man das Array gegen das geprüft werden soll auch im Nachhinein durch
  36. Verwendung der <methodname>setHaystack()</methodname> Methode angegeben werden.
  37. </para>
  38. <programlisting language="php"><![CDATA[
  39. $validator = new Zend_Validate_InArray();
  40. $validator->setHaystack(array('key' => 'value', 'otherkey' => 'othervalue'));
  41. if ($validator->isValid('value')) {
  42. // Wert gefunden
  43. } else {
  44. // Wert nicht gefunden
  45. }
  46. ]]></programlisting>
  47. </sect3>
  48. <sect3 id="zend.validate.set.in_array.strict">
  49. <title>Strikte Array Prüfung</title>
  50. <para>
  51. Wie vorher erwähnt kann man auch eine Strikte Prüfung im Array durchführen.
  52. Standardmäßig würde kein Unterschied zwischen dem Integerwert <emphasis>0</emphasis>
  53. und dem String <emphasis>"0"</emphasis> sein. Wenn eine strikte Prüfung durchgeführt
  54. wird dann wird dieser Unterschied auch geprüft und nur gleiche Typen werden akzeptiert.
  55. </para>
  56. <para>
  57. Eine strikte Prüfung kann auch auf zwei verschiedenen Wegen durchgeführt werden. Bei
  58. der Initiierung und durch Verwendung einer Methode. Bei der Initiierung muß hierfür ein
  59. Array mit der folgenden Struktur angegeben werden:
  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. // Wert gefunden
  70. } else {
  71. // Wert nicht gefunden
  72. }
  73. ]]></programlisting>
  74. <para>
  75. The <emphasis>haystack</emphasis> key contains your array to validate against, and by
  76. setting the <emphasis>script</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.
  82. </para>
  83. <note>
  84. <para>
  85. Note that the <emphasis>strict</emphasis> setting is per default
  86. <constant>FALSE</constant>.
  87. </para>
  88. </note>
  89. </sect3>
  90. <sect3 id="zend.validate.set.in_array.recursive">
  91. <title>Recursive array validation</title>
  92. <para>
  93. In addition to <acronym>PHP</acronym>'s <methodname>in_array()</methodname> method
  94. this validator can also be used to validate multidimensional arrays.
  95. </para>
  96. <para>
  97. To validate multidimensional arrays you have to set the <emphasis>recursive</emphasis>
  98. option.
  99. </para>
  100. <programlisting language="php"><![CDATA[
  101. $validator = new Zend_Validate_InArray(
  102. array(
  103. 'haystack' => array(
  104. 'firstDimension' => array('key' => 'value', 'otherkey' => 'othervalue'),
  105. 'secondDimension' => array('some' => 'real', 'different' => 'key')),
  106. 'recursive' => true
  107. )
  108. );
  109. if ($validator->isValid('value')) {
  110. // value found
  111. } else {
  112. // no value found
  113. }
  114. ]]></programlisting>
  115. <para>
  116. Your array will then be validated recursive to see if the given value is contained.
  117. </para>
  118. <note>
  119. <para>
  120. Note that per default the recursive validation is turned off.
  121. </para>
  122. </note>
  123. </sect3>
  124. </sect2>
  125. <!--
  126. vim:se ts=4 sw=4 et:
  127. -->