-
Notifications
You must be signed in to change notification settings - Fork 8
/
Identifiable.php
64 lines (60 loc) · 2.35 KB
/
Identifiable.php
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
<?php
/**
* The StreamBuilder framework.
* Copyright 2023 Automattic, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace Tumblr\StreamBuilder;
/**
* Interface Identifiable
* An identifiable class should contain an identity which identifies the uniqueness of that instance.
* NOTE: While an Identifiable class is initialized from a stream template, it usually comes with
* some kind of path as its identity, for example 'template_name/stream_a/stream_b'
*/
abstract class Identifiable
{
/**
* @var string
*/
private $identity;
/**
* Templatable constructor.
* @param string $identity The identity indicates the uniqueness of this class,
* $identity accepts any arbitrary string as long as it's sufficient for identification.
* Examples: 'foo', 'filtered_legacy/stream/stream_wym', 'proportional/stream_weight_array/1/stream'
* @throws \InvalidArgumentException When empty identity is passed in.
*/
public function __construct(string $identity)
{
if (empty($identity)) {
throw new \InvalidArgumentException(sprintf('%s identity should not be empty.', get_class($this)));
}
$this->identity = $identity;
}
/**
* @param bool $class_name If you want to append class name at the end of an identity.
* Identity becomes 'template_name/stream_a/stream_b[StreamB]'
* @return string The identity.
*/
final public function get_identity(bool $class_name = false): string
{
if ($class_name) {
$short_class = Helpers::get_unqualified_class_name($this);
return sprintf('%s[%s]', $this->identity, $short_class);
}
return $this->identity;
}
}