<?php

final class ExtensionStoreExtensionPhutilLibNameTransaction
  extends ExtensionStoreExtensionTransactionType {

  const TRANSACTIONTYPE = 'est:extension:phutilname';

  public function generateOldValue($object) {
    return $object->getExtensionKey();
  }

  public function applyInternalEffects($object, $value) {
    $object->setPhutilLibName($value);
  }

  public function validateTransactions($object, array $xactions) {
    $errors = array();

    if ($this->isEmptyTextTransaction($object->getName(), $xactions)) {
      $errors[] = $this->newRequiredError(
        pht('Extensions must have a Phutil library name.'));
    }

    if (!$this->isNewObject()) {
      foreach ($xactions as $xaction) {
        $errors[] = $this->newInvalidError(
          pht('Library name can not be changed.'),
          $xaction);
      }
    }

    foreach ($xactions as $xaction) {
      $value = $xaction->getNewValue();
      if (!preg_match('/^[a-z-]+$/', $value)) {
        $errors[] = $this->newInvalidError(
          pht(
            'Phutil-Library name may contain only '.
            'lowercase letters and hyphens.'),
          $xaction);
      }
    }

    return $errors;
  }



}
