Debian Codegraph @

vendor/wikimedia/shellbox/src/Command/OutputGlob.php

raw
1 <?php
2
3 namespace Shellbox\Command;
4
5 use Shellbox\Shellbox;
6 use Shellbox\ShellboxError;
7
8 /**
9 * The base class for output file glob patterns
10 */
11 abstract class OutputGlob {
12 use UserDataTrait;
13
14 /** @var string */
15 protected $prefix;
16 /** @var string */
17 protected $extension;
18 /** @var OutputFile[] */
19 protected $files = [];
20
21 /**
22 * @internal
23 * @param string $prefix
24 * @param string $extension
25 */
26 public function __construct( $prefix, $extension ) {
27 $this->prefix = Shellbox::normalizePath( $prefix );
28 Shellbox::checkExtension( $extension );
29 $this->extension = $extension;
30 }
31
32 /**
33 * Get an OutputFile corresponding to a single file that matches the glob.
34 *
35 * @internal
36 * @param string $boxedName
37 * @return OutputFile
38 */
39 abstract public function getOutputFile( $boxedName );
40
41 /**
42 * @return string
43 */
44 public function getPrefix() {
45 return $this->prefix;
46 }
47
48 /**
49 * @return string
50 */
51 public function getExtension() {
52 return $this->extension;
53 }
54
55 /**
56 * @internal
57 * @return string
58 */
59 public function getId() {
60 return $this->prefix . '.' . $this->extension;
61 }
62
63 /**
64 * Get JSON serializable data for client/server communication
65 *
66 * @internal
67 * @return array
68 */
69 public function getClientData() {
70 return [
71 'prefix' => $this->prefix,
72 'extension' => $this->extension
73 ];
74 }
75
76 /**
77 * This is used on the server side to create a placeholder object for globs
78 * based on a specification received from the client. Because the content is
79 * never actually populated, it's not necessary to distinguish between the
80 * different glob types.
81 *
82 * @internal
83 * @param array $data
84 * @return OutputGlob
85 */
86 public static function newFromClientData( array $data ) {
87 if ( ( $data['type'] ?? '' ) === 'url' ) {
88 if ( !isset( $data['url'] ) ) {
89 throw new ShellboxError( 'Missing required parameter for URL glob: "url"' );
90 }
91 return new OutputGlobToUrl( $data['prefix'], $data['extension'], $data['url'] );
92 }
93 return new OutputGlobPlaceholder( $data['prefix'], $data['extension'] );
94 }
95
96 /**
97 * @return OutputFile[]
98 */
99 public function getFiles() {
100 return $this->files;
101 }
102
103 /**
104 * Determine whether a given relative path matches the glob pattern
105 *
106 * @param string $boxedName
107 * @return bool
108 */
109 public function isMatch( $boxedName ) {
110 $ext = '.' . $this->extension;
111 return substr_compare( $boxedName, $this->prefix, 0, strlen( $this->prefix ) ) === 0
112 && substr_compare( $boxedName, $ext, -strlen( $ext ) ) === 0;
113 }
114 }