Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Annotation / PluralTranslation.php
1 <?php
2
3 namespace Drupal\Core\Annotation;
4
5 use Drupal\Component\Annotation\AnnotationBase;
6
7 /**
8  * Defines an annotation object for strings that require plural forms.
9  *
10  * Note that the return values for both 'singular' and 'plural' keys needs to be
11  * passed to
12  * \Drupal\Core\StringTranslation\TranslationInterface::formatPlural().
13  *
14  * For example, the annotation can look like this:
15  * @code
16  *   label_count = @ PluralTranslation(
17  *     singular = "@count item",
18  *     plural = "@count items",
19  *     context = "cart_items",
20  *   ),
21  * @endcode
22  * Remove spaces after @ in your actual plugin - these are put into this sample
23  * code so that it is not recognized as annotation.
24  *
25  * Code samples that make use of this annotation class and the definition sample
26  * above:
27  * @code
28  *   // Returns: 1 item
29  *   $entity_type->getCountLabel(1);
30  *
31  *   // Returns: 5 items
32  *   $entity_type->getCountLabel(5);
33  * @endcode
34  *
35  * @see \Drupal\Core\Entity\EntityType::getSingularLabel()
36  * @see \Drupal\Core\Entity\EntityType::getPluralLabel()
37  * @see \Drupal\Core\Entity\EntityType::getCountLabel()
38  *
39  * @ingroup plugin_translatable
40  *
41  * @Annotation
42  */
43 class PluralTranslation extends AnnotationBase {
44
45   /**
46    * The string for the singular case.
47    *
48    * @var string
49    */
50   protected $singular;
51
52   /**
53    * The string for the plural case.
54    *
55    * @var string
56    */
57   protected $plural;
58
59   /**
60    * The context the source strings belong to.
61    *
62    * @var string
63    */
64   protected $context;
65
66   /**
67    * Constructs a new class instance.
68    *
69    * @param array $values
70    *   An associative array with the following keys:
71    *   - singular: The string for the singular case.
72    *   - plural: The string for the plural case.
73    *   - context: The context the source strings belong to.
74    *
75    * @throws \InvalidArgumentException
76    *   Thrown when the keys 'singular' or 'plural' are missing from the $values
77    *   array.
78    */
79   public function __construct(array $values) {
80     if (!isset($values['singular'])) {
81       throw new \InvalidArgumentException('Missing "singular" value in the PluralTranslation annotation');
82     }
83     if (!isset($values['plural'])) {
84       throw new \InvalidArgumentException('Missing "plural" value in the PluralTranslation annotation');
85     }
86
87     $this->singular = $values['singular'];
88     $this->plural = $values['plural'];
89     if (isset($values['context'])) {
90       $this->context = $values['context'];
91     }
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function get() {
98     return [
99       'singular' => $this->singular,
100       'plural' => $this->plural,
101       'context' => $this->context,
102     ];
103   }
104
105 }