Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.filter.boolean.html

Size:22439
Storage flags:no_autoload,compress/gzip (22%)

Boolean — Zend Framework 2 2.4.2 documentation

Boolean

This filter changes a given input to be a BOOLEAN value. This is often useful when working with databases or when processing form values.

Supported Options

The following options are supported for Zend\Filter\Boolean:

  • casting: When this option is set to TRUE then any given input will be casted to boolean. This option defaults to TRUE.
  • translations: This option sets the translations which will be used to detect localized input.
  • type: The type option sets the boolean type which should be used. Read the following for details.

Default Behavior

By default, this filter works by casting the input to a BOOLEAN value; in other words, it operates in a similar fashion to calling (boolean) $value.

1
2
3
4
$filter = new Zend\Filter\Boolean();
$value  = '';
$result = $filter->filter($value);
// returns false

This means that without providing any configuration, Zend\Filter\Boolean accepts all input types and returns a BOOLEAN just as you would get by type casting to BOOLEAN.

Changing the Default Behavior

Sometimes casting with (boolean) will not suffice. Zend\Filter\Boolean allows you to configure specific types to convert, as well as which to omit.

The following types can be handled:

  • boolean: Returns a boolean value as is.
  • integer: Converts an integer 0 value to FALSE.
  • float: Converts a float 0.0 value to FALSE.
  • string: Converts an empty string ‘’ to FALSE.
  • zero: Converts a string containing the single character zero (‘0’) to FALSE.
  • empty_array: Converts an empty array to FALSE.
  • null: Converts a NULL value to FALSE.
  • php: Converts values according to PHP when casting them to BOOLEAN.
  • false_string: Converts a string containing the word “false” to a boolean FALSE.
  • yes: Converts a localized string which contains the word “no” to FALSE.
  • all: Converts all above types to BOOLEAN.

All other given values will return TRUE by default.

There are several ways to select which of the above types are filtered. You can give one or multiple types and add them, you can give an array, you can use constants, or you can give a textual string. See the following examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// converts 0 to false
$filter = new Zend\Filter\Boolean(Zend\Filter\Boolean::TYPE_INTEGER);

// converts 0 and '0' to false
$filter = new Zend\Filter\Boolean(
    Zend\Filter\Boolean::TYPE_INTEGER + Zend\Filter\Boolean::TYPE_ZERO_STRING
);

// converts 0 and '0' to false
$filter = new Zend\Filter\Boolean(array(
    'type' => array(
        Zend\Filter\Boolean::TYPE_INTEGER,
        Zend\Filter\Boolean::TYPE_ZERO_STRING,
    ),
));

// converts 0 and '0' to false
$filter = new Zend\Filter\Boolean(array(
    'type' => array(
        'integer',
        'zero',
    ),
));

You can also give an instance of Zend\Config\Config to set the desired types. To set types after instantiation, use the setType() method.

Localized Booleans

As mentioned previously, Zend\Filter\Boolean can also recognise localized “yes” and “no” strings. This means that you can ask your customer in a form for “yes” or “no” within his native language and Zend\Filter\Boolean will convert the response to the appropriate boolean value.

To set the translation and the corresponding value, you can use the translations option or the method setTranslations.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$filter = new Zend\Filter\Boolean(array(
    'type'         => Zend\Filter\Boolean::TYPE_LOCALIZED,
    'translations' => array(
        'ja'   => true,
        'nein' => false,
        'yes'  => true,
        'no'   => false,
    ),
));

// returns false
$result = $filter->filter('nein');

// returns true
$result = $filter->filter('yes');

Disable Casting

Sometimes it is necessary to recognise only TRUE or FALSE and return all other values without changes. Zend\Filter\Boolean allows you to do this by setting the casting option to FALSE.

In this case Zend\Filter\Boolean will work as described in the following table, which shows which values return TRUE or FALSE. All other given values are returned without change when casting is set to FALSE

Usage without casting
Type True False
Zend\Filter\Boolean::TYPE_BOOLEAN TRUE FALSE
Zend\Filter\Boolean::TYPE_EMPTY_ARRAY array()  
Zend\Filter\Boolean::TYPE_FALSE_STRING “false” (case independently) “true” (case independently)
Zend\Filter\Boolean::TYPE_FLOAT 0.0 1.0
Zend\Filter\Boolean::TYPE_INTEGER 0 1
Zend\Filter\Boolean::TYPE_LOCALIZED localized “yes” (case independently) localized “no” (case independently)
Zend\Filter\Boolean::TYPE_NULL NULL  
Zend\Filter\Boolean::TYPE_STRING “”  
Zend\Filter\Boolean::TYPE_ZERO_STRING “0” “1”

The following example shows the behaviour when changing the casting option:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$filter = new Zend\Filter\Boolean(array(
    'type'    => Zend\Filter\Boolean::TYPE_ALL,
    'casting' => false,
));

// returns false
$result = $filter->filter(0);

// returns true
$result = $filter->filter(1);

// returns the value
$result = $filter->filter(2);

Table Of Contents

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Boolean on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net