Overview

Namespaces

  • Apptus
    • ESales
      • Connector
        • Report
        • Time
    • Util
      • Cache
  • PHP
  • Overview
  • Namespace
  • Class
  • Tree
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 
<?php
namespace Apptus\ESales\Connector;

/**
 * Simple validation rules for eSales identifiers.
 */
final class AttributeValidation {
    /**
     * @internal
     */
    const ILLEGAL_KEY_CHARACTERS = ", \r\n\t";

    private static $products = null;
    private static $ads = null;

    private $reservedNames;

    // prevent instantiation and sub classing
    private final function __construct() {
        $this->reservedNames = array_flip(func_get_args());
    }

    /**
     * Get an AttributeValidation instance that can be used to check product attribute names and keys.
     *
     * @return AttributeValidation
     */
    public static function forProducts() {
        if (self::$products === null) {
            self::$products = new AttributeValidation('relevance', 'sales', 'rank', 'locale', 'abandonment_timestamp', 'viewed_timestamp', 'proportion', 'rating', 'clicks');
        }
        return self::$products;
    }

    /**
     * Get an AttributeValidation instance that can be used to check variant attribute names and keys.
     *
     * @return AttributeValidation
     */
    public static function forVariants() {
        return self::forProducts();
    }

    /**
     * Get an AttributeValidation instance that can be used to check ad attribute names and keys.
     *
     * @return AttributeValidation
     */
    public static function forAds() {
        if (self::$ads === null) {
            self::$ads = new AttributeValidation('rank', 'locale', 'products', 'start_time', 'end_time', 'related', 'included');
        }
        return self::$ads;
    }

    /**
     *
     * @param string
     * @return boolean
     */
    public function validAttributeName($name) {
        return $this->validAttributeNameHelper($name, '/^[a-zA-Z_][a-zA-Z0-9_\-]*$/');
    }

    /**
     *
     * @param string
     * @return boolean
     */
    public function validSortAttributeName($name) {
        return $this->validAttributeNameHelper($name, '/^[a-zA-Z_][a-zA-Z0-9_]*$/');
    }

    private function validAttributeNameHelper($name, $pattern) {
        if ($name === null || !is_string($name)) {
            return false;
        }
        if (isset($this->reservedNames[$name])) {
            return false;
        }
        if (!preg_match($pattern, $name)) {
            return false;
        }
        return true;
    }

    /**
     *
     * @param string
     * @return boolean
     */
    public function validItemKey($itemKey) {
        return strpbrk($itemKey, self::ILLEGAL_KEY_CHARACTERS) === false;
    }
}
Apptus ESales Connector PHP API documentation generated by ApiGen