You are here

class SFID in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 src/SFID.php \Drupal\salesforce\SFID
  2. 5.0.x src/SFID.php \Drupal\salesforce\SFID

Class SFID.

@package Drupal\salesforce

Hierarchy

  • class \Drupal\salesforce\SFID

Expanded class hierarchy of SFID

22 files declare their use of SFID
DeleteHandler.php in modules/salesforce_pull/src/DeleteHandler.php
MappedObject.php in modules/salesforce_mapping/src/Entity/MappedObject.php
MappedObjectForm.php in modules/salesforce_mapping_ui/src/Form/MappedObjectForm.php
MappedObjectStorage.php in modules/salesforce_mapping/src/MappedObjectStorage.php
MappedObjectTest.php in modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php

... See full list

File

src/SFID.php, line 10

Namespace

Drupal\salesforce
View source
class SFID {

  /**
   * The id string value.
   *
   * @var string
   */
  protected $id;
  const MAX_LENGTH = 18;

  /**
   * SFID constructor.
   *
   * @param string $id
   *   The SFID.
   *
   * @throws \Exception
   */
  public function __construct($id) {
    if (strlen($id) != 15 && strlen($id) != self::MAX_LENGTH) {
      throw new \Exception('Invalid sfid ' . strlen($id));
    }
    $this->id = $id;
    if (strlen($this->id) == 15) {
      $this->id = static::convertId($id);
    }
  }

  /**
   * Given a potential SFID, return a new SFID object if it's valid.
   *
   * @param string $id
   *   A potential sfid.
   *
   * @return \Drupal\salesforce\SFID|false
   *   A new SFID if $id is valid, otherwise FALSE.
   */
  public static function createIfValid($id) {
    if (static::isValid($id)) {
      return new static($id);
    }
    return FALSE;
  }

  /**
   * Returns TRUE if $id is a valid length.
   *
   * @param string $id
   *   An sfid.
   *
   * @return bool
   *   Returns TRUE if $id is a valid length.
   */
  public static function isValid($id) {
    return strlen($id) == 15 || strlen($id) == self::MAX_LENGTH;
  }

  /**
   * Magic method wrapping the SFID string.
   *
   * @return string
   *   The SFID.
   */
  public function __toString() {
    return (string) $this->id;
  }

  /**
   * Convert 15-character Salesforce ID to an 18-character ID.
   *
   * Converts a 15-character case-sensitive Salesforce ID to 18-character
   * case-insensitive ID. If input is not 15-characters, return input unaltered.
   *
   * @param string $sfid15
   *   15-character case-sensitive Salesforce ID.
   *
   * @return string
   *   18-character case-insensitive Salesforce ID
   */
  private static function convertId($sfid15) {
    $chunks = str_split($sfid15, 5);
    $extra = '';
    foreach ($chunks as $chunk) {
      $chars = str_split($chunk, 1);
      $bits = '';
      foreach ($chars as $char) {
        $bits .= !is_numeric($char) && $char == strtoupper($char) ? '1' : '0';
      }
      $map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';
      $extra .= substr($map, base_convert(strrev($bits), 2, 10), 1);
    }
    return $sfid15 . $extra;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SFID::$id protected property The id string value.
SFID::convertId private static function Convert 15-character Salesforce ID to an 18-character ID.
SFID::createIfValid public static function Given a potential SFID, return a new SFID object if it's valid.
SFID::isValid public static function Returns TRUE if $id is a valid length.
SFID::MAX_LENGTH constant
SFID::__construct public function SFID constructor.
SFID::__toString public function Magic method wrapping the SFID string.