PrimitiveString.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2008 by Konstantin V. Arkhipov, Sveta A. Smirnova  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     class PrimitiveString extends FiltrablePrimitive
00016     {
00017         // TODO: consider making a primitive based on main::Net::Mail::MailAddress
00018         const MAIL_PATTERN  = '/^[a-zA-Z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[a-zA-Z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+)*@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/Ds';
00019         const URL_PATTERN   = '/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}((:[0-9]{1,5})?\/.*)?$/is';
00020         const SHA1_PATTERN  = '/^[0-9a-f]{40}$/';
00021         const MD5_PATTERN   = '/^[0-9a-f]{32}$/';
00022         
00023         protected $pattern = null;
00024         
00028         public function setAllowedPattern($pattern)
00029         {
00030             $this->pattern = $pattern;
00031             
00032             return $this;
00033         }
00034         
00035         public function import($scope)
00036         {
00037             if (!BasePrimitive::import($scope))
00038                 return null;
00039             
00040             $this->value = (string) $scope[$this->name];
00041             
00042             $this->selfFilter();
00043             
00044             if (
00045                 is_string($this->value)
00046                 // zero is quite special value here
00047                 && ($this->value === '0' || !empty($this->value))
00048                 && ($length = mb_strlen($this->value))
00049                 && !($this->max && $length > $this->max)
00050                 && !($this->min && $length < $this->min)
00051                 && (!$this->pattern || preg_match($this->pattern, $this->value))
00052             ) {
00053                 return true;
00054             } else {
00055                 $this->value = null;
00056             }
00057             
00058             return false;
00059         }
00060     }
00061 ?>