예제 #1
0
 /**
  * Describes the given resource. The description includes the list of
  * fields, their data types and value constraints.
  *
  * @history
  * 2014.04.15:
  *   (AT)  Initial implementation
  *
  * @version 2014.04.15
  * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**>
  *
  * @param string $resource_id
  *   Resource name
  * @throws \Cougar\Exceptions\NotFoundException
  * @throws \Cougar\Exceptions\Exception
  * @return \Cougar\RestService\Models\ResourceDescription
  *   Description of the resource
  */
 public function getResource($resource_id)
 {
     // Make sure the resource exists
     if (!array_key_exists($resource_id, $this->resources)) {
         throw new NotFoundException("Resource not found");
     }
     $resource = $this->resources[$resource_id];
     // Get the annotations
     try {
         $class_annotations = Annotations::extractFromObject($resource->class);
     } catch (Exception $e) {
         throw new Exception("Could not obtain information on the resource");
     }
     // Create the Resource Description object and set some values
     $resource_desc = new ResourceDescription();
     $resource_desc->resourceId = $resource->resourceId;
     $resource_desc->name = $resource->name;
     // See if we have a comment for the class
     foreach ($class_annotations->class as $annotation) {
         if ($annotation->name == "_comment") {
             // Set the description
             $resource_desc->description = $annotation->value;
             break;
         }
     }
     // Go through the public properties in the object
     foreach ($class_annotations->properties as $property => $annotations) {
         // Create a new entry for the property and give it its name
         $value = new Value();
         $value->name = $property;
         // Go through the annotations
         foreach ($annotations as $annotation) {
             switch ($annotation->name) {
                 case "_comment":
                     $value->description = $annotation->value;
                     break;
                 case "var":
                     // Get the values from the annotation
                     $values = array_combine(array("type", "description"), array_pad(preg_split('/\\s+/u', $annotation->value, 2), 2, ""));
                     // Generate the proper name
                     $value->type = $this->generateResourceName($values["type"], $value->list);
                     // See if this is a resource
                     $filtered_resources = Arrays::dataFilter($this->resources, "name", $value->type);
                     if ($filtered_resources) {
                         // Yep, it's a resource
                         $value->isResource = true;
                     }
                     // See if we have a description
                     if ($values["description"] && !$value->description) {
                         $value->description = $values["description"];
                     }
                     break;
                 case "View":
                     // See if the field is hidden or optional
                     if (mb_strpos($annotation->value, "hidden") !== false || mb_strpos($annotation->value, "optional") !== false) {
                         $value->optional = true;
                     }
                     break;
                 case "Optional":
                     $value->optional = true;
                     break;
             }
         }
         // Add the value
         $resource_desc->values[] = $value;
     }
     // Return the description
     return $resource_desc;
 }
예제 #2
0
 /**
  * @covers \Cougar\Util\Arrays::dataFilter
  */
 public function testDataFilterMultipleValuesNegate()
 {
     // Define the array with records
     $records = array(array("id" => 1, "firstName" => "Peter", "lastName" => "Stevens", "age" => 45), array("id" => 2, "firstName" => "John", "lastName" => "Stevens", "age" => 45), array("id" => 3, "firstName" => "John", "lastName" => "Smith", "age" => 45), array("id" => 4, "firstName" => "Mark", "lastName" => "Johnson", "age" => 58), array("id" => 5, "firstName" => "Michael", "lastName" => "Zimmerman", "age" => 19));
     // Filter the results by last name
     $filtered_records = Arrays::dataFilter($records, "firstName", array("John", "Michael"), false);
     $this->assertCount(2, $filtered_records);
 }