DifferentialRevisionPHIDType.php 1.71 KB
Newer Older
1
2
<?php

Joshua Spence's avatar
Joshua Spence committed
3
final class DifferentialRevisionPHIDType extends PhabricatorPHIDType {
4
5
6
7

  const TYPECONST = 'DREV';

  public function getTypeName() {
Chad Little's avatar
Chad Little committed
8
    return pht('Differential Revision');
9
10
11
12
13
14
  }

  public function newObject() {
    return new DifferentialRevision();
  }

15
16
17
18
  public function getPHIDTypeApplicationClass() {
    return 'PhabricatorDifferentialApplication';
  }

19
  protected function buildQueryForObjects(
20
21
22
23
    PhabricatorObjectQuery $query,
    array $phids) {

    return id(new DifferentialRevisionQuery())
24
      ->withPHIDs($phids);
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  }

  public function loadHandles(
    PhabricatorHandleQuery $query,
    array $handles,
    array $objects) {

    foreach ($handles as $phid => $handle) {
      $revision = $objects[$phid];

      $title = $revision->getTitle();
      $id = $revision->getID();
      $status = $revision->getStatus();

39
      $handle->setName("D{$id}");
40
41
42
      $handle->setURI("/D{$id}");
      $handle->setFullName("D{$id}: {$title}");

43
      if ($revision->isClosed()) {
44
        $handle->setStatus(PhabricatorObjectHandle::STATUS_CLOSED);
45
46
47
48
49
      }
    }
  }

  public function canLoadNamedObject($name) {
50
    return preg_match('/^D[1-9]\d*$/i', $name);
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
  }

  public function loadNamedObjects(
    PhabricatorObjectQuery $query,
    array $names) {

    $id_map = array();
    foreach ($names as $name) {
      $id = (int)substr($name, 1);
      $id_map[$id][] = $name;
    }

    $objects = id(new DifferentialRevisionQuery())
      ->setViewer($query->getViewer())
      ->withIDs(array_keys($id_map))
      ->execute();

    $results = array();
    foreach ($objects as $id => $object) {
      foreach (idx($id_map, $id, array()) as $name) {
        $results[$name] = $object;
      }
    }

    return $results;
  }

}