Browse Source

Merge pull request #11216 from cakephp/3next-input-arguments

3.next- Start implementing the Arguments class.
Mark Story 8 years ago
parent
commit
e1ad320d27
2 changed files with 224 additions and 0 deletions
  1. 126 0
      src/Console/Arguments.php
  2. 98 0
      tests/TestCase/Console/ArgumentsTest.php

+ 126 - 0
src/Console/Arguments.php

@@ -0,0 +1,126 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Console;
+
+/**
+ * Provides an interface for interacting with
+ * a command's options and arguments.
+ */
+class Arguments
+{
+    /**
+     * Positional argument name map
+     *
+     * @var array
+     */
+    protected $argNames;
+
+    /**
+     * Positional arguments.
+     *
+     * @var string[]
+     */
+    protected $args;
+
+    /**
+     * Named options
+     *
+     * @var string[]
+     */
+    protected $options;
+
+    /**
+     * Constructor
+     *
+     * @param string[] $args Positional arguments
+     * @param array $options Named arguments
+     * @param string[] $argNames List of argument names. Order is expected to be
+     *  the same as $args.
+     */
+    public function __construct(array $args, array $options, array $argNames)
+    {
+        $this->args = $args;
+        $this->options = $options;
+        $this->argNames = $argNames;
+    }
+
+    /**
+     * Get all positional arguments.
+     *
+     * @return array
+     */
+    public function getArguments()
+    {
+        return $this->args;
+    }
+
+    /**
+     * Get positional arguments by index.
+     *
+     * @param int $index The argument index to access.
+     * @return string|null The argument value or null
+     */
+    public function getArgumentAt($index)
+    {
+        if ($this->hasArgumentAt($index)) {
+            return $this->args[$index];
+        }
+
+        return null;
+    }
+
+    /**
+     * Check if a positional argument exists
+     *
+     * @param int $index The argument index to check.
+     * @return bool
+     */
+    public function hasArgumentAt($index)
+    {
+        return isset($this->args[$index]);
+    }
+
+    /**
+     * Check if a positional argument exists by name
+     *
+     * @param string $name The argument name to check.
+     * @return bool
+     */
+    public function hasArgument($name)
+    {
+        $offset = array_search($name, $this->argNames, true);
+        if ($offset === false) {
+            return false;
+        }
+
+        return isset($this->args[$offset]);
+    }
+
+    /**
+     * Check if a positional argument exists by name
+     *
+     * @param string $name The argument name to check.
+     * @return string|null
+     */
+    public function getArgument($name)
+    {
+        $offset = array_search($name, $this->argNames, true);
+        if ($offset === false) {
+            return null;
+        }
+
+        return $this->args[$offset];
+    }
+}

+ 98 - 0
tests/TestCase/Console/ArgumentsTest.php

@@ -0,0 +1,98 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.6.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestSuite\Console;
+
+use Cake\Console\Arguments;
+use Cake\TestSuite\TestCase;
+
+/**
+ * Arguments test case.
+ */
+class ArgumentsTest extends TestCase
+{
+    /**
+     * Get all arguments
+     *
+     * @return void
+     */
+    public function testGetArguments()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $args = new Arguments($values, [], []);
+        $this->assertSame($values, $args->getArguments());
+    }
+
+    /**
+     * Get arguments by index
+     *
+     * @return void
+     */
+    public function testGetArgumentAt()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $args = new Arguments($values, [], []);
+        $this->assertSame($values[0], $args->getArgumentAt(0));
+        $this->assertSame($values[1], $args->getArgumentAt(1));
+        $this->assertNull($args->getArgumentAt(3));
+    }
+
+    /**
+     * check arguments by index
+     *
+     * @return void
+     */
+    public function testHasArgumentAt()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $args = new Arguments($values, [], []);
+        $this->assertTrue($args->hasArgumentAt(0));
+        $this->assertTrue($args->hasArgumentAt(1));
+        $this->assertFalse($args->hasArgumentAt(3));
+        $this->assertFalse($args->hasArgumentAt(-1));
+    }
+
+    /**
+     * check arguments by name
+     *
+     * @return void
+     */
+    public function testHasArgument()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $names = ['size', 'color', 'species', 'odd'];
+        $args = new Arguments($values, [], $names);
+        $this->assertTrue($args->hasArgument('size'));
+        $this->assertTrue($args->hasArgument('color'));
+        $this->assertFalse($args->hasArgument('hair'));
+        $this->assertFalse($args->hasArgument('Hair'), 'casing matters');
+        $this->assertFalse($args->hasArgument('odd'));
+    }
+
+    /**
+     * get arguments by name
+     *
+     * @return void
+     */
+    public function testGetArgument()
+    {
+        $values = ['big', 'brown', 'bear'];
+        $names = ['size', 'color', 'species', 'odd'];
+        $args = new Arguments($values, [], $names);
+        $this->assertSame($values[0], $args->getArgument('size'));
+        $this->assertSame($values[1], $args->getArgument('color'));
+        $this->assertNull($args->getArgument('Color'));
+        $this->assertNull($args->getArgument('hair'));
+    }
+}