Menu

Having every bit of information defined at every aspect of application makes system actually finished.
Piotr Masełkowski, Maslosoft Founder

Introducing polite PHP in error handler

Due to issues of handling exception in __toString PHP's method, the error handler can be invoked in such methods to catch exception and display nice stack trace. However using error handling in __toString methods need special care, thus very polite syntax have been introduced.

Example code to handle exceptions

public function __toString()
{
    try
    {
        // Do some stuff throwing exception
        return 'my result';
    }
    catch (Exception $exception)
    {
        ErrorHandler::please()->takeCareOf($this, $exception);
    }
}

This is actual working code in components project:)

Mangan Unique Validator with extra criteria

When developing web applications human or robot input validation is crucial for data integrity. It is possible to set database constraints to keep data consistent - and it should be done - however to display informative message we use validators. These analyze situation and try to display useful information. The unique validator for instance prevents inserting duplicated elements into database.

Use case for extra criteria parameter

The case I stumbled upon was to validate domain added by anyone from around the internet and allow to validate this domain. The point is that anyone can add any domain, and it is perfectly fine. However only one domain having same host can be verified. This use case is supported by Mangan Unique Validator. It allows to set additional criteria, which will be merged with uniqueness checking parameters.

The parameters for property criteria is same as for Criteria class constructor. Which is rather deep array, but can be written in many lines.

Example criteria parameters on unique validator annotation

/**
 * @UniqueValidator(
* 'criteria' => [
* 'conditions' => [
* 'verified' => ['==' => true]
* ]
* ],
* 'message' => @Label('Domain {value} is already verified by another account')
* ) */ public $host = ''; public $verified = false;

Trashing domain

Additional criteria with verified attribute check is sufficient to prevent multiple hosts to be verified. When one of visitor verifies domain, the others have message displayed informing that the domain is verified by someone else. The first thing to do is to move domain to trash, as it is no longer usable. But the Unique Validator will prevent this, because the domain is already verified.

There is additional feature of Mangan, called scenarios. These allow different behaviors depending on what is going to happen with model. In our case we want to allow trashing domain even if it fails uniqueness constraint. Adding trash scenario to except property solves this issue.

Example of using except to allow trashing

/**
 * @UniqueValidator(
 *     'except' => ['trash'],
* 'criteria' => [
* 'conditions' => [
* 'verified' => ['==' => true]
* ]
* ],
* 'message' => @Label('Domain {value} is already verified by another account')
* ) */ public $host = ''; public $verified = false;

To avoid using magic string the constant TrashInterface::ScenarioTrash can be used.

Annotations JSON Support

Introducing JSON support for annotations. This allows annotating objects with JSON configuration without need to convert it to PHP array syntax. Additionally new syntax can be mixed with old one. Adding support for JSON is backwards compatible, as in previous version such syntax would throw exception.

The idea came from the MongoDB documentation, where document indexes are defined with JSON. So that syntax known by MongoDB users could be used in Mangan.

Example of annotation with JSON syntax

@Index({score: 1})

Notice the unquoted key. It is possible to use unquoted values, but risky to some extend.

Defining getter and setter in CoffeeScript

JavaScript allows You to define getter and setter for property. So that this property can compute value and return it. This can be used to sanitize value or to return value based

on other object values. This can be done with Object.defineProperty function. It takes object as a first argument, property name as a second argument - defined as string. The third argument is object defining get and set methods, as well as indicating whether property should be enumerable. Enumerable property will be for example included in loops.

When defining in class constructor, ensure that the parent constructor will be called using super keyword.

Example of getter and setter

class User extends Model  

firstName: '' lastName: '' fullName: ''
constructor: (data) ->
Object.defineProperty @, 'fullName', {
get: () =>
return "#{@firstName} #{@lastName}"
enumerable: true,
set: (value) -> # Ignore
}


super(data)

In the example above, accessing fullName property will concatenate firstName and lastName.

Usage of example User class

user = new User
user.firstName = 'John'
user.lastName = 'Smith'
console.log user.fullName

This will output John Smith to browsers console.

Dark Website Background

To improve work experience, many of the developers use dark themes for their favorite IDE. Also many websites featured dark themes, often optional.

Going with the trend and for better visitors experience the layout of Maslosoft website got revamped into dark one. Currently in testing, but seems that it looks much more slick! Thanks to less variables it turned out to be trivial task to turn light layout into dark one. Possibly project icons colors will need to be adjusted.