<?php
namespace models;

use \ActiveRecord;
use \IoC;

class Page extends ActiveRecord {

    protected function validate() {
        $i18n = IoC::resolve( 'i18n' );
        $this->validateMaxLength( 'name', 100, $i18n->__( 'too_long_name', 100 ) );
        $this->validateNotEmpty( 'name', $i18n->__( 'name_cannot_be_empty' ) );
        $this->validateRegExpr( 'name', '/([A-Z][a-zà-ÿ0-9]+)/', $i18n->__( 'invalid_name' ) );
        if ( $this->isNewRecord() || $this->isAttributeChanged( 'name' ) ) {
            $this->validateUnique( 'name', $i18n->__( 'page_already_exists', $this->name ) );
        }
    }

    protected function beforeValidate() {
        $this->name = trim( $this->name );
    }

}
