PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/core/lib/Drupal/Core/TypedData/DataDefinition.php

http://github.com/drupal/drupal
PHP | 386 lines | 141 code | 39 blank | 206 comment | 6 complexity | c02ebc6660a84139cfcc87f13c732b6d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * A typed data definition class for defining data based on defined data types.
  5. */
  6. class DataDefinition implements DataDefinitionInterface, \ArrayAccess {
  7. use TypedDataTrait;
  8. /**
  9. * The array holding values for all definition keys.
  10. *
  11. * @var array
  12. */
  13. protected $definition = [];
  14. /**
  15. * Creates a new data definition.
  16. *
  17. * @param string $type
  18. * The data type of the data; e.g., 'string', 'integer' or 'any'.
  19. *
  20. * @return static
  21. * A new DataDefinition object.
  22. */
  23. public static function create($type) {
  24. $definition['type'] = $type;
  25. return new static($definition);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function createFromDataType($type) {
  31. return self::create($type);
  32. }
  33. /**
  34. * Constructs a new data definition object.
  35. *
  36. * @param array $values
  37. * (optional) If given, an array of initial values to set on the definition.
  38. */
  39. public function __construct(array $values = []) {
  40. $this->definition = $values;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getDataType() {
  46. return !empty($this->definition['type']) ? $this->definition['type'] : 'any';
  47. }
  48. /**
  49. * Sets the data type.
  50. *
  51. * @param string $type
  52. * The data type to set.
  53. *
  54. * @return static
  55. * The object itself for chaining.
  56. */
  57. public function setDataType($type) {
  58. $this->definition['type'] = $type;
  59. return $this;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getLabel() {
  65. return isset($this->definition['label']) ? $this->definition['label'] : NULL;
  66. }
  67. /**
  68. * Sets the human-readable label.
  69. *
  70. * @param string $label
  71. * The label to set.
  72. *
  73. * @return static
  74. * The object itself for chaining.
  75. */
  76. public function setLabel($label) {
  77. $this->definition['label'] = $label;
  78. return $this;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getDescription() {
  84. return isset($this->definition['description']) ? $this->definition['description'] : NULL;
  85. }
  86. /**
  87. * Sets the human-readable description.
  88. *
  89. * @param string $description
  90. * The description to set.
  91. *
  92. * @return static
  93. * The object itself for chaining.
  94. */
  95. public function setDescription($description) {
  96. $this->definition['description'] = $description;
  97. return $this;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function isList() {
  103. return ($this instanceof ListDataDefinitionInterface);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function isReadOnly() {
  109. if (!isset($this->definition['read-only'])) {
  110. // Default to read-only if the data value is computed.
  111. return $this->isComputed();
  112. }
  113. return !empty($this->definition['read-only']);
  114. }
  115. /**
  116. * Sets whether the data is read-only.
  117. *
  118. * @param bool $read_only
  119. * Whether the data is read-only.
  120. *
  121. * @return static
  122. * The object itself for chaining.
  123. */
  124. public function setReadOnly($read_only) {
  125. $this->definition['read-only'] = $read_only;
  126. return $this;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function isComputed() {
  132. return !empty($this->definition['computed']);
  133. }
  134. /**
  135. * Sets whether the data is computed.
  136. *
  137. * @param bool $computed
  138. * Whether the data is computed.
  139. *
  140. * @return static
  141. * The object itself for chaining.
  142. */
  143. public function setComputed($computed) {
  144. $this->definition['computed'] = $computed;
  145. return $this;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function isRequired() {
  151. return !empty($this->definition['required']);
  152. }
  153. /**
  154. * Sets whether the data is required.
  155. *
  156. * @param bool $required
  157. * Whether the data is required.
  158. *
  159. * @return static
  160. * The object itself for chaining.
  161. */
  162. public function setRequired($required) {
  163. $this->definition['required'] = $required;
  164. return $this;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function getClass() {
  170. if (isset($this->definition['class'])) {
  171. return $this->definition['class'];
  172. }
  173. else {
  174. $type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
  175. return $type_definition['class'];
  176. }
  177. }
  178. /**
  179. * Sets the class used for creating the typed data object.
  180. *
  181. * @param string|null $class
  182. * The class to use.
  183. *
  184. * @return static
  185. * The object itself for chaining.
  186. */
  187. public function setClass($class) {
  188. $this->definition['class'] = $class;
  189. return $this;
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function getSettings() {
  195. return isset($this->definition['settings']) ? $this->definition['settings'] : [];
  196. }
  197. /**
  198. * Sets the array of settings, as required by the used class.
  199. *
  200. * @param array $settings
  201. * The array of settings.
  202. *
  203. * @return static
  204. * The object itself for chaining.
  205. */
  206. public function setSettings(array $settings) {
  207. $this->definition['settings'] = $settings;
  208. return $this;
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function getSetting($setting_name) {
  214. return isset($this->definition['settings'][$setting_name]) ? $this->definition['settings'][$setting_name] : NULL;
  215. }
  216. /**
  217. * Sets a definition setting.
  218. *
  219. * @param string $setting_name
  220. * The definition setting to set.
  221. * @param mixed $value
  222. * The value to set.
  223. *
  224. * @return static
  225. * The object itself for chaining.
  226. */
  227. public function setSetting($setting_name, $value) {
  228. $this->definition['settings'][$setting_name] = $value;
  229. return $this;
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function getConstraints() {
  235. $constraints = isset($this->definition['constraints']) ? $this->definition['constraints'] : [];
  236. $constraints += $this->getTypedDataManager()->getDefaultConstraints($this);
  237. return $constraints;
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function getConstraint($constraint_name) {
  243. $constraints = $this->getConstraints();
  244. return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
  245. }
  246. /**
  247. * Sets an array of validation constraints.
  248. *
  249. * @param array $constraints
  250. * An array of validation constraint definitions, keyed by constraint name.
  251. * Each constraint definition can be used for instantiating
  252. * \Symfony\Component\Validator\Constraint objects.
  253. *
  254. * @return $this
  255. */
  256. public function setConstraints(array $constraints) {
  257. $this->definition['constraints'] = $constraints;
  258. return $this;
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function addConstraint($constraint_name, $options = NULL) {
  264. $this->definition['constraints'][$constraint_name] = $options;
  265. return $this;
  266. }
  267. /**
  268. * {@inheritdoc}
  269. *
  270. * This is for BC support only.
  271. * @todo: Remove in https://www.drupal.org/node/1928868.
  272. */
  273. public function offsetExists($offset) {
  274. // PHP's array access does not work correctly with isset(), so we have to
  275. // bake isset() in here. See https://bugs.php.net/bug.php?id=41727.
  276. return array_key_exists($offset, $this->definition) && isset($this->definition[$offset]);
  277. }
  278. /**
  279. * {@inheritdoc}
  280. *
  281. * This is for BC support only.
  282. * @todo: Remove in https://www.drupal.org/node/1928868.
  283. */
  284. public function &offsetGet($offset) {
  285. if (!isset($this->definition[$offset])) {
  286. $this->definition[$offset] = NULL;
  287. }
  288. return $this->definition[$offset];
  289. }
  290. /**
  291. * {@inheritdoc}
  292. *
  293. * This is for BC support only.
  294. * @todo: Remove in https://www.drupal.org/node/1928868.
  295. */
  296. public function offsetSet($offset, $value) {
  297. $this->definition[$offset] = $value;
  298. }
  299. /**
  300. * {@inheritdoc}
  301. *
  302. * This is for BC support only.
  303. * @todo: Remove in https://www.drupal.org/node/1928868.
  304. */
  305. public function offsetUnset($offset) {
  306. unset($this->definition[$offset]);
  307. }
  308. /**
  309. * Returns all definition values as array.
  310. *
  311. * @return array
  312. */
  313. public function toArray() {
  314. return $this->definition;
  315. }
  316. /**
  317. * {@inheritdoc}
  318. */
  319. public function __sleep() {
  320. // Never serialize the typed data manager.
  321. $vars = get_object_vars($this);
  322. unset($vars['typedDataManager']);
  323. return array_keys($vars);
  324. }
  325. /**
  326. * {@inheritdoc}
  327. */
  328. public function isInternal() {
  329. // Respect the definition, otherwise default to TRUE for computed fields.
  330. if (isset($this->definition['internal'])) {
  331. return $this->definition['internal'];
  332. }
  333. return $this->isComputed();
  334. }
  335. /**
  336. * Sets the whether the data value should be internal.
  337. *
  338. * @param bool $internal
  339. * Whether the data value should be internal.
  340. *
  341. * @return $this
  342. */
  343. public function setInternal($internal) {
  344. $this->definition['internal'] = $internal;
  345. return $this;
  346. }
  347. }