Creating a custom input capture type

Creating a custom input catcher is very easy. Basically, you have two options: extending / modifying / overriding one of the existing catchers, or creating a completely custom catcher from scratch – a brand new input catcher type. Let's take a look at how to do it.

Extending / Modifying an Existing Input Catcher

The principle is simple: using inheritance, you modify the original class and assign it to the given type using the factory system. Here is an example:

namespace JetApplication;

use 
Jet\InputCatcher_Int;

class 
MyIntCatcher extends InputCatcher_Int {
    protected function 
checkValue() : void
    
{
        if(
$this->value!=='') {
            
$this->value = (int)str_replace(' ', '', $this->value_raw);
        } else {
            
$this->value = null;
        }
    }
}

And then all you need to do is replace the old catcher with the new one. This means adding the following line of code to the initialization script application/Init/Factory.php:

use Jet\Factory_InputCatcher;

Factory_InputCatcher::setInputCatcherClassName( MyIntCatcher::getType(), MyIntCatcher::class );

Creating a Completely Custom Input Catcher

Creating your own input catcher follows the exact same principle, with the difference that your class will inherit directly from the InputCatcher class and must implement more methods. At the same time, we will use this example to show how to define attributes for an input catcher definition.

use Jet\InputCatcher;
use 
Jet\InputCatcher_Definition_InputCatcherOption;

class 
MyNumberCatcher extends InputCatcher {
    
    #[
InputCatcher_Definition_InputCatcherOption(
        
type: InputCatcher_Definition_InputCatcherOption::TYPE_BOOL,
        
getter: 'getRemoveSpaces',
        
setter: 'setRemoveSpaces'
    
)]
    protected 
bool $remove_spaces = false;
    
    public function 
getRemoveSpaces(): bool
    
{
        return 
$this->remove_spaces;
    }
    
    public function 
setRemoveSpaces( bool $remove_spaces ): void
    
{
        
$this->remove_spaces = $remove_spaces;
    }
    
    
    
    protected function 
checkValue() : void
    
{
        if(
$this->value!=='') {
            if(
$this->remove_spaces) {
                
$this->value = (int)str_replace(' ', '', $this->value_raw);
            } else {
                
$this->value = (int)$this->value_raw;
                
            }
        } else {
            
$this->value = null;
        }
    }
    
    public function 
getValue() : ?int
    
{
        return 
$this->value;
    }
}

Please note the use of InputCatcher_Definition_InputCatcherOption. This is how attributes are defined for input catchers. The InputCatcher_Definition_InputCatcherOption class takes care of the complete management of the catcher option; you simply define the attribute like this.

And all that remains is to register the new catcher. This again means adding the following line of code to the initialization script application/Init/Factory.php:

use Jet\Factory_Validator;

Factory_InputCatcher::registerNewInputCatcherType( MyNumberCatcher::getType(), MyNumberCatcher::class );
Previous chapter
Jet\InputCatcher_Strings
Next chapter
Jet\Entity_InputCatcher_Definition_InputCatcherOption