Search code examples
phpzend-frameworkfilterzend-framework2php-7

Error calling Zend Filter ToInt with PHP 7


I have the following Model class:

<?php
namespace Tropa\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;

class Setor
{
    public $codigo;
    public $nome;
    protected $inputFilter;

    public function exchangeArray($data)
    {
        $this->codigo = (isset($data['codigo'])) ? $data['codigo'] : null;
        $this->nome = (isset($data['nome'])) ? $data['nome'] : null;
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();
            $factory = new InputFactory();
            $inputFilter->add($factory->createInput(array(
                'name' => 'codigo',
                'required' => false,
                'filters' => array(
                    array('name' => 'ToInt'), ## It was Int, but in PHP 7 it not works anymore, so I replaced it as suggest the zend documentation https://framework.zend.com/apidoc/2.4/classes/Zend.Filter.Int.html
                ),
                'validators' => array(
                    array(
                        'name' => 'Between',
                        'options' => array(
                            'min' => 0,
                            'max' => 3600
                        )
                    ),
                ),
            )));
            $inputFilter->add($factory->createInput(array(
                'name' => 'nome',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 2,
                            'max' => 30,
                        )
                    )
                ),
            )));
            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }
}

When I run this class I get the message:

Zend\Filter\FilterPluginManager::get was unable to fetch or create an instance for ToInt

Anybody knows how to validate Integers now that PHP 7 reserved the word Int?

Added:

After installing the zendframework/zend-filter version 2.7.1 the error message had changed to:

Fatal error: Uncaught TypeError: Argument 1 passed to Zend\ServiceManager\AbstractPluginManager::__construct() must implement interface Zend\ServiceManager\ConfigInterface, instance of Zend\ServiceManager\ServiceManager given, called in C:\xampp\htdocs\corps\vendor\zendframework\zend-filter\src\FilterChain.php on line 112 and defined in C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php:60 Stack trace: #0 C:\xampp\htdocs\corps\vendor\zendframework\zend-filter\src\FilterChain.php(112): Zend\ServiceManager\AbstractPluginManager->__construct(Object(Zend\ServiceManager\ServiceManager)) #1 C:\xampp\htdocs\corps\vendor\zendframework\zend-filter\src\FilterChain.php(183): Zend\Filter\FilterChain->getPluginManager() #2 C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\InputFilter\Factory.php(358): Zend\Filter\FilterChain->attachByName('\Zend\Filter\To...', NULL, 1000) #3 C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\InputFilt in C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 60


Solution

  • It seems you’ve got zend-filter 2.3 installed? I think that you should start upgrading all Zend Framework packages to at least 2.4 version. zend-filter 2.3 doesn’t contain ToInt filter, but 2.4 does. Both versions contain Int filter, but in 2.4 it only serves as a link to ToInt and additionally generates an error message (have a look at Int class source in 2.4).