SassMixinDefinitionNode.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /* SVN FILE: $Id: SassMixinDefinitionNode.php 118 2010-09-21 09:45:11Z chris.l.yates@gmail.com $ */
  3. /**
  4. * SassMixinDefinitionNode class file.
  5. * @author Chris Yates <chris.l.yates@gmail.com>
  6. * @copyright Copyright (c) 2010 PBM Web Development
  7. * @license http://phamlp.googlecode.com/files/license.txt
  8. * @package PHamlP
  9. * @subpackage Sass.tree
  10. */
  11. /**
  12. * SassMixinDefinitionNode class.
  13. * Represents a Mixin definition.
  14. * @package PHamlP
  15. * @subpackage Sass.tree
  16. */
  17. class SassMixinDefinitionNode extends SassNode {
  18. const NODE_IDENTIFIER = '=';
  19. const MATCH = '/^(=|@mixin\s+)([-\w]+)\s*(?:\((.+?)\))?\s*$/i';
  20. const IDENTIFIER = 1;
  21. const NAME = 2;
  22. const ARGUMENTS = 3;
  23. /**
  24. * @var string name of the mixin
  25. */
  26. private $name;
  27. /**
  28. * @var array arguments for the mixin as name=>value pairs were value is the
  29. * default value or null for required arguments
  30. */
  31. private $args = array();
  32. /**
  33. * SassMixinDefinitionNode constructor.
  34. * @param object source token
  35. * @return SassMixinDefinitionNode
  36. */
  37. public function __construct($token) {
  38. if ($token->level !== 0) {
  39. throw new SassMixinDefinitionNodeException('Mixins can only be defined at root level', array(), $this);
  40. }
  41. parent::__construct($token);
  42. preg_match(self::MATCH, $token->source, $matches);
  43. if (empty($matches)) {
  44. throw new SassMixinDefinitionNodeException('Invalid {what}', array('{what}'=>'Mixin'), $this);
  45. }
  46. $this->name = $matches[self::NAME];
  47. if (isset($matches[self::ARGUMENTS])) {
  48. foreach (explode(',', $matches[self::ARGUMENTS]) as $arg) {
  49. $arg = explode(
  50. ($matches[self::IDENTIFIER] === self::NODE_IDENTIFIER ? '=' : ':'),
  51. trim($arg)
  52. );
  53. $this->args[substr(trim($arg[0]), 1)] = (count($arg) == 2 ? trim($arg[1]) : null);
  54. } // foreach
  55. }
  56. }
  57. /**
  58. * Parse this node.
  59. * Add this mixin to the current context.
  60. * @param SassContext the context in which this node is parsed
  61. * @return array the parsed node - an empty array
  62. */
  63. public function parse($context) {
  64. $context->addMixin($this->name, $this);
  65. return array();
  66. }
  67. /**
  68. * Returns the arguments with default values for this mixin
  69. * @return array the arguments with default values for this mixin
  70. */
  71. public function getArgs() {
  72. return $this->args;
  73. }
  74. /**
  75. * Returns a value indicating if the token represents this type of node.
  76. * @param object token
  77. * @return boolean true if the token represents this type of node, false if not
  78. */
  79. public static function isa($token, $syntax = '') {
  80. return $token->source[0] === self::NODE_IDENTIFIER;
  81. }
  82. }